blob: 868afe9b7f0f0cf9d78e5534f2490ecdbc4d69c0 [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
350 for (i = 0; i < 4; ++i) {
Pekka Paalanen219b9822012-02-08 15:38:37 +0200351 GLfloat x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400352 weston_surface_to_global_float(surface,
353 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200354 if (x < min_x)
355 min_x = x;
356 if (x > max_x)
357 max_x = x;
358 if (y < min_y)
359 min_y = y;
360 if (y > max_y)
361 max_y = y;
362 }
363
Pekka Paalanen219b9822012-02-08 15:38:37 +0200364 int_x = floorf(min_x);
365 int_y = floorf(min_y);
366 pixman_region32_init_rect(bbox, int_x, int_y,
367 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200368}
369
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200370static void
371weston_surface_update_transform_disable(struct weston_surface *surface)
372{
373 surface->transform.enabled = 0;
374
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200375 /* round off fractions when not transformed */
376 surface->geometry.x = roundf(surface->geometry.x);
377 surface->geometry.y = roundf(surface->geometry.y);
378
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200379 pixman_region32_init_rect(&surface->transform.boundingbox,
380 surface->geometry.x,
381 surface->geometry.y,
382 surface->geometry.width,
383 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500384
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400385 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500386 pixman_region32_copy(&surface->transform.opaque,
387 &surface->opaque);
388 pixman_region32_translate(&surface->transform.opaque,
389 surface->geometry.x,
390 surface->geometry.y);
391 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200392}
393
394static int
395weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200396{
397 struct weston_matrix *matrix = &surface->transform.matrix;
398 struct weston_matrix *inverse = &surface->transform.inverse;
399 struct weston_transform *tform;
400
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200401 surface->transform.enabled = 1;
402
403 /* Otherwise identity matrix, but with x and y translation. */
404 surface->transform.position.matrix.d[12] = surface->geometry.x;
405 surface->transform.position.matrix.d[13] = surface->geometry.y;
406
407 weston_matrix_init(matrix);
408 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
409 weston_matrix_multiply(matrix, &tform->matrix);
410
411 if (weston_matrix_invert(inverse, matrix) < 0) {
412 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200413 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200414 " transformation not invertible.\n", surface);
415 return -1;
416 }
417
418 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
419 surface->geometry.height,
420 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500421
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200422 return 0;
423}
424
425WL_EXPORT void
426weston_surface_update_transform(struct weston_surface *surface)
427{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200428 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200429 return;
430
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200431 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200432
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500433 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200434
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200435 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500436 pixman_region32_fini(&surface->transform.opaque);
437 pixman_region32_init(&surface->transform.opaque);
438
439 if (region_is_undefined(&surface->input))
440 pixman_region32_init_rect(&surface->input, 0, 0,
441 surface->geometry.width,
442 surface->geometry.height);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200443
Pekka Paalanencd403622012-01-25 13:37:39 +0200444 /* transform.position is always in transformation_list */
445 if (surface->geometry.transformation_list.next ==
446 &surface->transform.position.link &&
447 surface->geometry.transformation_list.prev ==
448 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200449 weston_surface_update_transform_disable(surface);
450 } else {
451 if (weston_surface_update_transform_enable(surface) < 0)
452 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200453 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200454
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400455 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200456
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300457 if (weston_surface_is_mapped(surface))
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +0200458 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200459}
460
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200461WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100462weston_surface_to_global_fixed(struct weston_surface *surface,
463 wl_fixed_t sx, wl_fixed_t sy,
464 wl_fixed_t *x, wl_fixed_t *y)
465{
466 GLfloat xf, yf;
467
468 weston_surface_to_global_float(surface,
469 wl_fixed_to_double(sx),
470 wl_fixed_to_double(sy),
471 &xf, &yf);
472 *x = wl_fixed_from_double(xf);
473 *y = wl_fixed_from_double(yf);
474}
475
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200476static void
477surface_from_global_float(struct weston_surface *surface,
Daniel Stonebd3489b2012-05-08 17:17:53 +0100478 GLfloat x, GLfloat y, GLfloat *sx, GLfloat *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200479{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200480 if (surface->transform.enabled) {
481 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
482
483 weston_matrix_transform(&surface->transform.inverse, &v);
484
485 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200486 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200487 "weston_surface_from_global(), divisor = %g\n",
488 v.f[3]);
489 *sx = 0;
490 *sy = 0;
491 return;
492 }
493
Pekka Paalanencd403622012-01-25 13:37:39 +0200494 *sx = v.f[0] / v.f[3];
495 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200496 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200497 *sx = x - surface->geometry.x;
498 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200499 }
500}
501
502WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100503weston_surface_from_global_fixed(struct weston_surface *surface,
504 wl_fixed_t x, wl_fixed_t y,
505 wl_fixed_t *sx, wl_fixed_t *sy)
506{
507 GLfloat sxf, syf;
508
Daniel Stonebd3489b2012-05-08 17:17:53 +0100509 surface_from_global_float(surface,
510 wl_fixed_to_double(x),
511 wl_fixed_to_double(y),
512 &sxf, &syf);
513 *sx = wl_fixed_from_double(sxf);
514 *sy = wl_fixed_from_double(syf);
515}
516
517WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200518weston_surface_from_global(struct weston_surface *surface,
519 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
520{
521 GLfloat sxf, syf;
522
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200523 surface_from_global_float(surface, x, y, &sxf, &syf);
524 *sx = floorf(sxf);
525 *sy = floorf(syf);
526}
527
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400528WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400529weston_surface_schedule_repaint(struct weston_surface *surface)
530{
531 struct weston_output *output;
532
533 wl_list_for_each(output, &surface->compositor->output_list, link)
534 if (surface->output_mask & (1 << output->id))
535 weston_output_schedule_repaint(output);
536}
537
538WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500539weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500540{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400541 pixman_region32_union_rect(&surface->damage, &surface->damage,
542 0, 0, surface->geometry.width,
543 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200544
Kristian Høgsberg98238702012-08-03 16:29:12 -0400545 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500546}
547
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400548WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500549weston_surface_configure(struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200550 GLfloat x, GLfloat y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400551{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200552 surface->geometry.x = x;
553 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200554 surface->geometry.width = width;
555 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200556 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400557}
558
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200559WL_EXPORT void
560weston_surface_set_position(struct weston_surface *surface,
561 GLfloat x, GLfloat y)
562{
563 surface->geometry.x = x;
564 surface->geometry.y = y;
565 surface->geometry.dirty = 1;
566}
567
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300568WL_EXPORT int
569weston_surface_is_mapped(struct weston_surface *surface)
570{
571 if (surface->output)
572 return 1;
573 else
574 return 0;
575}
576
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400577WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500578weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500579{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400580 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500581
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400582 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500583
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400584 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500585}
586
Tiago Vignatti9d393522012-02-10 16:26:19 +0200587static struct weston_surface *
588weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100589 wl_fixed_t x, wl_fixed_t y,
590 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200591{
592 struct weston_surface *surface;
593
594 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100595 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500596 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100597 wl_fixed_to_int(*sx),
598 wl_fixed_to_int(*sy),
599 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200600 return surface;
601 }
602
603 return NULL;
604}
605
606static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400607weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500608{
Scott Moreau447013d2012-02-18 05:05:29 -0700609 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500610 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400611 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500612
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400613 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100614 return;
615
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400616 surface = weston_compositor_pick_surface(seat->compositor,
617 pointer->x,
618 pointer->y,
619 &pointer->current_x,
620 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500621
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400622 if (&surface->surface != pointer->current) {
623 interface = pointer->grab->interface;
624 pointer->current = &surface->surface;
625 interface->focus(pointer->grab, &surface->surface,
626 pointer->current_x,
627 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500628 }
629
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400630 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500631 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100632 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400633 pointer->x,
634 pointer->y,
635 &pointer->grab->x,
636 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500637}
638
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400639static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500640weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400641{
Daniel Stone37816df2012-05-16 18:45:18 +0100642 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400643
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500644 if (!compositor->focus)
645 return;
646
Daniel Stone37816df2012-05-16 18:45:18 +0100647 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400648 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400649}
650
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400651WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500652weston_surface_unmap(struct weston_surface *surface)
653{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100654 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500655
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500656 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500657 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500658 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500659
Daniel Stone4dab5db2012-05-30 16:31:53 +0100660 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100661 if (seat->seat.keyboard &&
662 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100663 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100664 if (seat->seat.pointer &&
665 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100666 wl_pointer_set_focus(seat->seat.pointer,
667 NULL,
668 wl_fixed_from_int(0),
669 wl_fixed_from_int(0));
670 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500671
Kristian Høgsberg98238702012-08-03 16:29:12 -0400672 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500673}
674
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400675struct weston_frame_callback {
676 struct wl_resource resource;
677 struct wl_list link;
678};
679
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500680static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400681destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500682{
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200683 int i;
684
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500685 struct weston_surface *surface =
686 container_of(resource,
687 struct weston_surface, surface.resource);
688 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400689 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400690
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300691 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500692 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400693
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200694 glDeleteTextures(surface->num_textures, surface->textures);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200695
Benjamin Franzke06286262011-05-06 19:12:33 +0200696 if (surface->buffer)
697 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400698
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200699 for (i = 0; i < surface->num_images; i++)
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400700 compositor->destroy_image(compositor->egl_display,
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200701 surface->images[i]);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200702
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200703 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200704 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500705 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500706 pixman_region32_fini(&surface->clip);
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500707 if (!region_is_undefined(&surface->input))
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500708 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200709
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400710 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
711 wl_resource_destroy(&cb->resource);
712
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400713 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500714}
715
Alex Wu8811bf92012-02-28 18:07:54 +0800716WL_EXPORT void
717weston_surface_destroy(struct weston_surface *surface)
718{
719 /* Not a valid way to destroy a client surface */
720 assert(surface->surface.resource.client == NULL);
721
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400722 wl_signal_emit(&surface->surface.resource.destroy_signal,
723 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800724 destroy_surface(&surface->surface.resource);
725}
726
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100727static void
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200728ensure_textures(struct weston_surface *es, int num_textures)
729{
730 int i;
731
732 if (num_textures <= es->num_textures)
733 return;
734
735 for (i = es->num_textures; i < num_textures; i++) {
736 glGenTextures(1, &es->textures[i]);
Rob Clarke3b95912012-08-31 16:42:18 -0500737 glBindTexture(es->target, es->textures[i]);
738 glTexParameteri(es->target,
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200739 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
Rob Clarke3b95912012-08-31 16:42:18 -0500740 glTexParameteri(es->target,
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200741 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
742 }
743 es->num_textures = num_textures;
Rob Clarke3b95912012-08-31 16:42:18 -0500744 glBindTexture(es->target, 0);
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200745}
746
747static void
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -0400748weston_surface_attach(struct wl_surface *surface, struct wl_buffer *buffer)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100749{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500750 struct weston_surface *es = (struct weston_surface *) surface;
751 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400752 EGLint attribs[3], format;
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200753 int i, num_planes;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100754
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300755 if (es->buffer) {
756 weston_buffer_post_release(es->buffer);
757 wl_list_remove(&es->buffer_destroy_listener.link);
758 }
759
760 es->buffer = buffer;
761
762 if (!buffer) {
763 if (weston_surface_is_mapped(es))
764 weston_surface_unmap(es);
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200765 for (i = 0; i < es->num_images; i++) {
766 ec->destroy_image(ec->egl_display, es->images[i]);
767 es->images[i] = NULL;
Kristian Høgsbergb8ceaaa2012-06-19 15:41:12 -0400768 }
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200769 es->num_images = 0;
770 glDeleteTextures(es->num_textures, es->textures);
771 es->num_textures = 0;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300772 return;
773 }
774
775 buffer->busy_count++;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400776 wl_signal_add(&es->buffer->resource.destroy_signal,
777 &es->buffer_destroy_listener);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300778
779 if (es->geometry.width != buffer->width ||
780 es->geometry.height != buffer->height) {
781 undef_region(&es->input);
782 pixman_region32_fini(&es->opaque);
783 pixman_region32_init(&es->opaque);
784 }
785
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100786 if (wl_buffer_is_shm(buffer)) {
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200787 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
Rob Clarke3b95912012-08-31 16:42:18 -0500788 es->target = GL_TEXTURE_2D;
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200789
790 ensure_textures(es, 1);
791 glBindTexture(GL_TEXTURE_2D, es->textures[0]);
Kristian Høgsbergfb6de222012-03-27 17:10:13 -0400792 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
793 es->pitch, es->buffer->height, 0,
794 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
Kristian Høgsbergd6ad1222012-05-17 11:11:15 -0400795 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
Pekka Paalanen6b5585b2012-08-30 16:47:19 -0500796 es->shader = &ec->texture_shader_rgbx;
Kristian Høgsbergd6ad1222012-05-17 11:11:15 -0400797 else
Pekka Paalanen6b5585b2012-08-30 16:47:19 -0500798 es->shader = &ec->texture_shader_rgba;
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200799 } else if (ec->query_buffer(ec->egl_display, buffer,
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400800 EGL_TEXTURE_FORMAT, &format)) {
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200801 for (i = 0; i < es->num_images; i++)
Kristian Høgsberg971cbc82012-07-17 14:21:25 -0400802 ec->destroy_image(ec->egl_display, es->images[i]);
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200803 es->num_images = 0;
Rob Clarke3b95912012-08-31 16:42:18 -0500804 es->target = GL_TEXTURE_2D;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400805 switch (format) {
806 case EGL_TEXTURE_RGB:
807 case EGL_TEXTURE_RGBA:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200808 default:
809 num_planes = 1;
810 es->shader = &ec->texture_shader_rgba;
811 break;
Rob Clarke3b95912012-08-31 16:42:18 -0500812 case EGL_TEXTURE_EXTERNAL_WL:
813 num_planes = 1;
814 es->target = GL_TEXTURE_EXTERNAL_OES;
815 es->shader = &ec->texture_shader_egl_external;
816 break;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400817 case EGL_TEXTURE_Y_UV_WL:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200818 num_planes = 2;
819 es->shader = &ec->texture_shader_y_uv;
820 break;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400821 case EGL_TEXTURE_Y_U_V_WL:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200822 num_planes = 3;
823 es->shader = &ec->texture_shader_y_u_v;
824 break;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400825 case EGL_TEXTURE_Y_XUXV_WL:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200826 num_planes = 2;
827 es->shader = &ec->texture_shader_y_xuxv;
828 break;
829 }
830
831 ensure_textures(es, num_planes);
832 for (i = 0; i < num_planes; i++) {
833 attribs[0] = EGL_WAYLAND_PLANE_WL;
834 attribs[1] = i;
835 attribs[2] = EGL_NONE;
836 es->images[i] = ec->create_image(ec->egl_display,
837 NULL,
838 EGL_WAYLAND_BUFFER_WL,
839 buffer, attribs);
Rob Clark48cd58b2012-08-13 17:39:17 -0500840 if (!es->images[i]) {
841 weston_log("failed to create img for plane %d\n", i);
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200842 continue;
Rob Clark48cd58b2012-08-13 17:39:17 -0500843 }
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200844 es->num_images++;
845
846 glActiveTexture(GL_TEXTURE0 + i);
Rob Clarke3b95912012-08-31 16:42:18 -0500847 glBindTexture(es->target, es->textures[i]);
848 ec->image_target_texture_2d(es->target,
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200849 es->images[i]);
850 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400851
Ander Conselvan de Oliveira9390ae32012-03-27 17:36:38 +0300852 es->pitch = buffer->width;
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200853 } else {
Rob Clark48cd58b2012-08-13 17:39:17 -0500854 weston_log("unhandled buffer type!\n");
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100855 }
856}
857
Rob Clark0e5a2d02012-08-30 16:47:18 -0500858
859#define max(a, b) (((a) > (b)) ? (a) : (b))
860#define min(a, b) (((a) > (b)) ? (b) : (a))
861#define clip(x, a, b) min(max(x, a), b)
862#define sign(x) ((x) >= 0)
863
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400864static int
Rob Clark0e5a2d02012-08-30 16:47:18 -0500865calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
866 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500867{
Rob Clark0e5a2d02012-08-30 16:47:18 -0500868 int i, n = 0;
869 GLfloat min_x, max_x, min_y, max_y;
870 GLfloat x[4] = {
871 surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1,
872 };
873 GLfloat y[4] = {
874 surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2,
875 };
876 GLfloat cx1 = rect->x1;
877 GLfloat cx2 = rect->x2;
878 GLfloat cy1 = rect->y1;
879 GLfloat cy2 = rect->y2;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500880
Rob Clark0e5a2d02012-08-30 16:47:18 -0500881 GLfloat dist_squared(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
882 {
883 GLfloat dx = (x1 - x2);
884 GLfloat dy = (y1 - y2);
885 return dx * dx + dy * dy;
886 }
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400887
Rob Clark0e5a2d02012-08-30 16:47:18 -0500888 void append_vertex(GLfloat x, GLfloat y)
889 {
890 /* don't emit duplicate vertices: */
891 if ((n > 0) && (ex[n-1] == x) && (ey[n-1] == y))
892 return;
893 ex[n] = x;
894 ey[n] = y;
895 n++;
896 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500897
Rob Clark0e5a2d02012-08-30 16:47:18 -0500898 /* transform surface to screen space: */
899 for (i = 0; i < 4; i++)
900 weston_surface_to_global_float(es, x[i], y[i], &x[i], &y[i]);
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500901
Rob Clark0e5a2d02012-08-30 16:47:18 -0500902 /* find bounding box: */
903 min_x = max_x = x[0];
904 min_y = max_y = y[0];
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500905
Rob Clark0e5a2d02012-08-30 16:47:18 -0500906 for (i = 1; i < 4; i++) {
907 min_x = min(min_x, x[i]);
908 max_x = max(max_x, x[i]);
909 min_y = min(min_y, y[i]);
910 max_y = max(max_y, y[i]);
911 }
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500912
Rob Clark0e5a2d02012-08-30 16:47:18 -0500913 /* First, simple bounding box check to discard early transformed
914 * surface rects that do not intersect with the clip region:
915 */
916 if ((min_x > cx2) || (max_x < cx1) ||
917 (min_y > cy2) || (max_y < cy1))
918 return 0;
919
920 /* Simple case, bounding box edges are parallel to surface edges,
921 * there will be only four edges. We just need to clip the surface
922 * vertices to the clip rect bounds:
923 */
924 if (!es->transform.enabled) {
925 for (i = 0; i < 4; i++) {
926 ex[n] = clip(x[i], cx1, cx2);
927 ey[n] = clip(y[i], cy1, cy2);
928 n++;
929 }
930 return 4;
931 }
932
933 /* Hard case, transformation applied. We need to find the vertices
934 * of the shape that is the intersection of the clip rect and
935 * transformed surface. This can be anything from 3 to 8 sides.
936 *
937 * Observation: all the resulting vertices will be the intersection
938 * points of the transformed surface and the clip rect, plus the
939 * vertices of the clip rect which are enclosed by the transformed
940 * surface and the vertices of the transformed surface which are
941 * enclosed by the clip rect.
942 *
943 * Observation: there will be zero, one, or two resulting vertices
944 * for each edge of the src rect.
945 *
946 * Loop over four edges of the transformed rect:
947 */
948 for (i = 0; i < 4; i++) {
949 GLfloat x1, y1, x2, y2;
950 int last_n = n;
951
952 x1 = x[i];
953 y1 = y[i];
954
955 /* if this vertex is contained in the clip rect, use it as-is: */
956 if ((cx1 <= x1) && (x1 <= cx2) &&
957 (cy1 <= y1) && (y1 <= cy2))
958 append_vertex(x1, y1);
959
960 /* for remaining, we consider the point as part of a line: */
961 x2 = x[(i+1) % 4];
962 y2 = y[(i+1) % 4];
963
964 if (x1 == x2) {
965 append_vertex(clip(x1, cx1, cx2), clip(y1, cy1, cy2));
966 append_vertex(clip(x2, cx1, cx2), clip(y2, cy1, cy2));
967 } else if (y1 == y2) {
968 append_vertex(clip(x1, cx1, cx2), clip(y1, cy1, cy2));
969 append_vertex(clip(x2, cx1, cx2), clip(y2, cy1, cy2));
970 } else {
971 GLfloat m, c, p;
972 GLfloat tx[2], ty[2];
973 int tn = 0;
974
975 int intersect_horiz(GLfloat y, GLfloat *p)
976 {
977 GLfloat x;
978
979 /* if y does not lie between y1 and y2, no
980 * intersection possible
981 */
982 if (sign(y-y1) == sign(y-y2))
983 return 0;
984
985 x = (y - c) / m;
986
987 /* if x does not lie between cx1 and cx2, no
988 * intersection:
989 */
990 if (sign(x-cx1) == sign(x-cx2))
991 return 0;
992
993 *p = x;
994 return 1;
995 }
996
997 int intersect_vert(GLfloat x, GLfloat *p)
998 {
999 GLfloat y;
1000
1001 if (sign(x-x1) == sign(x-x2))
1002 return 0;
1003
1004 y = m * x + c;
1005
1006 if (sign(y-cy1) == sign(y-cy2))
1007 return 0;
1008
1009 *p = y;
1010 return 1;
1011 }
1012
1013 /* y = mx + c */
1014 m = (y2 - y1) / (x2 - x1);
1015 c = y1 - m * x1;
1016
1017 /* check for up to two intersections with the four edges
1018 * of the clip rect. Note that we don't know the orientation
1019 * of the transformed surface wrt. the clip rect. So if when
1020 * there are two intersection points, we need to put the one
1021 * closest to x1,y1 first:
1022 */
1023
1024 /* check top clip rect edge: */
1025 if (intersect_horiz(cy1, &p)) {
1026 ty[tn] = cy1;
1027 tx[tn] = p;
1028 tn++;
1029 }
1030
1031 /* check right clip rect edge: */
1032 if (intersect_vert(cx2, &p)) {
1033 ty[tn] = p;
1034 tx[tn] = cx2;
1035 tn++;
1036 if (tn == 2)
1037 goto edge_check_done;
1038 }
1039
1040 /* check bottom clip rect edge: */
1041 if (intersect_horiz(cy2, &p)) {
1042 ty[tn] = cy2;
1043 tx[tn] = p;
1044 tn++;
1045 if (tn == 2)
1046 goto edge_check_done;
1047 }
1048
1049 /* check left clip rect edge: */
1050 if (intersect_vert(cx1, &p)) {
1051 ty[tn] = p;
1052 tx[tn] = cx1;
1053 tn++;
1054 }
1055
1056edge_check_done:
1057 if (tn == 1) {
1058 append_vertex(tx[0], ty[0]);
1059 } else if (tn == 2) {
1060 if (dist_squared(x1, y1, tx[0], ty[0]) <
1061 dist_squared(x1, y1, tx[1], ty[1])) {
1062 append_vertex(tx[0], ty[0]);
1063 append_vertex(tx[1], ty[1]);
1064 } else {
1065 append_vertex(tx[1], ty[1]);
1066 append_vertex(tx[0], ty[0]);
1067 }
1068 }
1069
1070 if (n == last_n) {
1071 GLfloat best_x=0, best_y=0;
1072 uint32_t d, best_d = (unsigned int)-1; /* distance squared */
1073 uint32_t max_d = dist_squared(x2, y2,
1074 x[(i+2) % 4], y[(i+2) % 4]);
1075
1076 /* if there are no vertices on this line, it could be that
1077 * there is a vertex of the clip rect that is enclosed by
1078 * the transformed surface. Find the vertex of the clip
1079 * rect that is reached by the shortest line perpendicular
1080 * to the current edge, if any.
1081 *
1082 * slope of perpendicular is 1/m, so
1083 *
1084 * cy = -cx/m + c2
1085 * c2 = cy + cx/m
1086 *
1087 */
1088
1089 int perp_intersect(GLfloat cx, GLfloat cy, uint32_t *d)
1090 {
1091 GLfloat c2 = cy + cx/m;
1092 GLfloat x = (c2 - c) / (m + 1/m);
1093
1094 /* if the x position of the intersection of the
1095 * perpendicular with the transformed edge does
1096 * not lie within the bounds of the edge, then
1097 * no intersection:
1098 */
1099 if (sign(x-x1) == sign(x-x2))
1100 return 0;
1101
1102 *d = dist_squared(cx, cy, x, (m * x) + c);
1103
1104 /* if intersection distance is further away than
1105 * opposite edge of surface region, it is invalid:
1106 */
1107 if (*d > max_d)
1108 return 0;
1109
1110 return 1;
1111 }
1112
1113 if (perp_intersect(cx1, cy1, &d)) {
1114 best_x = cx1;
1115 best_y = cy1;
1116 best_d = d;
1117 }
1118
1119 if (perp_intersect(cx1, cy2, &d) && (d < best_d)) {
1120 best_x = cx1;
1121 best_y = cy2;
1122 best_d = d;
1123 }
1124
1125 if (perp_intersect(cx2, cy2, &d) && (d < best_d)) {
1126 best_x = cx2;
1127 best_y = cy2;
1128 best_d = d;
1129 }
1130
1131 if (perp_intersect(cx2, cy1, &d) && (d < best_d)) {
1132 best_x = cx2;
1133 best_y = cy1;
1134 best_d = d;
1135 }
1136
1137 if (best_d != (unsigned int)-1) // XXX can this happen?
1138 append_vertex(best_x, best_y);
1139 }
1140 }
1141
Kristian Høgsberg525e4c02011-02-14 10:39:54 -05001142 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001143
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001144 return n;
1145}
1146
Rob Clark0e5a2d02012-08-30 16:47:18 -05001147static int
1148texture_region(struct weston_surface *es, pixman_region32_t *region,
1149 pixman_region32_t *surf_region)
1150{
1151 struct weston_compositor *ec = es->compositor;
1152 GLfloat *v, inv_width, inv_height;
1153 unsigned int *vtxcnt, nvtx = 0;
1154 pixman_box32_t *rects, *surf_rects;
1155 int i, j, k, nrects, nsurf;
1156
1157 rects = pixman_region32_rectangles(region, &nrects);
1158 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
1159
Pekka Paalanen85a46672012-09-04 13:55:42 +03001160 /* worst case we can have 8 vertices per rect (ie. clipped into
Rob Clark0e5a2d02012-08-30 16:47:18 -05001161 * an octagon):
1162 */
Pekka Paalanen85a46672012-09-04 13:55:42 +03001163 v = wl_array_add(&ec->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
Rob Clark0e5a2d02012-08-30 16:47:18 -05001164 vtxcnt = wl_array_add(&ec->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
1165
1166 inv_width = 1.0 / es->pitch;
1167 inv_height = 1.0 / es->geometry.height;
1168
1169 for (i = 0; i < nrects; i++) {
1170 pixman_box32_t *rect = &rects[i];
1171 for (j = 0; j < nsurf; j++) {
1172 pixman_box32_t *surf_rect = &surf_rects[j];
Pekka Paalanen85a46672012-09-04 13:55:42 +03001173 GLfloat sx, sy;
Rob Clark0e5a2d02012-08-30 16:47:18 -05001174 GLfloat ex[8], ey[8]; /* edge points in screen space */
1175 int n;
1176
Rob Clark0e5a2d02012-08-30 16:47:18 -05001177 /* The transformed surface, after clipping to the clip region,
1178 * can have as many as eight sides, emitted as a triangle-fan.
Pekka Paalanen85a46672012-09-04 13:55:42 +03001179 * The first vertex in the triangle fan can be chosen arbitrarily,
1180 * since the area is guaranteed to be convex.
Rob Clark0e5a2d02012-08-30 16:47:18 -05001181 *
1182 * If a corner of the transformed surface falls outside of the
1183 * clip region, instead of emitting one vertex for the corner
1184 * of the surface, up to two are emitted for two corresponding
1185 * intersection point(s) between the surface and the clip region.
1186 *
1187 * To do this, we first calculate the (up to eight) points that
1188 * form the intersection of the clip rect and the transformed
Pekka Paalanen85a46672012-09-04 13:55:42 +03001189 * surface.
Rob Clark0e5a2d02012-08-30 16:47:18 -05001190 */
1191 n = calculate_edges(es, rect, surf_rect, ex, ey);
1192 if (n < 3)
1193 continue;
1194
Pekka Paalanen85a46672012-09-04 13:55:42 +03001195 /* emit edge points: */
Rob Clark0e5a2d02012-08-30 16:47:18 -05001196 for (k = 0; k < n; k++) {
Pekka Paalanen85a46672012-09-04 13:55:42 +03001197 surface_from_global_float(es, ex[k], ey[k], &sx, &sy);
1198 /* position: */
1199 *(v++) = ex[k];
1200 *(v++) = ey[k];
1201 /* texcoord: */
1202 *(v++) = sx * inv_width;
1203 *(v++) = sy * inv_height;
Rob Clark0e5a2d02012-08-30 16:47:18 -05001204 }
Rob Clark0e5a2d02012-08-30 16:47:18 -05001205
Pekka Paalanen85a46672012-09-04 13:55:42 +03001206 vtxcnt[nvtx++] = n;
Rob Clark0e5a2d02012-08-30 16:47:18 -05001207 }
1208 }
1209
1210 return nvtx;
1211}
1212
1213static void
Pekka Paalanen07c91f82012-08-30 16:47:21 -05001214triangle_fan_debug(struct weston_surface *surface, int first, int count)
1215{
1216 struct weston_compositor *compositor = surface->compositor;
1217 int i;
1218 GLushort *buffer;
1219 GLushort *index;
1220 int nelems;
1221 static int color_idx = 0;
1222 static const GLfloat color[][4] = {
1223 { 1.0, 0.0, 0.0, 1.0 },
1224 { 0.0, 1.0, 0.0, 1.0 },
1225 { 0.0, 0.0, 1.0, 1.0 },
1226 { 1.0, 1.0, 1.0, 1.0 },
1227 };
1228
1229 nelems = (count - 1 + count - 2) * 2;
1230
1231 buffer = malloc(sizeof(GLushort) * nelems);
1232 index = buffer;
1233
1234 for (i = 1; i < count; i++) {
1235 *index++ = first;
1236 *index++ = first + i;
1237 }
1238
1239 for (i = 2; i < count; i++) {
1240 *index++ = first + i - 1;
1241 *index++ = first + i;
1242 }
1243
1244 glUseProgram(compositor->solid_shader.program);
1245 glUniform4fv(compositor->solid_shader.color_uniform, 1,
Pekka Paalanen27a8c532012-09-04 13:55:43 +03001246 color[color_idx++ % ARRAY_LENGTH(color)]);
Pekka Paalanen07c91f82012-08-30 16:47:21 -05001247 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
Pekka Paalanenc73bd542012-09-03 16:48:43 +03001248 glUseProgram(compositor->current_shader->program);
Pekka Paalanen07c91f82012-08-30 16:47:21 -05001249 free(buffer);
1250}
1251
1252static void
Rob Clark0e5a2d02012-08-30 16:47:18 -05001253repaint_region(struct weston_surface *es, pixman_region32_t *region,
1254 pixman_region32_t *surf_region)
1255{
1256 struct weston_compositor *ec = es->compositor;
1257 GLfloat *v;
1258 unsigned int *vtxcnt;
1259 int i, first, nfans;
1260
1261 /* The final region to be painted is the intersection of
1262 * 'region' and 'surf_region'. However, 'region' is in the global
1263 * coordinates, and 'surf_region' is in the surface-local
Pekka Paalanen85a46672012-09-04 13:55:42 +03001264 * coordinates. texture_region() will iterate over all pairs of
Rob Clark0e5a2d02012-08-30 16:47:18 -05001265 * rectangles from both regions, compute the intersection
1266 * polygon for each pair, and store it as a triangle fan if
Pekka Paalanen85a46672012-09-04 13:55:42 +03001267 * it has a non-zero area (at least 3 vertices, actually).
Rob Clark0e5a2d02012-08-30 16:47:18 -05001268 */
1269 nfans = texture_region(es, region, surf_region);
1270
1271 v = ec->vertices.data;
1272 vtxcnt = ec->vtxcnt.data;
1273
1274 /* position: */
1275 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
1276 glEnableVertexAttribArray(0);
1277
1278 /* texcoord: */
1279 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
1280 glEnableVertexAttribArray(1);
1281
1282 for (i = 0, first = 0; i < nfans; i++) {
1283 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
Pekka Paalanen07c91f82012-08-30 16:47:21 -05001284 if (ec->fan_debug)
1285 triangle_fan_debug(es, first, vtxcnt[i]);
Rob Clark0e5a2d02012-08-30 16:47:18 -05001286 first += vtxcnt[i];
1287 }
1288
1289 glDisableVertexAttribArray(1);
1290 glDisableVertexAttribArray(0);
1291
1292 ec->vertices.size = 0;
1293 ec->vtxcnt.size = 0;
1294}
1295
Pekka Paalanen2abe2e62012-09-03 16:48:42 +03001296static void
1297weston_compositor_use_shader(struct weston_compositor *compositor,
1298 struct weston_shader *shader)
1299{
1300 if (compositor->current_shader == shader)
1301 return;
1302
1303 glUseProgram(shader->program);
1304 compositor->current_shader = shader;
1305}
1306
1307static void
1308weston_shader_uniforms(struct weston_shader *shader,
1309 struct weston_surface *surface,
1310 struct weston_output *output)
1311{
1312 int i;
1313
1314 glUniformMatrix4fv(shader->proj_uniform,
1315 1, GL_FALSE, output->matrix.d);
1316 glUniform4fv(shader->color_uniform, 1, surface->color);
1317 glUniform1f(shader->alpha_uniform, surface->alpha);
1318
1319 for (i = 0; i < surface->num_textures; i++)
1320 glUniform1i(shader->tex_uniforms[i], i);
1321}
Rob Clark0e5a2d02012-08-30 16:47:18 -05001322
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05001323WL_EXPORT void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001324weston_surface_draw(struct weston_surface *es, struct weston_output *output,
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001325 pixman_region32_t *damage) /* in global coordinates */
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001326{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001327 struct weston_compositor *ec = es->compositor;
Rob Clark0e5a2d02012-08-30 16:47:18 -05001328 /* repaint bounding region in global coordinates: */
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001329 pixman_region32_t repaint;
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001330 /* non-opaque region in surface coordinates: */
1331 pixman_region32_t surface_blend;
Kristian Høgsberg40caded2011-06-20 19:48:16 -04001332 GLint filter;
Rob Clark0e5a2d02012-08-30 16:47:18 -05001333 int i;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001334
Pekka Paalanena9f8a212012-01-26 11:28:08 +02001335 pixman_region32_init(&repaint);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001336 pixman_region32_intersect(&repaint,
1337 &es->transform.boundingbox, damage);
1338 pixman_region32_subtract(&repaint, &repaint, &es->clip);
Pekka Paalanenc7b45f62012-02-06 12:16:07 +02001339
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001340 if (!pixman_region32_not_empty(&repaint))
Pekka Paalanena9f8a212012-01-26 11:28:08 +02001341 goto out;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001342
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001343 pixman_region32_subtract(&ec->primary_plane.damage,
1344 &ec->primary_plane.damage, &repaint);
1345
Kristian Høgsberg101cb652012-02-17 10:45:16 -05001346 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001347
Pekka Paalanenc73bd542012-09-03 16:48:43 +03001348 if (ec->fan_debug) {
1349 weston_compositor_use_shader(ec, &ec->solid_shader);
1350 weston_shader_uniforms(&ec->solid_shader, es, output);
1351 }
1352
Pekka Paalanen2abe2e62012-09-03 16:48:42 +03001353 weston_compositor_use_shader(ec, es->shader);
1354 weston_shader_uniforms(es->shader, es, output);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001355
Scott Moreauccbf29d2012-02-22 14:21:41 -07001356 if (es->transform.enabled || output->zoom.active)
Kristian Høgsberg40caded2011-06-20 19:48:16 -04001357 filter = GL_LINEAR;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001358 else
Pekka Paalanenc61eca62012-01-06 14:10:06 +02001359 filter = GL_NEAREST;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001360
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02001361 for (i = 0; i < es->num_textures; i++) {
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02001362 glActiveTexture(GL_TEXTURE0 + i);
Rob Clarke3b95912012-08-31 16:42:18 -05001363 glBindTexture(es->target, es->textures[i]);
1364 glTexParameteri(es->target, GL_TEXTURE_MIN_FILTER, filter);
1365 glTexParameteri(es->target, GL_TEXTURE_MAG_FILTER, filter);
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02001366 }
Kristian Høgsberg40caded2011-06-20 19:48:16 -04001367
Pekka Paalanen2abe2e62012-09-03 16:48:42 +03001368 /* blended region is whole surface minus opaque region: */
1369 pixman_region32_init_rect(&surface_blend, 0, 0,
1370 es->geometry.width, es->geometry.height);
1371 pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
1372
1373 if (pixman_region32_not_empty(&es->opaque)) {
1374 if (es->shader == &ec->texture_shader_rgba) {
1375 /* Special case for RGBA textures with possibly
1376 * bad data in alpha channel: use the shader
1377 * that forces texture alpha = 1.0.
1378 * Xwayland surfaces need this.
1379 */
1380 weston_compositor_use_shader(ec, &ec->texture_shader_rgbx);
1381 weston_shader_uniforms(&ec->texture_shader_rgbx, es, output);
1382 }
1383
1384 if (es->alpha < 1.0)
1385 glEnable(GL_BLEND);
1386 else
1387 glDisable(GL_BLEND);
1388
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001389 repaint_region(es, &repaint, &es->opaque);
Rob Clark0e5a2d02012-08-30 16:47:18 -05001390 }
Pekka Paalanen3df327f2012-01-24 15:53:35 +02001391
Rob Clark0e5a2d02012-08-30 16:47:18 -05001392 if (pixman_region32_not_empty(&surface_blend)) {
Pekka Paalanen2abe2e62012-09-03 16:48:42 +03001393 weston_compositor_use_shader(ec, es->shader);
Rob Clark0e5a2d02012-08-30 16:47:18 -05001394 glEnable(GL_BLEND);
1395 repaint_region(es, &repaint, &surface_blend);
1396 }
Pekka Paalanena9f8a212012-01-26 11:28:08 +02001397
Pekka Paalanen2abe2e62012-09-03 16:48:42 +03001398 pixman_region32_fini(&surface_blend);
1399
Pekka Paalanena9f8a212012-01-26 11:28:08 +02001400out:
Kristian Høgsberg525e4c02011-02-14 10:39:54 -05001401 pixman_region32_fini(&repaint);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001402}
1403
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001404WL_EXPORT void
1405weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001406{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001407 wl_list_remove(&surface->layer_link);
1408 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001409 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001410 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001411}
1412
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001413WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001414weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001415{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001416 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001417
1418 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001419 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001420}
1421
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001422WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001423weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +02001424{
Kristian Høgsberg24596942011-10-24 17:51:02 -04001425 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +02001426 return;
Benjamin Franzke06286262011-05-06 19:12:33 +02001427
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001428 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -05001429 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +02001430}
1431
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001432WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001433weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001434{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001435 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001436
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001437 pixman_region32_union(&compositor->primary_plane.damage,
1438 &compositor->primary_plane.damage,
1439 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001440 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001441}
1442
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001443static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001444fade_frame(struct weston_animation *animation,
1445 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001446{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001447 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001448 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001449 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001450 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001451
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001452 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -06001453 compositor->fade.spring.timestamp = msecs;
1454
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001455 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001456 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001457 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
1458 compositor->fade.spring.current);
1459 weston_surface_damage(surface);
1460
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001461 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001462 compositor->fade.spring.current =
1463 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001464 wl_list_remove(&animation->link);
1465 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001466
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001467 if (compositor->fade.spring.current < 0.001) {
1468 destroy_surface(&surface->surface.resource);
1469 compositor->fade.surface = NULL;
1470 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001471 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001472 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001473 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001474 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001475}
1476
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001477static void
1478update_shm_texture(struct weston_surface *surface)
1479{
1480#ifdef GL_UNPACK_ROW_LENGTH
1481 pixman_box32_t *rectangles;
1482 void *data;
1483 int i, n;
1484#endif
1485
Gwenole Beauchesne023f8552012-04-20 11:07:06 +02001486 glBindTexture(GL_TEXTURE_2D, surface->textures[0]);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001487
1488 if (!surface->compositor->has_unpack_subimage) {
1489 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1490 surface->pitch, surface->buffer->height, 0,
1491 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1492 wl_shm_buffer_get_data(surface->buffer));
1493
1494 return;
1495 }
1496
1497#ifdef GL_UNPACK_ROW_LENGTH
1498 /* Mesa does not define GL_EXT_unpack_subimage */
1499 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch);
1500 data = wl_shm_buffer_get_data(surface->buffer);
1501 rectangles = pixman_region32_rectangles(&surface->damage, &n);
1502 for (i = 0; i < n; i++) {
1503 glPixelStorei(GL_UNPACK_SKIP_PIXELS, rectangles[i].x1);
1504 glPixelStorei(GL_UNPACK_SKIP_ROWS, rectangles[i].y1);
1505 glTexSubImage2D(GL_TEXTURE_2D, 0,
1506 rectangles[i].x1, rectangles[i].y1,
Rob Bradford8f241562012-07-02 17:33:40 +01001507 rectangles[i].x2 - rectangles[i].x1,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001508 rectangles[i].y2 - rectangles[i].y1,
1509 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
1510 }
1511#endif
1512}
1513
1514static void
1515surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001516 pixman_region32_t *opaque)
1517{
1518 if (surface->buffer && wl_buffer_is_shm(surface->buffer))
1519 update_shm_texture(surface);
1520
1521 if (surface->transform.enabled) {
1522 pixman_box32_t *extents;
1523
1524 extents = pixman_region32_extents(&surface->damage);
1525 surface_compute_bbox(surface, extents->x1, extents->y1,
1526 extents->x2 - extents->x1,
1527 extents->y2 - extents->y1,
1528 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001529 pixman_region32_translate(&surface->damage,
1530 -surface->plane->x,
1531 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001532 } else {
1533 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001534 surface->geometry.x - surface->plane->x,
1535 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001536 }
1537
1538 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001539 pixman_region32_union(&surface->plane->damage,
1540 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001541 empty_region(&surface->damage);
1542 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001543 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001544}
1545
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001546static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001547weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001548{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001549 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001550 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001551 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001552 struct weston_animation *animation, *next;
1553 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001554 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001555 pixman_region32_t opaque, output_damage, new_damage;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001556 int32_t width, height;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001557
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001558 weston_compositor_update_drag_surfaces(ec);
1559
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001560 width = output->current->width +
1561 output->border.left + output->border.right;
1562 height = output->current->height +
1563 output->border.top + output->border.bottom;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001564
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001565 glViewport(0, 0, width, height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -05001566
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001567 /* Rebuild the surface list and update surface transforms up front. */
1568 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +02001569 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001570 wl_list_for_each(layer, &ec->layer_list, link) {
1571 wl_list_for_each(es, &layer->surface_list, layer_link) {
1572 weston_surface_update_transform(es);
1573 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +02001574 if (es->output == output) {
1575 wl_list_insert_list(&frame_callback_list,
1576 &es->frame_callback_list);
1577 wl_list_init(&es->frame_callback_list);
1578 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001579 }
1580 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001581
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001582 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001583 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001584 else
1585 wl_list_for_each(es, &ec->surface_list, link)
1586 weston_surface_move_to_plane(es, &ec->primary_plane);
1587
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001588 pixman_region32_init(&opaque);
1589
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001590 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001591 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001592
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -04001593 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001594
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001595 pixman_region32_init(&output_damage);
1596
1597 pixman_region32_init(&new_damage);
1598 pixman_region32_copy(&new_damage, &ec->primary_plane.damage);
1599
1600 pixman_region32_union(&ec->primary_plane.damage,
1601 &ec->primary_plane.damage,
1602 &output->previous_damage);
1603
1604 pixman_region32_intersect(&output->previous_damage,
1605 &new_damage, &output->region);
1606
1607 pixman_region32_intersect(&output_damage,
1608 &ec->primary_plane.damage, &output->region);
1609
1610 pixman_region32_fini(&new_damage);
1611
Scott Moreauccbf29d2012-02-22 14:21:41 -07001612 if (output->dirty)
1613 weston_output_update_matrix(output);
1614
Pekka Paalanen07c91f82012-08-30 16:47:21 -05001615 /* if debugging, redraw everything outside the damage to clean up
1616 * debug lines from the previous draw on this buffer:
1617 */
1618 if (ec->fan_debug) {
1619 pixman_region32_t undamaged;
1620 pixman_region32_init(&undamaged);
1621 pixman_region32_subtract(&undamaged, &output->region,
1622 &output_damage);
1623 ec->fan_debug = 0;
1624 output->repaint(output, &undamaged, 0);
1625 ec->fan_debug = 1;
1626 pixman_region32_fini(&undamaged);
1627 }
1628
1629 output->repaint(output, &output_damage, 1);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001630
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001631 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001632
Kristian Høgsbergef044142011-06-21 15:02:12 -04001633 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001634
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001635 weston_compositor_repick(ec);
1636 wl_event_loop_dispatch(ec->input_loop, 0);
1637
Jonas Ådahldb773762012-06-13 00:01:21 +02001638 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001639 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001640 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001641 }
Jonas Ådahldb773762012-06-13 00:01:21 +02001642 wl_list_init(&frame_callback_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001643
Scott Moreaud64cf212012-06-08 19:40:54 -06001644 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001645 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001646 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001647 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001648}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001649
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001650static int
1651weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001652{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001653 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001654
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001655 wl_event_loop_dispatch(compositor->input_loop, 0);
1656
1657 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001658}
1659
1660WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001661weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001662{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001663 struct weston_compositor *compositor = output->compositor;
1664 struct wl_event_loop *loop =
1665 wl_display_get_event_loop(compositor->wl_display);
1666 int fd;
1667
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001668 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001669 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001670 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001671 return;
1672 }
1673
1674 output->repaint_scheduled = 0;
1675 if (compositor->input_loop_source)
1676 return;
1677
1678 fd = wl_event_loop_get_fd(compositor->input_loop);
1679 compositor->input_loop_source =
1680 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1681 weston_compositor_read_input, compositor);
1682}
1683
1684static void
1685idle_repaint(void *data)
1686{
1687 struct weston_output *output = data;
1688
1689 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001690}
1691
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001692WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001693weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1694{
1695 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001696 if (below != NULL)
1697 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001698}
1699
1700WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001701weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001702{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001703 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001704 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001705
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001706 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001707 return;
1708
Kristian Høgsbergef044142011-06-21 15:02:12 -04001709 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001710 output->repaint_needed = 1;
1711 if (output->repaint_scheduled)
1712 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001713
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001714 wl_event_loop_add_idle(loop, idle_repaint, output);
1715 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001716
1717 if (compositor->input_loop_source) {
1718 wl_event_source_remove(compositor->input_loop_source);
1719 compositor->input_loop_source = NULL;
1720 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001721}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001722
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001723WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001724weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1725{
1726 struct weston_output *output;
1727
1728 wl_list_for_each(output, &compositor->output_list, link)
1729 weston_output_schedule_repaint(output);
1730}
1731
1732WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001733weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001734{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001735 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001736 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001737
Scott Moreau9d1b1122012-06-08 19:40:53 -06001738 output = container_of(compositor->output_list.next,
1739 struct weston_output, link);
1740
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001741 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001742 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001743 return;
1744
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001745 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001746 surface = weston_surface_create(compositor);
1747 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001748 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001749 wl_list_insert(&compositor->fade_layer.surface_list,
1750 &surface->layer_link);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001751 weston_surface_assign_output(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001752 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001753 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001754 }
1755
1756 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001757 if (wl_list_empty(&compositor->fade.animation.link)) {
1758 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001759 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001760 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001761 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001762}
1763
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001764static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001765surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001766{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001767 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001768}
1769
Casey Dahlin96d8a752012-04-19 22:50:07 -04001770static struct wl_resource *
1771find_resource_for_client(struct wl_list *list, struct wl_client *client)
1772{
1773 struct wl_resource *r;
1774
1775 wl_list_for_each(r, list, link) {
1776 if (r->client == client)
1777 return r;
1778 }
1779
1780 return NULL;
1781}
1782
Casey Dahlin9074db52012-04-19 22:50:09 -04001783static void
1784weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1785{
1786 uint32_t different = es->output_mask ^ mask;
1787 uint32_t entered = mask & different;
1788 uint32_t left = es->output_mask & different;
1789 struct weston_output *output;
Rob Bradford8667e772012-05-18 14:13:03 +01001790 struct wl_resource *resource = NULL;
Casey Dahlin9074db52012-04-19 22:50:09 -04001791 struct wl_client *client = es->surface.resource.client;
1792
Scott Moreaua5021522012-08-03 17:11:51 -06001793 es->output_mask = mask;
Casey Dahlin9074db52012-04-19 22:50:09 -04001794 if (es->surface.resource.client == NULL)
1795 return;
1796 if (different == 0)
1797 return;
1798
Casey Dahlin9074db52012-04-19 22:50:09 -04001799 wl_list_for_each(output, &es->compositor->output_list, link) {
1800 if (1 << output->id & different)
1801 resource =
1802 find_resource_for_client(&output->resource_list,
1803 client);
Kristian Høgsbergc7814d22012-07-12 12:34:43 -04001804 if (resource == NULL)
1805 continue;
Casey Dahlin9074db52012-04-19 22:50:09 -04001806 if (1 << output->id & entered)
1807 wl_surface_send_enter(&es->surface.resource, resource);
1808 if (1 << output->id & left)
1809 wl_surface_send_leave(&es->surface.resource, resource);
1810 }
1811}
1812
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001813WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001814weston_surface_assign_output(struct weston_surface *es)
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001815{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001816 struct weston_compositor *ec = es->compositor;
1817 struct weston_output *output, *new_output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001818 pixman_region32_t region;
Casey Dahlin9074db52012-04-19 22:50:09 -04001819 uint32_t max, area, mask;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001820 pixman_box32_t *e;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001821
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001822 new_output = NULL;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001823 max = 0;
Casey Dahlin9074db52012-04-19 22:50:09 -04001824 mask = 0;
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001825 pixman_region32_init(&region);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001826 wl_list_for_each(output, &ec->output_list, link) {
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001827 pixman_region32_intersect(&region, &es->transform.boundingbox,
1828 &output->region);
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001829
1830 e = pixman_region32_extents(&region);
1831 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1832
Casey Dahlin9074db52012-04-19 22:50:09 -04001833 if (area > 0)
1834 mask |= 1 << output->id;
1835
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001836 if (area >= max) {
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001837 new_output = output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001838 max = area;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001839 }
1840 }
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001841 pixman_region32_fini(&region);
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001842
1843 es->output = new_output;
Casey Dahlin9074db52012-04-19 22:50:09 -04001844 weston_surface_update_output_mask(es, mask);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001845}
1846
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001847static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001848surface_attach(struct wl_client *client,
1849 struct wl_resource *resource,
1850 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1851{
1852 struct weston_surface *es = resource->data;
1853 struct wl_buffer *buffer = NULL;
1854
1855 if (buffer_resource)
1856 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001857
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -04001858 weston_surface_attach(&es->surface, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001859
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001860 if (buffer && es->configure)
1861 es->configure(es, sx, sy);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001862}
1863
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001864static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001865surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001866 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001867 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001868{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001869 struct weston_surface *es = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001870
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001871 pixman_region32_union_rect(&es->damage, &es->damage,
1872 x, y, width, height);
Kristian Høgsberg98238702012-08-03 16:29:12 -04001873 weston_surface_schedule_repaint(es);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001874}
1875
Kristian Høgsberg33418202011-08-16 23:01:28 -04001876static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001877destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001878{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001879 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001880
1881 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001882 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001883}
1884
1885static void
1886surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001887 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001888{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001889 struct weston_frame_callback *cb;
1890 struct weston_surface *es = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001891
1892 cb = malloc(sizeof *cb);
1893 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001894 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001895 return;
1896 }
1897
1898 cb->resource.object.interface = &wl_callback_interface;
1899 cb->resource.object.id = callback;
1900 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001901 cb->resource.client = client;
1902 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001903
1904 wl_client_add_resource(client, &cb->resource);
Jonas Ådahldb773762012-06-13 00:01:21 +02001905 wl_list_insert(es->frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001906}
1907
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001908static void
1909surface_set_opaque_region(struct wl_client *client,
1910 struct wl_resource *resource,
1911 struct wl_resource *region_resource)
1912{
1913 struct weston_surface *surface = resource->data;
1914 struct weston_region *region;
1915
1916 pixman_region32_fini(&surface->opaque);
1917
1918 if (region_resource) {
1919 region = region_resource->data;
1920 pixman_region32_init_rect(&surface->opaque, 0, 0,
1921 surface->geometry.width,
1922 surface->geometry.height);
1923 pixman_region32_intersect(&surface->opaque,
1924 &surface->opaque, &region->region);
1925 } else {
1926 pixman_region32_init(&surface->opaque);
1927 }
1928
1929 surface->geometry.dirty = 1;
1930}
1931
1932static void
1933surface_set_input_region(struct wl_client *client,
1934 struct wl_resource *resource,
1935 struct wl_resource *region_resource)
1936{
1937 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001938 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001939
1940 if (region_resource) {
1941 region = region_resource->data;
1942 pixman_region32_init_rect(&surface->input, 0, 0,
1943 surface->geometry.width,
1944 surface->geometry.height);
1945 pixman_region32_intersect(&surface->input,
1946 &surface->input, &region->region);
1947 } else {
1948 pixman_region32_init_rect(&surface->input, 0, 0,
1949 surface->geometry.width,
1950 surface->geometry.height);
1951 }
1952
Kristian Høgsberg98238702012-08-03 16:29:12 -04001953 weston_surface_schedule_repaint(surface);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001954}
1955
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001956static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001957 surface_destroy,
1958 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001959 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001960 surface_frame,
1961 surface_set_opaque_region,
1962 surface_set_input_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001963};
1964
1965static void
1966compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001967 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001968{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001969 struct weston_compositor *ec = resource->data;
1970 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001971
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001972 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001973 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001974 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001975 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001976 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001977
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001978 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001979
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001980 surface->surface.resource.object.id = id;
1981 surface->surface.resource.object.interface = &wl_surface_interface;
1982 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001983 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001984 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001985
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001986 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001987}
1988
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001989static void
1990destroy_region(struct wl_resource *resource)
1991{
1992 struct weston_region *region =
1993 container_of(resource, struct weston_region, resource);
1994
1995 pixman_region32_fini(&region->region);
1996 free(region);
1997}
1998
1999static void
2000region_destroy(struct wl_client *client, struct wl_resource *resource)
2001{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002002 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002003}
2004
2005static void
2006region_add(struct wl_client *client, struct wl_resource *resource,
2007 int32_t x, int32_t y, int32_t width, int32_t height)
2008{
2009 struct weston_region *region = resource->data;
2010
2011 pixman_region32_union_rect(&region->region, &region->region,
2012 x, y, width, height);
2013}
2014
2015static void
2016region_subtract(struct wl_client *client, struct wl_resource *resource,
2017 int32_t x, int32_t y, int32_t width, int32_t height)
2018{
2019 struct weston_region *region = resource->data;
2020 pixman_region32_t rect;
2021
2022 pixman_region32_init_rect(&rect, x, y, width, height);
2023 pixman_region32_subtract(&region->region, &region->region, &rect);
2024 pixman_region32_fini(&rect);
2025}
2026
2027static const struct wl_region_interface region_interface = {
2028 region_destroy,
2029 region_add,
2030 region_subtract
2031};
2032
2033static void
2034compositor_create_region(struct wl_client *client,
2035 struct wl_resource *resource, uint32_t id)
2036{
2037 struct weston_region *region;
2038
2039 region = malloc(sizeof *region);
2040 if (region == NULL) {
2041 wl_resource_post_no_memory(resource);
2042 return;
2043 }
2044
2045 region->resource.destroy = destroy_region;
2046
2047 region->resource.object.id = id;
2048 region->resource.object.interface = &wl_region_interface;
2049 region->resource.object.implementation =
2050 (void (**)(void)) &region_interface;
2051 region->resource.data = region;
2052
2053 pixman_region32_init(&region->region);
2054
2055 wl_client_add_resource(client, &region->resource);
2056}
2057
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002058static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002059 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002060 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002061};
2062
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002063WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002064weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002065{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002066 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2067 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002068
2069 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02002070 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002071}
2072
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002073static void
2074weston_compositor_dpms_on(struct weston_compositor *compositor)
2075{
2076 struct weston_output *output;
2077
2078 wl_list_for_each(output, &compositor->output_list, link)
2079 if (output->set_dpms)
2080 output->set_dpms(output, WESTON_DPMS_ON);
2081}
2082
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002083WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002084weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002085{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002086 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
2087 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002088 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002089 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002090 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002091 }
2092}
2093
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002094static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002095weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002096{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002097 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002098 compositor->idle_inhibit++;
2099}
2100
2101static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002102weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002103{
2104 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002105 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002106}
2107
2108static int
2109idle_handler(void *data)
2110{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002111 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002112
2113 if (compositor->idle_inhibit)
2114 return 1;
2115
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002116 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002117
2118 return 1;
2119}
2120
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002121WL_EXPORT void
2122weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2123{
2124 pixman_region32_init(&plane->damage);
2125 plane->x = x;
2126 plane->y = y;
2127}
2128
2129WL_EXPORT void
2130weston_plane_release(struct weston_plane *plane)
2131{
2132 pixman_region32_fini(&plane->damage);
2133}
2134
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002135static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002136weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002137
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002138static void
Daniel Stone37816df2012-05-16 18:45:18 +01002139clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002140{
Daniel Stone37816df2012-05-16 18:45:18 +01002141 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002142 struct weston_output *output, *prev = NULL;
2143 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002144
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002145 x = wl_fixed_to_int(*fx);
2146 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01002147 old_x = wl_fixed_to_int(seat->seat.pointer->x);
2148 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002149
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002150 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002151 if (pixman_region32_contains_point(&output->region,
2152 x, y, NULL))
2153 valid = 1;
2154 if (pixman_region32_contains_point(&output->region,
2155 old_x, old_y, NULL))
2156 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002157 }
Daniel Stone37816df2012-05-16 18:45:18 +01002158
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002159 if (!valid) {
2160 if (x < prev->x)
2161 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06002162 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002163 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06002164 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002165 if (y < prev->y)
2166 *fy = wl_fixed_from_int(prev->y);
2167 else if (y >= prev->y + prev->current->height)
2168 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06002169 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002170 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002171}
2172
2173WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002174notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002175{
2176 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002177 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002178 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002179 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002180 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002181
2182 weston_compositor_activity(ec);
2183
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002184 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002185
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002186 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002187
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002188 pointer->x = x;
2189 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002190
2191 ix = wl_fixed_to_int(x);
2192 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002193
Scott Moreauccbf29d2012-02-22 14:21:41 -07002194 wl_list_for_each(output, &ec->output_list, link)
2195 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002196 pixman_region32_contains_point(&output->region,
2197 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06002198 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002199
Daniel Stone37816df2012-05-16 18:45:18 +01002200 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002201 interface = pointer->grab->interface;
2202 interface->motion(pointer->grab, time,
2203 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002204
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002205 if (seat->sprite) {
2206 weston_surface_set_position(seat->sprite,
2207 ix - seat->hotspot_x,
2208 iy - seat->hotspot_y);
2209 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002210 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002211}
2212
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002213WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002214weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002215 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05002216{
Daniel Stone37816df2012-05-16 18:45:18 +01002217 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002218
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03002219 if (seat->seat.keyboard) {
2220 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
2221 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04002222 }
2223
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002224 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05002225}
2226
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002227WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002228notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002229 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05002230{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002231 struct weston_compositor *compositor = seat->compositor;
2232 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01002233 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002234 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002235 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05002236
Daniel Stone4dbadb12012-05-30 16:31:51 +01002237 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002238 if (compositor->ping_handler && focus)
2239 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002240 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002241 if (pointer->button_count == 0) {
2242 pointer->grab_button = button;
2243 pointer->grab_time = time;
2244 pointer->grab_x = pointer->x;
2245 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002246 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002247 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002248 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002249 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002250 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002251 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002252
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002253 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002254 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05002255
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002256 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05002257
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002258 if (pointer->button_count == 1)
2259 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002260 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05002261}
2262
Scott Moreau210d0792012-03-22 10:47:01 -06002263WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002264notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01002265 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002266{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002267 struct weston_compositor *compositor = seat->compositor;
2268 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01002269 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002270 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002271 uint32_t serial = wl_display_next_serial(compositor->wl_display);
2272
2273 if (compositor->ping_handler && focus)
2274 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06002275
2276 weston_compositor_activity(compositor);
2277
Scott Moreau6a3633d2012-03-20 08:47:59 -06002278 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002279 weston_compositor_run_axis_binding(compositor, seat,
2280 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06002281 else
2282 return;
2283
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002284 if (pointer->focus_resource)
2285 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01002286 value);
Scott Moreau210d0792012-03-22 10:47:01 -06002287}
2288
Daniel Stone05d58682012-06-22 13:21:38 +01002289WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002290notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002291{
Daniel Stone05d58682012-06-22 13:21:38 +01002292 struct wl_keyboard *keyboard = &seat->keyboard;
2293 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01002294 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002295 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01002296 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01002297 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01002298
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002299 /* Serialize and update our internal state, checking to see if it's
2300 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01002301 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
2302 XKB_STATE_DEPRESSED);
2303 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
2304 XKB_STATE_LATCHED);
2305 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
2306 XKB_STATE_LOCKED);
2307 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01002308 XKB_STATE_EFFECTIVE);
2309
Daniel Stone71c38772012-06-22 13:21:30 +01002310 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
2311 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
2312 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
2313 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01002314 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01002315
Daniel Stone71c38772012-06-22 13:21:30 +01002316 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
2317 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
2318 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
2319 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05002320
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002321 /* And update the modifier_state for bindings. */
2322 mods_lookup = mods_depressed | mods_latched;
2323 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01002324 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002325 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01002326 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002327 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01002328 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002329 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04002330 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
2331 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01002332
Daniel Stone8c8164f2012-05-30 16:31:45 +01002333 /* Finally, notify the compositor that LEDs have changed. */
2334 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01002335 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01002336 leds |= LED_NUM_LOCK;
2337 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01002338 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01002339 leds |= LED_CAPS_LOCK;
2340 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01002341 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01002342 leds |= LED_SCROLL_LOCK;
2343 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01002344 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01002345 seat->xkb_state.leds = leds;
2346
Daniel Stone05d58682012-06-22 13:21:38 +01002347 if (changed) {
2348 grab->interface->modifiers(grab,
2349 serial,
2350 keyboard->modifiers.mods_depressed,
2351 keyboard->modifiers.mods_latched,
2352 keyboard->modifiers.mods_locked,
2353 keyboard->modifiers.group);
2354 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002355}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04002356
Daniel Stone05d58682012-06-22 13:21:38 +01002357static void
2358update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002359 enum wl_keyboard_key_state state)
2360{
2361 enum xkb_key_direction direction;
2362
2363 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2364 direction = XKB_KEY_DOWN;
2365 else
2366 direction = XKB_KEY_UP;
2367
2368 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
2369 * broken keycode system, which starts at 8. */
2370 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
2371
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002372 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002373}
2374
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002375WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002376notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01002377 enum wl_keyboard_key_state state,
2378 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002379{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002380 struct weston_compositor *compositor = seat->compositor;
2381 struct wl_keyboard *keyboard = seat->seat.keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01002382 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002383 (struct weston_surface *) keyboard->focus;
2384 struct wl_keyboard_grab *grab = keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002385 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002386 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002387
Daniel Stonec9785ea2012-05-30 16:31:52 +01002388 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002389 if (compositor->ping_handler && focus)
2390 compositor->ping_handler(focus, serial);
2391
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002392 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002393 keyboard->grab_key = key;
2394 keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07002395 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002396 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07002397 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002398
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002399 end = keyboard->keys.data + keyboard->keys.size;
2400 for (k = keyboard->keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01002401 if (*k == key) {
2402 /* Ignore server-generated repeats. */
2403 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2404 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002405 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01002406 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002407 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002408 keyboard->keys.size = (void *) end - keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002409 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002410 k = wl_array_add(&keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002411 *k = key;
2412 }
Ray Strodee96dcb82008-12-20 02:00:49 -05002413
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002414 if (grab == &keyboard->default_grab) {
2415 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002416 state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002417 grab = keyboard->grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06002418 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05002419
Daniel Stone7ace3902012-05-30 16:31:40 +01002420 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01002421
2422 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002423 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01002424 wl_display_get_serial(compositor->wl_display),
2425 key,
2426 state);
2427 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002428}
2429
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002430WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002431notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01002432 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002433{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002434 struct weston_compositor *compositor = seat->compositor;
2435 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002436
2437 if (output) {
Daniel Stone37816df2012-05-16 18:45:18 +01002438 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002439 x - pointer->x,
2440 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002441
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002442 pointer->x = x;
2443 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002444 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002445 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002446 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002447 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03002448 /* FIXME: We should call wl_pointer_set_focus(seat,
2449 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002450 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002451}
2452
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002453static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002454destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002455{
Daniel Stone37816df2012-05-16 18:45:18 +01002456 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002457
Daniel Stone37816df2012-05-16 18:45:18 +01002458 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002459 saved_kbd_focus_listener);
2460
Daniel Stone37816df2012-05-16 18:45:18 +01002461 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002462}
2463
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002464WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002465notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002466 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002467{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002468 struct weston_compositor *compositor = seat->compositor;
2469 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04002470 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002471 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05002472
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002473 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002474 wl_array_copy(&keyboard->keys, keys);
2475 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002476 weston_compositor_idle_inhibit(compositor);
2477 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002478 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002479 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002480 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002481
Daniel Stoneef172672012-06-22 13:21:32 +01002482 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002483 wl_array_for_each(k, &keyboard->keys) {
2484 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01002485 WL_KEYBOARD_KEY_STATE_PRESSED);
2486 }
2487
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002488 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002489
2490 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002491 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2492 wl_keyboard_set_focus(keyboard, surface);
2493 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002494 }
2495}
2496
2497WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002498notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01002499{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002500 struct weston_compositor *compositor = seat->compositor;
2501 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002502 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002503
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002504 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002505 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002506 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002507 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002508 WL_KEYBOARD_KEY_STATE_RELEASED);
2509 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002510
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002511 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002512
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002513 if (keyboard->focus) {
2514 seat->saved_kbd_focus = keyboard->focus;
2515 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01002516 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002517 wl_signal_add(&keyboard->focus->resource.destroy_signal,
2518 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002519 }
2520
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002521 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002522 /* FIXME: We really need keyboard grab cancel here to
2523 * let the grab shut down properly. As it is we leak
2524 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002525 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002526}
2527
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002528static void
Daniel Stone37816df2012-05-16 18:45:18 +01002529touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002530{
Daniel Stone37816df2012-05-16 18:45:18 +01002531 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002532 struct wl_resource *resource;
2533
Pekka Paalanen56464252012-07-31 13:21:08 +03002534 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002535 return;
2536
Pekka Paalanen56464252012-07-31 13:21:08 +03002537 if (seat->touch->focus_resource)
2538 wl_list_remove(&seat->touch->focus_listener.link);
2539 seat->touch->focus = NULL;
2540 seat->touch->focus_resource = NULL;
2541
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002542 if (surface) {
2543 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01002544 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04002545 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002546 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02002547 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002548 return;
2549 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002550
Daniel Stone37816df2012-05-16 18:45:18 +01002551 seat->touch->focus = surface;
2552 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03002553 wl_signal_add(&resource->destroy_signal,
2554 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002555 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002556}
2557
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002558/**
2559 * notify_touch - emulates button touches and notifies surfaces accordingly.
2560 *
2561 * It assumes always the correct cycle sequence until it gets here: touch_down
2562 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2563 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002564 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002565 */
2566WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002567notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002568 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002569{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002570 struct weston_compositor *ec = seat->compositor;
2571 struct wl_touch *touch = seat->seat.touch;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002572 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002573 wl_fixed_t sx, sy;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002574 uint32_t serial = 0;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002575
2576 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01002577 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002578 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002579
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002580 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002581
2582 /* the first finger down picks the surface, and all further go
2583 * to that surface for the remainder of the touch session i.e.
2584 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002585 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002586 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002587 touch_set_focus(seat, &es->surface);
2588 } else if (touch->focus) {
2589 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002590 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002591 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002592
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002593 if (touch->focus_resource && touch->focus)
2594 wl_touch_send_down(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002595 serial, time,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002596 &touch->focus->resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002597 touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002598 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002599 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002600 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002601 if (!es)
2602 break;
2603
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002604 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002605 if (touch->focus_resource)
2606 wl_touch_send_motion(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002607 time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002608 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002609 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002610 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002611 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002612
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002613 if (touch->focus_resource)
2614 wl_touch_send_up(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002615 serial, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002616 if (seat->num_tp == 0)
2617 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002618 break;
2619 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002620}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002621
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04002622static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002623pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
2624{
2625 struct weston_seat *seat = container_of(listener, struct weston_seat,
2626 sprite_destroy_listener);
2627
2628 seat->sprite = NULL;
2629}
2630
2631static void
2632pointer_cursor_surface_configure(struct weston_surface *es,
2633 int32_t dx, int32_t dy)
2634{
2635 struct weston_seat *seat = es->private;
2636 int x, y;
2637
2638 assert(es == seat->sprite);
2639
2640 seat->hotspot_x -= dx;
2641 seat->hotspot_y -= dy;
2642
2643 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
2644 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
2645
2646 weston_surface_configure(seat->sprite, x, y,
2647 es->buffer->width, es->buffer->height);
2648
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03002649 empty_region(&es->input);
2650
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002651 if (!weston_surface_is_mapped(es)) {
2652 wl_list_insert(&es->compositor->cursor_layer.surface_list,
2653 &es->layer_link);
2654 weston_surface_assign_output(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002655 }
2656}
2657
2658static void
2659pointer_unmap_sprite(struct weston_seat *seat)
2660{
2661 if (weston_surface_is_mapped(seat->sprite))
2662 weston_surface_unmap(seat->sprite);
2663
2664 wl_list_remove(&seat->sprite_destroy_listener.link);
2665 seat->sprite->configure = NULL;
2666 seat->sprite->private = NULL;
2667 seat->sprite = NULL;
2668}
2669
2670static void
2671pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2672 uint32_t serial, struct wl_resource *surface_resource,
2673 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002674{
Daniel Stone37816df2012-05-16 18:45:18 +01002675 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002676 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002677
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002678 if (surface_resource)
2679 surface = container_of(surface_resource->data,
2680 struct weston_surface, surface);
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002681
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04002682 if (seat->seat.pointer->focus == NULL)
2683 return;
2684 if (seat->seat.pointer->focus->resource.client != client)
2685 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002686 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002687 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002688
2689 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002690 if (surface->configure) {
2691 wl_resource_post_error(&surface->surface.resource,
2692 WL_DISPLAY_ERROR_INVALID_OBJECT,
2693 "surface->configure already "
2694 "set");
2695 return;
2696 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002697 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002698
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002699 if (seat->sprite)
2700 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002701
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002702 if (!surface)
2703 return;
2704
2705 wl_signal_add(&surface->surface.resource.destroy_signal,
2706 &seat->sprite_destroy_listener);
2707
2708 surface->configure = pointer_cursor_surface_configure;
2709 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002710 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002711 seat->hotspot_x = x;
2712 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002713
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04002714 if (surface->buffer)
2715 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002716}
2717
Daniel Stone37816df2012-05-16 18:45:18 +01002718static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002719 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002720};
2721
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002722static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002723handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002724{
Daniel Stone37816df2012-05-16 18:45:18 +01002725 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002726
Daniel Stone37816df2012-05-16 18:45:18 +01002727 seat = container_of(listener, struct weston_seat,
2728 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002729
Daniel Stone37816df2012-05-16 18:45:18 +01002730 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002731}
2732
Casey Dahlin9074db52012-04-19 22:50:09 -04002733static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002734{
2735 wl_list_remove(&resource->link);
2736 free(resource);
2737}
2738
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002739static void
Daniel Stone37816df2012-05-16 18:45:18 +01002740seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2741 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002742{
Daniel Stone37816df2012-05-16 18:45:18 +01002743 struct weston_seat *seat = resource->data;
2744 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002745
Daniel Stone37816df2012-05-16 18:45:18 +01002746 if (!seat->seat.pointer)
2747 return;
2748
2749 cr = wl_client_add_object(client, &wl_pointer_interface,
2750 &pointer_interface, id, seat);
2751 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002752 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002753
2754 if (seat->seat.pointer->focus &&
2755 seat->seat.pointer->focus->resource.client == client) {
2756 struct weston_surface *surface;
2757 wl_fixed_t sx, sy;
2758
2759 surface = (struct weston_surface *) seat->seat.pointer->focus;
2760 weston_surface_from_global_fixed(surface,
2761 seat->seat.pointer->x,
2762 seat->seat.pointer->y,
2763 &sx,
2764 &sy);
2765 wl_pointer_set_focus(seat->seat.pointer,
2766 seat->seat.pointer->focus,
2767 sx,
2768 sy);
2769 }
Daniel Stone37816df2012-05-16 18:45:18 +01002770}
2771
2772static void
2773seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2774 uint32_t id)
2775{
2776 struct weston_seat *seat = resource->data;
2777 struct wl_resource *cr;
2778
2779 if (!seat->seat.keyboard)
2780 return;
2781
2782 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2783 seat);
2784 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002785 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002786
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002787 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2788 seat->xkb_info.keymap_fd,
2789 seat->xkb_info.keymap_size);
2790
Daniel Stone17b13bd2012-05-30 16:31:39 +01002791 if (seat->seat.keyboard->focus &&
2792 seat->seat.keyboard->focus->resource.client == client) {
2793 wl_keyboard_set_focus(seat->seat.keyboard,
2794 seat->seat.keyboard->focus);
2795 }
Daniel Stone37816df2012-05-16 18:45:18 +01002796}
2797
2798static void
2799seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2800 uint32_t id)
2801{
2802 struct weston_seat *seat = resource->data;
2803 struct wl_resource *cr;
2804
2805 if (!seat->seat.touch)
2806 return;
2807
2808 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2809 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002810 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002811}
2812
2813static const struct wl_seat_interface seat_interface = {
2814 seat_get_pointer,
2815 seat_get_keyboard,
2816 seat_get_touch,
2817};
2818
2819static void
2820bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2821{
2822 struct wl_seat *seat = data;
2823 struct wl_resource *resource;
2824 enum wl_seat_capability caps = 0;
2825
2826 resource = wl_client_add_object(client, &wl_seat_interface,
2827 &seat_interface, id, data);
2828 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002829 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002830
2831 if (seat->pointer)
2832 caps |= WL_SEAT_CAPABILITY_POINTER;
2833 if (seat->keyboard)
2834 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2835 if (seat->touch)
2836 caps |= WL_SEAT_CAPABILITY_TOUCH;
2837
2838 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002839}
2840
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002841static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002842device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002843{
Daniel Stone37816df2012-05-16 18:45:18 +01002844 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002845
Daniel Stone37816df2012-05-16 18:45:18 +01002846 seat = container_of(listener, struct weston_seat,
2847 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002848
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002849 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002850}
2851
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002852static void weston_compositor_xkb_init(struct weston_compositor *ec,
2853 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002854{
Daniel Stonee379da92012-05-30 16:32:04 +01002855 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002856 ec->xkb_context = xkb_context_new(0);
2857 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002858 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002859 exit(1);
2860 }
Daniel Stone994679a2012-05-30 16:31:43 +01002861 }
2862
2863 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002864 ec->xkb_names = *names;
2865 if (!ec->xkb_names.rules)
2866 ec->xkb_names.rules = strdup("evdev");
2867 if (!ec->xkb_names.model)
2868 ec->xkb_names.model = strdup("pc105");
2869 if (!ec->xkb_names.layout)
2870 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002871}
2872
Daniel Stonee379da92012-05-30 16:32:04 +01002873static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2874{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002875 if (xkb_info->keymap)
2876 xkb_map_unref(xkb_info->keymap);
2877
2878 if (xkb_info->keymap_area)
2879 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2880 if (xkb_info->keymap_fd >= 0)
2881 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002882}
2883
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002884static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2885{
Daniel Stonee379da92012-05-30 16:32:04 +01002886 free((char *) ec->xkb_names.rules);
2887 free((char *) ec->xkb_names.model);
2888 free((char *) ec->xkb_names.layout);
2889 free((char *) ec->xkb_names.variant);
2890 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002891
Daniel Stonee379da92012-05-30 16:32:04 +01002892 xkb_info_destroy(&ec->xkb_info);
2893 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002894}
2895
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002896static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002897weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002898{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002899 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002900
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002901 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2902 XKB_MOD_NAME_SHIFT);
2903 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2904 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002905 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2906 XKB_MOD_NAME_CTRL);
2907 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2908 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002909 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2910 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002911 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2912 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002913 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002914
2915 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2916 XKB_LED_NAME_NUM);
2917 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2918 XKB_LED_NAME_CAPS);
2919 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2920 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002921
2922 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2923 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002924 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002925 exit(EXIT_FAILURE);
2926 }
2927 xkb_info->keymap_size = strlen(keymap_str) + 1;
2928
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002929 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002930 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002931 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002932 (unsigned long) xkb_info->keymap_size);
2933 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002934 }
2935
2936 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2937 PROT_READ | PROT_WRITE,
2938 MAP_SHARED, xkb_info->keymap_fd, 0);
2939 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002940 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002941 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002942 goto err_dev_zero;
2943 }
2944 strcpy(xkb_info->keymap_area, keymap_str);
2945 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002946
2947 return;
2948
2949err_dev_zero:
2950 close(xkb_info->keymap_fd);
2951 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002952err_keymap_str:
2953 free(keymap_str);
2954 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002955}
2956
2957static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002958weston_compositor_build_global_keymap(struct weston_compositor *ec)
2959{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002960 if (ec->xkb_info.keymap != NULL)
2961 return;
2962
Daniel Stonee379da92012-05-30 16:32:04 +01002963 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002964 &ec->xkb_names,
2965 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002966 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002967 weston_log("failed to compile global XKB keymap\n");
2968 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002969 "options %s",
2970 ec->xkb_names.rules, ec->xkb_names.model,
2971 ec->xkb_names.layout, ec->xkb_names.variant,
2972 ec->xkb_names.options);
2973 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002974 }
2975
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002976 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002977}
2978
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002979WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002980weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002981{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002982 if (seat->has_keyboard)
2983 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002984
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002985 if (keymap != NULL) {
2986 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002987 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002988 }
2989 else {
2990 weston_compositor_build_global_keymap(seat->compositor);
2991 seat->xkb_info = seat->compositor->xkb_info;
2992 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2993 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002994
2995 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2996 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002997 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01002998 exit(1);
2999 }
3000
Daniel Stone71c38772012-06-22 13:21:30 +01003001 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01003002
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01003003 wl_keyboard_init(&seat->keyboard);
3004 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
3005
3006 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01003007}
3008
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003009WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01003010weston_seat_init_pointer(struct weston_seat *seat)
3011{
3012 if (seat->has_pointer)
3013 return;
3014
3015 wl_pointer_init(&seat->pointer);
3016 wl_seat_set_pointer(&seat->seat, &seat->pointer);
3017
3018 seat->has_pointer = 1;
3019}
3020
3021WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01003022weston_seat_init_touch(struct weston_seat *seat)
3023{
3024 if (seat->has_touch)
3025 return;
3026
3027 wl_touch_init(&seat->touch);
3028 wl_seat_set_touch(&seat->seat, &seat->touch);
3029
3030 seat->has_touch = 1;
3031}
3032
3033WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01003034weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05003035{
Daniel Stone37816df2012-05-16 18:45:18 +01003036 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01003037 seat->has_pointer = 0;
3038 seat->has_keyboard = 0;
3039 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05003040
Daniel Stone37816df2012-05-16 18:45:18 +01003041 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
3042 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003043
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003044 seat->sprite = NULL;
3045 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04003046
Daniel Stone37816df2012-05-16 18:45:18 +01003047 seat->compositor = ec;
3048 seat->hotspot_x = 16;
3049 seat->hotspot_y = 16;
3050 seat->modifier_state = 0;
3051 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05003052
Daniel Stone37816df2012-05-16 18:45:18 +01003053 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003054 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02003055
Daniel Stone37816df2012-05-16 18:45:18 +01003056 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03003057
Daniel Stone37816df2012-05-16 18:45:18 +01003058 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
3059 wl_signal_add(&seat->seat.drag_icon_signal,
3060 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04003061
3062 clipboard_create(seat);
Jan Arne Petersene829adc2012-08-10 16:47:22 +02003063 input_method_create(ec, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05003064}
3065
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003066WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01003067weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003068{
Daniel Stone37816df2012-05-16 18:45:18 +01003069 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003070 /* The global object is destroyed at wl_display_destroy() time. */
3071
Daniel Stone37816df2012-05-16 18:45:18 +01003072 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003073 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003074
Daniel Stone74419a22012-05-30 16:32:02 +01003075 if (seat->xkb_state.state != NULL)
3076 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01003077 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01003078
Daniel Stone37816df2012-05-16 18:45:18 +01003079 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003080}
3081
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003082static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003083drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
3084{
3085 weston_surface_configure(es,
3086 es->geometry.x + sx, es->geometry.y + sy,
3087 es->buffer->width, es->buffer->height);
3088}
3089
3090static int
Daniel Stone37816df2012-05-16 18:45:18 +01003091device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003092 struct weston_surface *surface)
3093{
Daniel Stone37816df2012-05-16 18:45:18 +01003094 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003095
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003096 if (surface->configure) {
3097 wl_resource_post_error(&surface->surface.resource,
3098 WL_DISPLAY_ERROR_INVALID_OBJECT,
3099 "surface->configure already set");
3100 return 0;
3101 }
3102
Daniel Stone37816df2012-05-16 18:45:18 +01003103 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003104
Daniel Stone37816df2012-05-16 18:45:18 +01003105 weston_surface_set_position(ws->drag_surface,
3106 wl_fixed_to_double(seat->pointer->x),
3107 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003108
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003109 surface->configure = drag_surface_configure;
3110
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003111 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01003112 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003113
3114 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003115}
3116
3117static void
Daniel Stone37816df2012-05-16 18:45:18 +01003118device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003119{
Daniel Stone37816df2012-05-16 18:45:18 +01003120 seat->drag_surface->configure = NULL;
3121 undef_region(&seat->drag_surface->input);
3122 wl_list_remove(&seat->drag_surface_destroy_listener.link);
3123 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003124}
3125
3126static void
Daniel Stone37816df2012-05-16 18:45:18 +01003127device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003128{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003129 struct wl_list *list;
3130
Daniel Stone37816df2012-05-16 18:45:18 +01003131 if (weston_surface_is_mapped(seat->drag_surface) ||
3132 !seat->drag_surface->buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003133 return;
3134
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003135 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
3136 list = &seat->sprite->layer_link;
3137 else
3138 list = &seat->compositor->cursor_layer.surface_list;
3139
3140 wl_list_insert(list, &seat->drag_surface->layer_link);
Daniel Stone37816df2012-05-16 18:45:18 +01003141 weston_surface_assign_output(seat->drag_surface);
3142 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003143}
3144
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003145static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003146weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01003147 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003148{
3149 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003150
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003151 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003152 return;
3153
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003154 if (seat->drag_surface && seat->seat.drag_surface &&
3155 (&seat->drag_surface->surface.resource !=
3156 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003157 /* between calls to this funcion we got a new drag_surface */
3158 surface_changed = 1;
3159
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003160 if (!seat->seat.drag_surface || surface_changed) {
3161 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003162 if (!surface_changed)
3163 return;
3164 }
3165
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003166 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003167 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003168 seat->seat.drag_surface;
3169 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003170 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003171 }
3172
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003173 /* the client may not have attached a buffer to the drag surface
3174 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003175 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003176
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02003177 /* the client may have attached a buffer with a different size to
3178 * the drag surface, causing the input region to be reset */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003179 if (region_is_undefined(&seat->drag_surface->input))
3180 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02003181
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003182 if (!dx && !dy)
3183 return;
3184
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003185 weston_surface_set_position(seat->drag_surface,
3186 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
3187 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003188}
3189
3190WL_EXPORT void
3191weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
3192{
Daniel Stone4dab5db2012-05-30 16:31:53 +01003193 struct weston_seat *seat;
3194
3195 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003196 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003197}
3198
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003199static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003200bind_output(struct wl_client *client,
3201 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003202{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003203 struct weston_output *output = data;
3204 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003205 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003206
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003207 resource = wl_client_add_object(client,
3208 &wl_output_interface, NULL, id, data);
3209
Casey Dahlin9074db52012-04-19 22:50:09 -04003210 wl_list_insert(&output->resource_list, &resource->link);
3211 resource->destroy = unbind_resource;
3212
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003213 wl_output_send_geometry(resource,
3214 output->x,
3215 output->y,
3216 output->mm_width,
3217 output->mm_height,
3218 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003219 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003220 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003221
3222 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003223 wl_output_send_mode(resource,
3224 mode->flags,
3225 mode->width,
3226 mode->height,
3227 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003228 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003229}
3230
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003231static const char vertex_shader[] =
3232 "uniform mat4 proj;\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05003233 "attribute vec2 position;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003234 "attribute vec2 texcoord;\n"
3235 "varying vec2 v_texcoord;\n"
3236 "void main()\n"
3237 "{\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05003238 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003239 " v_texcoord = texcoord;\n"
3240 "}\n";
3241
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02003242/* Declare common fragment shader uniforms */
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003243#define FRAGMENT_CONVERT_YUV \
Rob Clark0e5a2d02012-08-30 16:47:18 -05003244 " y *= alpha;\n" \
3245 " u *= alpha;\n" \
3246 " v *= alpha;\n" \
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003247 " gl_FragColor.r = y + 1.59602678 * v;\n" \
3248 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
3249 " gl_FragColor.b = y + 2.01723214 * u;\n" \
Rob Clark0e5a2d02012-08-30 16:47:18 -05003250 " gl_FragColor.a = alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003251
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02003252static const char texture_fragment_shader_rgba[] =
Kristian Høgsbergdfce71d2010-12-07 20:19:10 -05003253 "precision mediump float;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003254 "varying vec2 v_texcoord;\n"
3255 "uniform sampler2D tex;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003256 "uniform float alpha;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003257 "void main()\n"
3258 "{\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003259 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003260 "}\n";
3261
Pekka Paalanen6b5585b2012-08-30 16:47:19 -05003262static const char texture_fragment_shader_rgbx[] =
3263 "precision mediump float;\n"
3264 "varying vec2 v_texcoord;\n"
3265 "uniform sampler2D tex;\n"
3266 "uniform float alpha;\n"
3267 "void main()\n"
3268 "{\n"
3269 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
3270 " gl_FragColor.a = alpha;\n"
3271 "}\n";
3272
Rob Clarke3b95912012-08-31 16:42:18 -05003273static const char texture_fragment_shader_egl_external[] =
3274 "#extension GL_OES_EGL_image_external : require\n"
3275 "precision mediump float;\n"
3276 "varying vec2 v_texcoord;\n"
3277 "uniform samplerExternalOES tex;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003278 "uniform float alpha;\n"
Rob Clarke3b95912012-08-31 16:42:18 -05003279 "void main()\n"
3280 "{\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003281 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Rob Clarke3b95912012-08-31 16:42:18 -05003282 "}\n";
3283
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003284static const char texture_fragment_shader_y_uv[] =
3285 "precision mediump float;\n"
3286 "uniform sampler2D tex;\n"
3287 "uniform sampler2D tex1;\n"
3288 "varying vec2 v_texcoord;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003289 "uniform float alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003290 "void main() {\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003291 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
3292 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
3293 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
3294 FRAGMENT_CONVERT_YUV
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003295 "}\n";
3296
3297static const char texture_fragment_shader_y_u_v[] =
3298 "precision mediump float;\n"
3299 "uniform sampler2D tex;\n"
3300 "uniform sampler2D tex1;\n"
3301 "uniform sampler2D tex2;\n"
3302 "varying vec2 v_texcoord;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003303 "uniform float alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003304 "void main() {\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003305 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
3306 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
3307 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
3308 FRAGMENT_CONVERT_YUV
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003309 "}\n";
3310
3311static const char texture_fragment_shader_y_xuxv[] =
3312 "precision mediump float;\n"
3313 "uniform sampler2D tex;\n"
3314 "uniform sampler2D tex1;\n"
3315 "varying vec2 v_texcoord;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003316 "uniform float alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003317 "void main() {\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003318 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
3319 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
3320 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
3321 FRAGMENT_CONVERT_YUV
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003322 "}\n";
3323
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003324static const char solid_fragment_shader[] =
3325 "precision mediump float;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003326 "uniform vec4 color;\n"
Kristian Høgsbergdbae80e2012-03-29 11:34:39 -04003327 "uniform float alpha;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003328 "void main()\n"
3329 "{\n"
Kristian Høgsbergdbae80e2012-03-29 11:34:39 -04003330 " gl_FragColor = alpha * color\n;"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003331 "}\n";
3332
Kristian Høgsberga9468212010-06-14 21:03:11 -04003333static int
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003334compile_shader(GLenum type, const char *source)
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003335{
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003336 GLuint s;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003337 char msg[512];
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003338 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003339
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003340 s = glCreateShader(type);
3341 glShaderSource(s, 1, &source, NULL);
3342 glCompileShader(s);
3343 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003344 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003345 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
Martin Minarik6d118362012-06-07 18:01:59 +02003346 weston_log("shader info: %s\n", msg);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003347 return GL_NONE;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003348 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003349
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003350 return s;
3351}
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003352
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003353static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003354weston_shader_init(struct weston_shader *shader,
3355 const char *vertex_source, const char *fragment_source)
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003356{
3357 char msg[512];
3358 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003359
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003360 shader->vertex_shader =
3361 compile_shader(GL_VERTEX_SHADER, vertex_source);
3362 shader->fragment_shader =
3363 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
3364
3365 shader->program = glCreateProgram();
3366 glAttachShader(shader->program, shader->vertex_shader);
3367 glAttachShader(shader->program, shader->fragment_shader);
3368 glBindAttribLocation(shader->program, 0, "position");
3369 glBindAttribLocation(shader->program, 1, "texcoord");
3370
3371 glLinkProgram(shader->program);
3372 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003373 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003374 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
Martin Minarik6d118362012-06-07 18:01:59 +02003375 weston_log("link info: %s\n", msg);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003376 return -1;
3377 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003378
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003379 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02003380 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
3381 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
3382 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
Kristian Høgsberg541e5552011-12-14 09:24:11 -05003383 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
Pekka Paalanenf55f5442012-02-02 16:49:05 +02003384 shader->color_uniform = glGetUniformLocation(shader->program, "color");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003385
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003386 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003387}
3388
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003389WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003390weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003391{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003392 struct weston_compositor *c = output->compositor;
3393
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003394 pixman_region32_fini(&output->region);
Kristian Høgsberg8b72f602011-06-23 20:46:34 -04003395 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003396 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003397
3398 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003399}
3400
Scott Moreau1bad5db2012-08-18 01:04:05 -06003401static void
3402weston_output_compute_transform(struct weston_output *output)
3403{
3404 struct weston_matrix transform;
3405 int flip;
3406
3407 weston_matrix_init(&transform);
3408
3409 switch(output->transform) {
3410 case WL_OUTPUT_TRANSFORM_FLIPPED:
3411 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3412 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3413 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3414 flip = -1;
3415 break;
3416 default:
3417 flip = 1;
3418 break;
3419 }
3420
3421 switch(output->transform) {
3422 case WL_OUTPUT_TRANSFORM_NORMAL:
3423 case WL_OUTPUT_TRANSFORM_FLIPPED:
3424 transform.d[0] = flip;
3425 transform.d[1] = 0;
3426 transform.d[4] = 0;
3427 transform.d[5] = 1;
3428 break;
3429 case WL_OUTPUT_TRANSFORM_90:
3430 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3431 transform.d[0] = 0;
3432 transform.d[1] = -flip;
3433 transform.d[4] = 1;
3434 transform.d[5] = 0;
3435 break;
3436 case WL_OUTPUT_TRANSFORM_180:
3437 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3438 transform.d[0] = -flip;
3439 transform.d[1] = 0;
3440 transform.d[4] = 0;
3441 transform.d[5] = -1;
3442 break;
3443 case WL_OUTPUT_TRANSFORM_270:
3444 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3445 transform.d[0] = 0;
3446 transform.d[1] = flip;
3447 transform.d[4] = -1;
3448 transform.d[5] = 0;
3449 break;
3450 default:
3451 break;
3452 }
3453
3454 weston_matrix_multiply(&output->matrix, &transform);
3455}
3456
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003457WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003458weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003459{
Scott Moreau850ca422012-05-21 15:21:25 -06003460 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003461 struct weston_matrix camera;
3462 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003463
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003464 weston_matrix_init(&output->matrix);
3465 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003466 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
3467 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003468
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003469 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003470 2.0 / (output->width + output->border.left + output->border.right),
3471 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
3472
3473 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003474
Scott Moreauccbf29d2012-02-22 14:21:41 -07003475 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003476 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003477 weston_matrix_init(&camera);
3478 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06003479 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06003480 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003481 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003482 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003483 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003484 weston_matrix_multiply(&output->matrix, &modelview);
3485 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003486
Scott Moreauccbf29d2012-02-22 14:21:41 -07003487 output->dirty = 0;
3488}
3489
Scott Moreau1bad5db2012-08-18 01:04:05 -06003490static void
3491weston_output_transform_init(struct weston_output *output, uint32_t transform)
3492{
3493 output->transform = transform;
3494
3495 switch (transform) {
3496 case WL_OUTPUT_TRANSFORM_90:
3497 case WL_OUTPUT_TRANSFORM_270:
3498 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3499 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3500 /* Swap width and height */
3501 output->width = output->current->height;
3502 output->height = output->current->width;
3503 break;
3504 case WL_OUTPUT_TRANSFORM_NORMAL:
3505 case WL_OUTPUT_TRANSFORM_180:
3506 case WL_OUTPUT_TRANSFORM_FLIPPED:
3507 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3508 output->width = output->current->width;
3509 output->height = output->current->height;
3510 break;
3511 default:
3512 break;
3513 }
3514}
3515
Scott Moreauccbf29d2012-02-22 14:21:41 -07003516WL_EXPORT void
3517weston_output_move(struct weston_output *output, int x, int y)
3518{
3519 output->x = x;
3520 output->y = y;
3521
3522 pixman_region32_init(&output->previous_damage);
3523 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003524 output->width,
3525 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003526}
3527
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003528WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003529weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003530 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003531{
3532 output->compositor = c;
3533 output->x = x;
3534 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05003535 output->border.top = 0;
3536 output->border.bottom = 0;
3537 output->border.left = 0;
3538 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003539 output->mm_width = width;
3540 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003541 output->dirty = 1;
3542
Scott Moreau1bad5db2012-08-18 01:04:05 -06003543 if (transform != WL_OUTPUT_TRANSFORM_NORMAL)
3544 output->disable_planes++;
3545
3546 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06003547 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003548
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003549 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003550 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003551
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003552 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003553 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003554 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003555
Casey Dahlin58ba1372012-04-19 22:50:08 -04003556 output->id = ffs(~output->compositor->output_id_pool) - 1;
3557 output->compositor->output_id_pool |= 1 << output->id;
3558
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003559 output->global =
3560 wl_display_add_global(c->wl_display, &wl_output_interface,
3561 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003562}
3563
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003564static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003565compositor_bind(struct wl_client *client,
3566 void *data, uint32_t version, uint32_t id)
3567{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003568 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003569
3570 wl_client_add_object(client, &wl_compositor_interface,
3571 &compositor_interface, id, compositor);
3572}
3573
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003574static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003575log_uname(void)
3576{
3577 struct utsname usys;
3578
3579 uname(&usys);
3580
3581 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3582 usys.version, usys.machine);
3583}
3584
3585static void
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003586log_extensions(const char *name, const char *extensions)
3587{
3588 const char *p, *end;
3589 int l;
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003590 int len;
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003591
3592 l = weston_log("%s:", name);
3593 p = extensions;
3594 while (*p) {
3595 end = strchrnul(p, ' ');
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003596 len = end - p;
3597 if (l + len > 78)
Pekka Paalanen4e4167d2012-06-11 14:06:05 +03003598 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003599 len, p);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003600 else
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003601 l += weston_log_continue(" %.*s", len, p);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003602 for (p = end; isspace(*p); p++)
3603 ;
3604 }
3605 weston_log_continue("\n");
3606}
3607
Pekka Paalanen4e4167d2012-06-11 14:06:05 +03003608static void
3609log_egl_gl_info(EGLDisplay egldpy)
3610{
3611 const char *str;
3612
3613 str = eglQueryString(egldpy, EGL_VERSION);
3614 weston_log("EGL version: %s\n", str ? str : "(null)");
3615
3616 str = eglQueryString(egldpy, EGL_VENDOR);
3617 weston_log("EGL vendor: %s\n", str ? str : "(null)");
3618
3619 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
3620 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
3621
3622 str = eglQueryString(egldpy, EGL_EXTENSIONS);
3623 log_extensions("EGL extensions", str ? str : "(null)");
3624
3625 str = (char *)glGetString(GL_VERSION);
3626 weston_log("GL version: %s\n", str ? str : "(null)");
3627
3628 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
3629 weston_log("GLSL version: %s\n", str ? str : "(null)");
3630
3631 str = (char *)glGetString(GL_VENDOR);
3632 weston_log("GL vendor: %s\n", str ? str : "(null)");
3633
3634 str = (char *)glGetString(GL_RENDERER);
3635 weston_log("GL renderer: %s\n", str ? str : "(null)");
3636
3637 str = (char *)glGetString(GL_EXTENSIONS);
3638 log_extensions("GL extensions", str ? str : "(null)");
3639}
3640
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003641WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003642weston_compositor_init(struct weston_compositor *ec,
3643 struct wl_display *display,
3644 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003645 char *argv[],
3646 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003647{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003648 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003649 struct xkb_rule_names xkb_names;
3650 const struct config_key keyboard_config_keys[] = {
3651 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
3652 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
3653 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
3654 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
3655 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
3656 };
3657 const struct config_section cs[] = {
3658 { "keyboard",
3659 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
3660 };
3661
3662 memset(&xkb_names, 0, sizeof(xkb_names));
3663 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003664
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003665 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003666 wl_signal_init(&ec->destroy_signal);
3667 wl_signal_init(&ec->activate_signal);
3668 wl_signal_init(&ec->lock_signal);
3669 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003670 wl_signal_init(&ec->show_input_panel_signal);
3671 wl_signal_init(&ec->hide_input_panel_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01003672 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003673
Casey Dahlin58ba1372012-04-19 22:50:08 -04003674 ec->output_id_pool = 0;
3675
Kristian Høgsberga8873122011-11-23 10:39:34 -05003676 if (!wl_display_add_global(display, &wl_compositor_interface,
3677 ec, compositor_bind))
3678 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003679
Daniel Stone725c2c32012-06-22 14:04:36 +01003680 wl_list_init(&ec->surface_list);
3681 wl_list_init(&ec->layer_list);
3682 wl_list_init(&ec->seat_list);
3683 wl_list_init(&ec->output_list);
3684 wl_list_init(&ec->key_binding_list);
3685 wl_list_init(&ec->button_binding_list);
3686 wl_list_init(&ec->axis_binding_list);
3687 wl_list_init(&ec->fade.animation.link);
3688
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003689 weston_plane_init(&ec->primary_plane, 0, 0);
3690
Daniel Stone725c2c32012-06-22 14:04:36 +01003691 weston_compositor_xkb_init(ec, &xkb_names);
3692
3693 ec->ping_handler = NULL;
3694
3695 screenshooter_create(ec);
3696 text_cursor_position_notifier_create(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003697
3698 wl_data_device_manager_init(ec->wl_display);
3699
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003700 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003701
Daniel Stone725c2c32012-06-22 14:04:36 +01003702 loop = wl_display_get_event_loop(ec->wl_display);
3703 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3704 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3705
3706 ec->input_loop = wl_event_loop_create();
3707
3708 return 0;
3709}
3710
3711WL_EXPORT int
3712weston_compositor_init_gl(struct weston_compositor *ec)
3713{
3714 const char *extensions;
Rob Clarke3b95912012-08-31 16:42:18 -05003715 int has_egl_image_external = 0;
Daniel Stone725c2c32012-06-22 14:04:36 +01003716
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003717 log_egl_gl_info(ec->egl_display);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003718
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003719 ec->image_target_texture_2d =
3720 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3721 ec->image_target_renderbuffer_storage = (void *)
3722 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
3723 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3724 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3725 ec->bind_display =
3726 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
3727 ec->unbind_display =
3728 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02003729 ec->query_buffer =
3730 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003731
3732 extensions = (const char *) glGetString(GL_EXTENSIONS);
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003733 if (!extensions) {
Martin Minarik6d118362012-06-07 18:01:59 +02003734 weston_log("Retrieving GL extension string failed.\n");
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003735 return -1;
3736 }
3737
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003738 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
Martin Minarik6d118362012-06-07 18:01:59 +02003739 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003740 return -1;
3741 }
3742
Pekka Paalanena1d57db2012-04-17 15:02:08 +03003743 if (strstr(extensions, "GL_EXT_read_format_bgra"))
3744 ec->read_format = GL_BGRA_EXT;
3745 else
3746 ec->read_format = GL_RGBA;
Kristian Høgsbergf9247dd2012-03-27 15:25:46 -04003747
Pekka Paalanen52bfbaa2012-04-11 16:19:37 +03003748 if (strstr(extensions, "GL_EXT_unpack_subimage"))
3749 ec->has_unpack_subimage = 1;
Kristian Høgsbergfb6de222012-03-27 17:10:13 -04003750
Rob Clarke3b95912012-08-31 16:42:18 -05003751 if (strstr(extensions, "GL_OES_EGL_image_external"))
3752 has_egl_image_external = 1;
3753
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003754 extensions =
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003755 (const char *) eglQueryString(ec->egl_display, EGL_EXTENSIONS);
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003756 if (!extensions) {
Martin Minarik6d118362012-06-07 18:01:59 +02003757 weston_log("Retrieving EGL extension string failed.\n");
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003758 return -1;
3759 }
3760
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003761 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
3762 ec->has_bind_display = 1;
3763 if (ec->has_bind_display)
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003764 ec->bind_display(ec->egl_display, ec->wl_display);
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04003765
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003766 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003767 ec->fade.animation.frame = fade_frame;
Kristian Høgsberg3555d092011-04-11 13:58:13 -04003768
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003769 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3770 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3771
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003772 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003773
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02003774 if (weston_shader_init(&ec->texture_shader_rgba,
3775 vertex_shader, texture_fragment_shader_rgba) < 0)
Kristian Høgsberga9468212010-06-14 21:03:11 -04003776 return -1;
Pekka Paalanen6b5585b2012-08-30 16:47:19 -05003777 if (weston_shader_init(&ec->texture_shader_rgbx,
3778 vertex_shader, texture_fragment_shader_rgbx) < 0)
3779 return -1;
Rob Clarke3b95912012-08-31 16:42:18 -05003780 if (has_egl_image_external &&
3781 weston_shader_init(&ec->texture_shader_egl_external,
3782 vertex_shader, texture_fragment_shader_egl_external) < 0)
3783 return -1;
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003784 if (weston_shader_init(&ec->texture_shader_y_uv,
3785 vertex_shader, texture_fragment_shader_y_uv) < 0)
3786 return -1;
3787 if (weston_shader_init(&ec->texture_shader_y_u_v,
3788 vertex_shader, texture_fragment_shader_y_u_v) < 0)
3789 return -1;
3790 if (weston_shader_init(&ec->texture_shader_y_xuxv,
3791 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
3792 return -1;
Pekka Paalanenf55f5442012-02-02 16:49:05 +02003793 if (weston_shader_init(&ec->solid_shader,
3794 vertex_shader, solid_fragment_shader) < 0)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003795 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05003796
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003797 weston_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003798
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003799 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003800}
3801
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003802WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003803weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003804{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003805 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003806
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003807 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003808 if (ec->input_loop_source)
3809 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003810
Matt Roper361d2ad2011-08-29 13:52:23 -07003811 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003812 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003813 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003814
Daniel Stone325fc2d2012-05-30 16:31:58 +01003815 weston_binding_list_destroy_all(&ec->key_binding_list);
3816 weston_binding_list_destroy_all(&ec->button_binding_list);
3817 weston_binding_list_destroy_all(&ec->axis_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003818
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003819 weston_plane_release(&ec->primary_plane);
3820
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003821 wl_array_release(&ec->vertices);
3822 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05003823 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003824
3825 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07003826}
3827
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003828static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003829{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003830 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003831
Martin Minarik6d118362012-06-07 18:01:59 +02003832 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003833 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003834
3835 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003836}
3837
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003838static void
3839on_segv_signal(int s, siginfo_t *siginfo, void *context)
3840{
3841 void *buffer[32];
3842 int i, count;
3843 Dl_info info;
3844
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003845 /* This SIGSEGV handler will do a best-effort backtrace, and
3846 * then call the backend restore function, which will switch
3847 * back to the vt we launched from or ungrab X etc and then
3848 * raise SIGTRAP. If we run weston under gdb from X or a
3849 * different vt, and tell gdb "handle SIGSEGV nostop", this
3850 * will allow weston to switch back to gdb on crash and then
3851 * gdb will catch the crash with SIGTRAP. */
3852
Martin Minarik6d118362012-06-07 18:01:59 +02003853 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003854
3855 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3856 for (i = 0; i < count; i++) {
3857 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02003858 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003859 (long) buffer[i],
3860 info.dli_sname ? info.dli_sname : "--",
3861 info.dli_fname);
3862 }
3863
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003864 segv_compositor->restore(segv_compositor);
3865
3866 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003867}
3868
3869
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003870static void *
3871load_module(const char *name, const char *entrypoint, void **handle)
3872{
3873 char path[PATH_MAX];
3874 void *module, *init;
3875
3876 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003877 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003878 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003879 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003880
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003881 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003882 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003883 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003884 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003885 return NULL;
3886 }
3887
3888 init = dlsym(module, entrypoint);
3889 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003890 weston_log("Failed to lookup init function: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003891 return NULL;
3892 }
3893
3894 return init;
3895}
3896
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003897static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003898 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3899
3900static const char xdg_wrong_message[] =
3901 "fatal: environment variable XDG_RUNTIME_DIR\n"
3902 "is set to \"%s\", which is not a directory.\n";
3903
3904static const char xdg_wrong_mode_message[] =
3905 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3906 "correctly. Unix access mode must be 0700 but is %o,\n"
3907 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003908 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003909
3910static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003911 "Refer to your distribution on how to get it, or\n"
3912 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3913 "on how to implement it.\n";
3914
Martin Minarik37032f82012-06-18 20:15:18 +02003915static void
3916verify_xdg_runtime_dir(void)
3917{
3918 char *dir = getenv("XDG_RUNTIME_DIR");
3919 struct stat s;
3920
3921 if (!dir) {
3922 weston_log(xdg_error_message);
3923 weston_log_continue(xdg_detail_message);
3924 exit(EXIT_FAILURE);
3925 }
3926
3927 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3928 weston_log(xdg_wrong_message, dir);
3929 weston_log_continue(xdg_detail_message);
3930 exit(EXIT_FAILURE);
3931 }
3932
3933 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3934 weston_log(xdg_wrong_mode_message,
3935 dir, s.st_mode & 0777, s.st_uid);
3936 weston_log_continue(xdg_detail_message);
3937 }
3938}
3939
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003940static int
3941usage(int error_code)
3942{
3943 fprintf(stderr,
3944 "Usage: weston [OPTIONS]\n\n"
3945 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3946 "Weston supports multiple backends, and depending on which backend is in use\n"
3947 "different options will be accepted.\n\n"
3948
3949
3950 "Core options:\n\n"
3951 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3952 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3953 " -S, --socket=NAME\tName of socket to listen on\n"
3954 " -i, --idle-time=SECS\tIdle time in seconds\n"
3955 " --xserver\t\tEnable X server integration\n"
3956 " --module\t\tLoad the specified module\n"
3957 " --log==FILE\t\tLog to the given file\n"
3958 " -h, --help\t\tThis help message\n\n");
3959
3960 fprintf(stderr,
3961 "Options for drm-backend.so:\n\n"
3962 " --connector=ID\tBring up only this connector\n"
3963 " --seat=SEAT\t\tThe seat that weston should run on\n"
3964 " --tty=TTY\t\tThe tty to use\n"
3965 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3966
3967 fprintf(stderr,
3968 "Options for x11-backend.so:\n\n"
3969 " --width=WIDTH\t\tWidth of X window\n"
3970 " --height=HEIGHT\tHeight of X window\n"
3971 " --fullscreen\t\tRun in fullscreen mode\n"
3972 " --output-count=COUNT\tCreate multiple outputs\n"
3973 " --no-input\t\tDont create input devices\n\n");
3974
3975 fprintf(stderr,
3976 "Options for wayland-backend.so:\n\n"
3977 " --width=WIDTH\t\tWidth of Wayland surface\n"
3978 " --height=HEIGHT\tHeight of Wayland surface\n"
3979 " --display=DISPLAY\tWayland display to connect to\n\n");
3980
3981 exit(error_code);
3982}
3983
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003984int main(int argc, char *argv[])
3985{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003986 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003987 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003988 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003989 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003990 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003991 struct sigaction segv_action;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04003992 void *shell_module, *backend_module, *xserver_module;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003993 int (*module_init)(struct weston_compositor *ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003994 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003995 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003996 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003997 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003998 char *backend = NULL;
3999 char *shell = NULL;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004000 char *module = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004001 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004002 int32_t idle_time = 300;
Scott Moreaue17f6102012-04-25 10:03:06 -06004003 int32_t xserver = 0;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004004 int32_t help = 0;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004005 char *socket_name = NULL;
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004006 char *config_file;
4007
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004008 const struct config_key shell_config_keys[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004009 { "type", CONFIG_KEY_STRING, &shell },
4010 };
4011
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004012 const struct config_section cs[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004013 { "shell",
4014 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
4015 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004016
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004017 const struct weston_option core_options[] = {
4018 { WESTON_OPTION_STRING, "backend", 'B', &backend },
4019 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4020 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004021 { WESTON_OPTION_BOOLEAN, "xserver", 0, &xserver },
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004022 { WESTON_OPTION_STRING, "module", 0, &module },
Martin Minarik19e6f262012-06-07 13:08:46 +02004023 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004024 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004025 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004026
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004027 argc = parse_options(core_options,
4028 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004029
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004030 if (help)
4031 usage(EXIT_SUCCESS);
4032
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004033 weston_log_file_open(log);
4034
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004035 weston_log("%s\n"
4036 STAMP_SPACE "%s\n"
4037 STAMP_SPACE "Bug reports to: %s\n"
4038 STAMP_SPACE "Build: %s\n",
4039 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004040 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004041 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004042
Martin Minarik37032f82012-06-18 20:15:18 +02004043 verify_xdg_runtime_dir();
4044
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004045 display = wl_display_create();
4046
Tiago Vignatti2116b892011-08-08 05:52:59 -07004047 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004048 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4049 display);
4050 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4051 display);
4052 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4053 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004054
4055 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004056 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4057 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004058
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004059 if (!backend) {
4060 if (getenv("WAYLAND_DISPLAY"))
4061 backend = "wayland-backend.so";
4062 else if (getenv("DISPLAY"))
4063 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004064 else
4065 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004066 }
4067
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004068 config_file = config_file_path("weston.ini");
4069 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004070
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004071 backend_init = load_module(backend, "backend_init", &backend_module);
4072 if (!backend_init)
4073 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004074
Daniel Stonec1be8e52012-06-01 11:14:02 -04004075 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004076 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004077 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004078 exit(EXIT_FAILURE);
4079 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004080
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004081 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4082 segv_action.sa_sigaction = on_segv_signal;
4083 sigemptyset(&segv_action.sa_mask);
4084 sigaction(SIGSEGV, &segv_action, NULL);
4085 segv_compositor = ec;
4086
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004087 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03004088 weston_log("fatal: unhandled option: %s\n", argv[i]);
4089 if (argv[1]) {
4090 ret = EXIT_FAILURE;
4091 goto out;
4092 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004093
Daniel Stonec1be8e52012-06-01 11:14:02 -04004094 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01004095
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004096 ec->option_idle_time = idle_time;
4097 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004098
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004099 module_init = NULL;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04004100 if (xserver)
Tiago Vignatti629ce232012-05-23 23:04:14 +03004101 module_init = load_module("xwayland.so",
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004102 "weston_xserver_init",
4103 &xserver_module);
Pekka Paalanen33616392012-07-30 16:56:57 +03004104 if (module_init && module_init(ec) < 0) {
4105 ret = EXIT_FAILURE;
4106 goto out;
4107 }
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04004108
Kristian Høgsberg33d75092012-08-13 13:56:03 -04004109 if (socket_name)
4110 setenv("WAYLAND_DISPLAY", socket_name, 1);
4111
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004112 if (!shell)
4113 shell = "desktop-shell.so";
4114 module_init = load_module(shell, "shell_init", &shell_module);
Pekka Paalanen33616392012-07-30 16:56:57 +03004115 if (!module_init || module_init(ec) < 0) {
4116 ret = EXIT_FAILURE;
4117 goto out;
4118 }
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004119
4120
4121 module_init = NULL;
4122 if (module)
4123 module_init = load_module(module, "module_init", NULL);
Pekka Paalanen33616392012-07-30 16:56:57 +03004124 if (module_init && module_init(ec) < 0) {
4125 ret = EXIT_FAILURE;
4126 goto out;
4127 }
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004128
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004129 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004130 weston_log("fatal: failed to add socket: %m\n");
4131 ret = EXIT_FAILURE;
4132 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004133 }
4134
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004135 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004136 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004137
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004138 wl_display_run(display);
4139
4140 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004141 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05004142 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004143
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004144 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004145
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004146 if (ec->has_bind_display)
Kristian Høgsberg362b6722012-06-18 15:13:51 -04004147 ec->unbind_display(ec->egl_display, display);
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004148
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004149 for (i = ARRAY_LENGTH(signals); i;)
4150 wl_event_source_remove(signals[--i]);
4151
Daniel Stone855028f2012-05-01 20:37:10 +01004152 weston_compositor_xkb_destroy(ec);
4153
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004154 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004155 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004156
Martin Minarik19e6f262012-06-07 13:08:46 +02004157 weston_log_file_close();
4158
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004159 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004160}