blob: 918b455bbc3c7959bfbdacdd005f54587fa30470 [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
Pekka Paalanen07c91f82012-08-30 16:47:21 -050058#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
59
60
Kristian Høgsberg27da5382011-06-21 17:32:25 -040061static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040062static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040063
64static int
65sigchld_handler(int signal_number, void *data)
66{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050067 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040068 int status;
69 pid_t pid;
70
Bill Spitzak027b9622012-03-17 15:22:03 -070071 pid = waitpid(-1, &status, WNOHANG);
72 if (!pid)
73 return 1;
74
Kristian Høgsberg27da5382011-06-21 17:32:25 -040075 wl_list_for_each(p, &child_process_list, link) {
76 if (p->pid == pid)
77 break;
78 }
79
80 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020081 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040082 return 1;
83 }
84
85 wl_list_remove(&p->link);
86 p->cleanup(p, status);
87
88 return 1;
89}
90
Alex Wu2dda6042012-04-17 17:20:47 +080091WL_EXPORT int
92weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode)
93{
94 if (!output->switch_mode)
95 return -1;
96
97 return output->switch_mode(output, mode);
98}
99
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400100WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500101weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400102{
103 wl_list_insert(&child_process_list, &process->link);
104}
105
Benjamin Franzke06286262011-05-06 19:12:33 +0200106static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200107child_client_exec(int sockfd, const char *path)
108{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500109 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200110 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200111 sigset_t allsigs;
112
113 /* do not give our signal mask to the new process */
114 sigfillset(&allsigs);
115 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200116
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500117 /* Launch clients as the user. */
118 seteuid(getuid());
119
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500120 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
121 * non-CLOEXEC fd to pass through exec. */
122 clientfd = dup(sockfd);
123 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200124 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500125 return;
126 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200127
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500128 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200129 setenv("WAYLAND_SOCKET", s, 1);
130
131 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200132 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200133 path);
134}
135
136WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500137weston_client_launch(struct weston_compositor *compositor,
138 struct weston_process *proc,
139 const char *path,
140 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200141{
142 int sv[2];
143 pid_t pid;
144 struct wl_client *client;
145
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300146 weston_log("launching '%s'\n", path);
147
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300148 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200149 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200150 "socketpair failed while launching '%s': %m\n",
151 path);
152 return NULL;
153 }
154
155 pid = fork();
156 if (pid == -1) {
157 close(sv[0]);
158 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200159 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200160 "fork failed while launching '%s': %m\n", path);
161 return NULL;
162 }
163
164 if (pid == 0) {
165 child_client_exec(sv[1], path);
166 exit(-1);
167 }
168
169 close(sv[1]);
170
171 client = wl_client_create(compositor->wl_display, sv[0]);
172 if (!client) {
173 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200174 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200175 "wl_client_create failed while launching '%s'.\n",
176 path);
177 return NULL;
178 }
179
180 proc->pid = pid;
181 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500182 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200183
184 return client;
185}
186
187static void
Kristian Høgsberga85b4fb2012-06-20 09:24:35 -0400188update_shm_texture(struct weston_surface *surface);
189
190static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400191surface_handle_buffer_destroy(struct wl_listener *listener, void *data)
Benjamin Franzke06286262011-05-06 19:12:33 +0200192{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500193 struct weston_surface *es =
194 container_of(listener, struct weston_surface,
195 buffer_destroy_listener);
Benjamin Franzke06286262011-05-06 19:12:33 +0200196
Kristian Høgsberga85b4fb2012-06-20 09:24:35 -0400197 if (es->buffer && wl_buffer_is_shm(es->buffer))
198 update_shm_texture(es);
199
Kristian Høgsberg24596942011-10-24 17:51:02 -0400200 es->buffer = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200201}
202
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500203static const pixman_region32_data_t undef_region_data;
204
205static void
206undef_region(pixman_region32_t *region)
207{
Kristian Høgsberg66a099b2012-05-29 16:49:45 -0400208 if (region->data != &undef_region_data)
209 pixman_region32_fini(region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500210 region->data = (pixman_region32_data_t *) &undef_region_data;
211}
212
213static int
214region_is_undefined(pixman_region32_t *region)
215{
216 return region->data == &undef_region_data;
217}
218
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200219static void
220empty_region(pixman_region32_t *region)
221{
222 if (!region_is_undefined(region))
223 pixman_region32_fini(region);
224
225 pixman_region32_init(region);
226}
227
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500228WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500229weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500230{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500231 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400232
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200233 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400234 if (surface == NULL)
235 return NULL;
236
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400237 wl_signal_init(&surface->surface.resource.destroy_signal);
Kristian Høgsberg48891542012-02-23 17:38:33 -0500238
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500239 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500240 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500241
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400242 surface->surface.resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400243
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500244 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400245 surface->alpha = 1.0;
Juan Zhao46436612012-03-20 01:48:46 +0800246 surface->pitch = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400247
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200248 surface->num_textures = 0;
249 surface->num_images = 0;
250
Benjamin Franzke06286262011-05-06 19:12:33 +0200251 surface->buffer = NULL;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400252 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400253 surface->plane = &compositor->primary_plane;
Benjamin Franzke06286262011-05-06 19:12:33 +0200254
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400255 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500256 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500257 pixman_region32_init(&surface->clip);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500258 undef_region(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200259 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500260 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400261
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400262 surface->buffer_destroy_listener.notify =
263 surface_handle_buffer_destroy;
Benjamin Franzke06286262011-05-06 19:12:33 +0200264
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200265 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200266 wl_list_insert(&surface->geometry.transformation_list,
267 &surface->transform.position.link);
268 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200269 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200270 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500271
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400272 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500273}
274
Alex Wu8811bf92012-02-28 18:07:54 +0800275WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500276weston_surface_set_color(struct weston_surface *surface,
277 GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
278{
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500279 surface->color[0] = red;
280 surface->color[1] = green;
281 surface->color[2] = blue;
282 surface->color[3] = alpha;
283 surface->shader = &surface->compositor->solid_shader;
284}
285
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400286WL_EXPORT void
287weston_surface_to_global_float(struct weston_surface *surface,
288 GLfloat sx, GLfloat sy, GLfloat *x, GLfloat *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200289{
290 if (surface->transform.enabled) {
291 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
292
293 weston_matrix_transform(&surface->transform.matrix, &v);
294
295 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200296 weston_log("warning: numerical instability in "
Pekka Paalanenece8a012012-02-08 15:23:15 +0200297 "weston_surface_to_global(), divisor = %g\n",
298 v.f[3]);
299 *x = 0;
300 *y = 0;
301 return;
302 }
303
304 *x = v.f[0] / v.f[3];
305 *y = v.f[1] / v.f[3];
306 } else {
307 *x = sx + surface->geometry.x;
308 *y = sy + surface->geometry.y;
309 }
310}
311
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500312WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400313weston_surface_move_to_plane(struct weston_surface *surface,
314 struct weston_plane *plane)
315{
316 if (surface->plane == plane)
317 return;
318
319 weston_surface_damage_below(surface);
320 surface->plane = plane;
321 weston_surface_damage(surface);
322}
323
324WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500325weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200326{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500327 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200328
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500329 pixman_region32_init(&damage);
330 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
331 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400332 pixman_region32_union(&surface->plane->damage,
333 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500334 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200335}
336
337static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200338surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
339 int32_t width, int32_t height,
340 pixman_region32_t *bbox)
341{
Pekka Paalanen219b9822012-02-08 15:38:37 +0200342 GLfloat min_x = HUGE_VALF, min_y = HUGE_VALF;
343 GLfloat max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200344 int32_t s[4][2] = {
345 { sx, sy },
346 { sx, sy + height },
347 { sx + width, sy },
348 { sx + width, sy + height }
349 };
Pekka Paalanen219b9822012-02-08 15:38:37 +0200350 GLfloat int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200351 int i;
352
353 for (i = 0; i < 4; ++i) {
Pekka Paalanen219b9822012-02-08 15:38:37 +0200354 GLfloat x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400355 weston_surface_to_global_float(surface,
356 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200357 if (x < min_x)
358 min_x = x;
359 if (x > max_x)
360 max_x = x;
361 if (y < min_y)
362 min_y = y;
363 if (y > max_y)
364 max_y = y;
365 }
366
Pekka Paalanen219b9822012-02-08 15:38:37 +0200367 int_x = floorf(min_x);
368 int_y = floorf(min_y);
369 pixman_region32_init_rect(bbox, int_x, int_y,
370 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200371}
372
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200373static void
374weston_surface_update_transform_disable(struct weston_surface *surface)
375{
376 surface->transform.enabled = 0;
377
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200378 /* round off fractions when not transformed */
379 surface->geometry.x = roundf(surface->geometry.x);
380 surface->geometry.y = roundf(surface->geometry.y);
381
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200382 pixman_region32_init_rect(&surface->transform.boundingbox,
383 surface->geometry.x,
384 surface->geometry.y,
385 surface->geometry.width,
386 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500387
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400388 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500389 pixman_region32_copy(&surface->transform.opaque,
390 &surface->opaque);
391 pixman_region32_translate(&surface->transform.opaque,
392 surface->geometry.x,
393 surface->geometry.y);
394 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200395}
396
397static int
398weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200399{
400 struct weston_matrix *matrix = &surface->transform.matrix;
401 struct weston_matrix *inverse = &surface->transform.inverse;
402 struct weston_transform *tform;
403
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200404 surface->transform.enabled = 1;
405
406 /* Otherwise identity matrix, but with x and y translation. */
407 surface->transform.position.matrix.d[12] = surface->geometry.x;
408 surface->transform.position.matrix.d[13] = surface->geometry.y;
409
410 weston_matrix_init(matrix);
411 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
412 weston_matrix_multiply(matrix, &tform->matrix);
413
414 if (weston_matrix_invert(inverse, matrix) < 0) {
415 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200416 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200417 " transformation not invertible.\n", surface);
418 return -1;
419 }
420
421 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
422 surface->geometry.height,
423 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500424
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200425 return 0;
426}
427
428WL_EXPORT void
429weston_surface_update_transform(struct weston_surface *surface)
430{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200431 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200432 return;
433
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200434 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200435
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500436 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200437
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200438 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500439 pixman_region32_fini(&surface->transform.opaque);
440 pixman_region32_init(&surface->transform.opaque);
441
442 if (region_is_undefined(&surface->input))
443 pixman_region32_init_rect(&surface->input, 0, 0,
444 surface->geometry.width,
445 surface->geometry.height);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200446
Pekka Paalanencd403622012-01-25 13:37:39 +0200447 /* transform.position is always in transformation_list */
448 if (surface->geometry.transformation_list.next ==
449 &surface->transform.position.link &&
450 surface->geometry.transformation_list.prev ==
451 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200452 weston_surface_update_transform_disable(surface);
453 } else {
454 if (weston_surface_update_transform_enable(surface) < 0)
455 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200456 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200457
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400458 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200459
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300460 if (weston_surface_is_mapped(surface))
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +0200461 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200462}
463
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200464WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100465weston_surface_to_global_fixed(struct weston_surface *surface,
466 wl_fixed_t sx, wl_fixed_t sy,
467 wl_fixed_t *x, wl_fixed_t *y)
468{
469 GLfloat xf, yf;
470
471 weston_surface_to_global_float(surface,
472 wl_fixed_to_double(sx),
473 wl_fixed_to_double(sy),
474 &xf, &yf);
475 *x = wl_fixed_from_double(xf);
476 *y = wl_fixed_from_double(yf);
477}
478
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200479static void
480surface_from_global_float(struct weston_surface *surface,
Daniel Stonebd3489b2012-05-08 17:17:53 +0100481 GLfloat x, GLfloat y, GLfloat *sx, GLfloat *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200482{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200483 if (surface->transform.enabled) {
484 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
485
486 weston_matrix_transform(&surface->transform.inverse, &v);
487
488 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200489 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200490 "weston_surface_from_global(), divisor = %g\n",
491 v.f[3]);
492 *sx = 0;
493 *sy = 0;
494 return;
495 }
496
Pekka Paalanencd403622012-01-25 13:37:39 +0200497 *sx = v.f[0] / v.f[3];
498 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200499 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200500 *sx = x - surface->geometry.x;
501 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200502 }
503}
504
505WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100506weston_surface_from_global_fixed(struct weston_surface *surface,
507 wl_fixed_t x, wl_fixed_t y,
508 wl_fixed_t *sx, wl_fixed_t *sy)
509{
510 GLfloat sxf, syf;
511
Daniel Stonebd3489b2012-05-08 17:17:53 +0100512 surface_from_global_float(surface,
513 wl_fixed_to_double(x),
514 wl_fixed_to_double(y),
515 &sxf, &syf);
516 *sx = wl_fixed_from_double(sxf);
517 *sy = wl_fixed_from_double(syf);
518}
519
520WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200521weston_surface_from_global(struct weston_surface *surface,
522 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
523{
524 GLfloat sxf, syf;
525
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200526 surface_from_global_float(surface, x, y, &sxf, &syf);
527 *sx = floorf(sxf);
528 *sy = floorf(syf);
529}
530
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400531WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400532weston_surface_schedule_repaint(struct weston_surface *surface)
533{
534 struct weston_output *output;
535
536 wl_list_for_each(output, &surface->compositor->output_list, link)
537 if (surface->output_mask & (1 << output->id))
538 weston_output_schedule_repaint(output);
539}
540
541WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500542weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500543{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400544 pixman_region32_union_rect(&surface->damage, &surface->damage,
545 0, 0, surface->geometry.width,
546 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200547
Kristian Høgsberg98238702012-08-03 16:29:12 -0400548 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500549}
550
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400551WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500552weston_surface_configure(struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200553 GLfloat x, GLfloat y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400554{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200555 surface->geometry.x = x;
556 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200557 surface->geometry.width = width;
558 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200559 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400560}
561
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200562WL_EXPORT void
563weston_surface_set_position(struct weston_surface *surface,
564 GLfloat x, GLfloat y)
565{
566 surface->geometry.x = x;
567 surface->geometry.y = y;
568 surface->geometry.dirty = 1;
569}
570
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300571WL_EXPORT int
572weston_surface_is_mapped(struct weston_surface *surface)
573{
574 if (surface->output)
575 return 1;
576 else
577 return 0;
578}
579
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400580WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500581weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500582{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400583 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500584
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400585 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500586
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400587 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500588}
589
Tiago Vignatti9d393522012-02-10 16:26:19 +0200590static struct weston_surface *
591weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100592 wl_fixed_t x, wl_fixed_t y,
593 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200594{
595 struct weston_surface *surface;
596
597 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100598 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500599 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100600 wl_fixed_to_int(*sx),
601 wl_fixed_to_int(*sy),
602 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200603 return surface;
604 }
605
606 return NULL;
607}
608
609static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400610weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500611{
Scott Moreau447013d2012-02-18 05:05:29 -0700612 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500613 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400614 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500615
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400616 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100617 return;
618
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400619 surface = weston_compositor_pick_surface(seat->compositor,
620 pointer->x,
621 pointer->y,
622 &pointer->current_x,
623 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500624
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400625 if (&surface->surface != pointer->current) {
626 interface = pointer->grab->interface;
627 pointer->current = &surface->surface;
628 interface->focus(pointer->grab, &surface->surface,
629 pointer->current_x,
630 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500631 }
632
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400633 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500634 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100635 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400636 pointer->x,
637 pointer->y,
638 &pointer->grab->x,
639 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500640}
641
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400642static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500643weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400644{
Daniel Stone37816df2012-05-16 18:45:18 +0100645 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400646
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500647 if (!compositor->focus)
648 return;
649
Daniel Stone37816df2012-05-16 18:45:18 +0100650 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400651 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400652}
653
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400654WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500655weston_surface_unmap(struct weston_surface *surface)
656{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100657 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500658
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500659 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500660 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500661 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500662
Daniel Stone4dab5db2012-05-30 16:31:53 +0100663 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100664 if (seat->seat.keyboard &&
665 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100666 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100667 if (seat->seat.pointer &&
668 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100669 wl_pointer_set_focus(seat->seat.pointer,
670 NULL,
671 wl_fixed_from_int(0),
672 wl_fixed_from_int(0));
673 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500674
Kristian Høgsberg98238702012-08-03 16:29:12 -0400675 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500676}
677
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400678struct weston_frame_callback {
679 struct wl_resource resource;
680 struct wl_list link;
681};
682
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500683static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400684destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500685{
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200686 int i;
687
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500688 struct weston_surface *surface =
689 container_of(resource,
690 struct weston_surface, surface.resource);
691 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400692 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400693
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300694 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500695 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400696
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200697 glDeleteTextures(surface->num_textures, surface->textures);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200698
Benjamin Franzke06286262011-05-06 19:12:33 +0200699 if (surface->buffer)
700 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400701
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200702 for (i = 0; i < surface->num_images; i++)
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400703 compositor->destroy_image(compositor->egl_display,
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200704 surface->images[i]);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200705
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200706 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200707 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500708 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500709 pixman_region32_fini(&surface->clip);
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500710 if (!region_is_undefined(&surface->input))
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500711 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200712
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400713 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
714 wl_resource_destroy(&cb->resource);
715
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400716 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500717}
718
Alex Wu8811bf92012-02-28 18:07:54 +0800719WL_EXPORT void
720weston_surface_destroy(struct weston_surface *surface)
721{
722 /* Not a valid way to destroy a client surface */
723 assert(surface->surface.resource.client == NULL);
724
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400725 wl_signal_emit(&surface->surface.resource.destroy_signal,
726 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800727 destroy_surface(&surface->surface.resource);
728}
729
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100730static void
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200731ensure_textures(struct weston_surface *es, int num_textures)
732{
733 int i;
734
735 if (num_textures <= es->num_textures)
736 return;
737
738 for (i = es->num_textures; i < num_textures; i++) {
739 glGenTextures(1, &es->textures[i]);
Rob Clarke3b95912012-08-31 16:42:18 -0500740 glBindTexture(es->target, es->textures[i]);
741 glTexParameteri(es->target,
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200742 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
Rob Clarke3b95912012-08-31 16:42:18 -0500743 glTexParameteri(es->target,
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200744 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
745 }
746 es->num_textures = num_textures;
Rob Clarke3b95912012-08-31 16:42:18 -0500747 glBindTexture(es->target, 0);
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200748}
749
750static void
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -0400751weston_surface_attach(struct wl_surface *surface, struct wl_buffer *buffer)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100752{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500753 struct weston_surface *es = (struct weston_surface *) surface;
754 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400755 EGLint attribs[3], format;
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200756 int i, num_planes;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100757
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300758 if (es->buffer) {
759 weston_buffer_post_release(es->buffer);
760 wl_list_remove(&es->buffer_destroy_listener.link);
761 }
762
763 es->buffer = buffer;
764
765 if (!buffer) {
766 if (weston_surface_is_mapped(es))
767 weston_surface_unmap(es);
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200768 for (i = 0; i < es->num_images; i++) {
769 ec->destroy_image(ec->egl_display, es->images[i]);
770 es->images[i] = NULL;
Kristian Høgsbergb8ceaaa2012-06-19 15:41:12 -0400771 }
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200772 es->num_images = 0;
773 glDeleteTextures(es->num_textures, es->textures);
774 es->num_textures = 0;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300775 return;
776 }
777
778 buffer->busy_count++;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400779 wl_signal_add(&es->buffer->resource.destroy_signal,
780 &es->buffer_destroy_listener);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300781
782 if (es->geometry.width != buffer->width ||
783 es->geometry.height != buffer->height) {
784 undef_region(&es->input);
785 pixman_region32_fini(&es->opaque);
786 pixman_region32_init(&es->opaque);
787 }
788
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100789 if (wl_buffer_is_shm(buffer)) {
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200790 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
Rob Clarke3b95912012-08-31 16:42:18 -0500791 es->target = GL_TEXTURE_2D;
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200792
793 ensure_textures(es, 1);
794 glBindTexture(GL_TEXTURE_2D, es->textures[0]);
Kristian Høgsbergfb6de222012-03-27 17:10:13 -0400795 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
796 es->pitch, es->buffer->height, 0,
797 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
Kristian Høgsbergd6ad1222012-05-17 11:11:15 -0400798 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
Pekka Paalanen6b5585b2012-08-30 16:47:19 -0500799 es->shader = &ec->texture_shader_rgbx;
Kristian Høgsbergd6ad1222012-05-17 11:11:15 -0400800 else
Pekka Paalanen6b5585b2012-08-30 16:47:19 -0500801 es->shader = &ec->texture_shader_rgba;
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200802 } else if (ec->query_buffer(ec->egl_display, buffer,
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400803 EGL_TEXTURE_FORMAT, &format)) {
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200804 for (i = 0; i < es->num_images; i++)
Kristian Høgsberg971cbc82012-07-17 14:21:25 -0400805 ec->destroy_image(ec->egl_display, es->images[i]);
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200806 es->num_images = 0;
Rob Clarke3b95912012-08-31 16:42:18 -0500807 es->target = GL_TEXTURE_2D;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400808 switch (format) {
809 case EGL_TEXTURE_RGB:
810 case EGL_TEXTURE_RGBA:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200811 default:
812 num_planes = 1;
813 es->shader = &ec->texture_shader_rgba;
814 break;
Rob Clarke3b95912012-08-31 16:42:18 -0500815 case EGL_TEXTURE_EXTERNAL_WL:
816 num_planes = 1;
817 es->target = GL_TEXTURE_EXTERNAL_OES;
818 es->shader = &ec->texture_shader_egl_external;
819 break;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400820 case EGL_TEXTURE_Y_UV_WL:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200821 num_planes = 2;
822 es->shader = &ec->texture_shader_y_uv;
823 break;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400824 case EGL_TEXTURE_Y_U_V_WL:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200825 num_planes = 3;
826 es->shader = &ec->texture_shader_y_u_v;
827 break;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400828 case EGL_TEXTURE_Y_XUXV_WL:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200829 num_planes = 2;
830 es->shader = &ec->texture_shader_y_xuxv;
831 break;
832 }
833
834 ensure_textures(es, num_planes);
835 for (i = 0; i < num_planes; i++) {
836 attribs[0] = EGL_WAYLAND_PLANE_WL;
837 attribs[1] = i;
838 attribs[2] = EGL_NONE;
839 es->images[i] = ec->create_image(ec->egl_display,
840 NULL,
841 EGL_WAYLAND_BUFFER_WL,
842 buffer, attribs);
Rob Clark48cd58b2012-08-13 17:39:17 -0500843 if (!es->images[i]) {
844 weston_log("failed to create img for plane %d\n", i);
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200845 continue;
Rob Clark48cd58b2012-08-13 17:39:17 -0500846 }
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200847 es->num_images++;
848
849 glActiveTexture(GL_TEXTURE0 + i);
Rob Clarke3b95912012-08-31 16:42:18 -0500850 glBindTexture(es->target, es->textures[i]);
851 ec->image_target_texture_2d(es->target,
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200852 es->images[i]);
853 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400854
Ander Conselvan de Oliveira9390ae32012-03-27 17:36:38 +0300855 es->pitch = buffer->width;
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200856 } else {
Rob Clark48cd58b2012-08-13 17:39:17 -0500857 weston_log("unhandled buffer type!\n");
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100858 }
859}
860
Rob Clark0e5a2d02012-08-30 16:47:18 -0500861
862#define max(a, b) (((a) > (b)) ? (a) : (b))
863#define min(a, b) (((a) > (b)) ? (b) : (a))
864#define clip(x, a, b) min(max(x, a), b)
865#define sign(x) ((x) >= 0)
866
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400867static int
Rob Clark0e5a2d02012-08-30 16:47:18 -0500868calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
869 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500870{
Rob Clark0e5a2d02012-08-30 16:47:18 -0500871 int i, n = 0;
872 GLfloat min_x, max_x, min_y, max_y;
873 GLfloat x[4] = {
874 surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1,
875 };
876 GLfloat y[4] = {
877 surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2,
878 };
879 GLfloat cx1 = rect->x1;
880 GLfloat cx2 = rect->x2;
881 GLfloat cy1 = rect->y1;
882 GLfloat cy2 = rect->y2;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500883
Rob Clark0e5a2d02012-08-30 16:47:18 -0500884 GLfloat dist_squared(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
885 {
886 GLfloat dx = (x1 - x2);
887 GLfloat dy = (y1 - y2);
888 return dx * dx + dy * dy;
889 }
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400890
Rob Clark0e5a2d02012-08-30 16:47:18 -0500891 void append_vertex(GLfloat x, GLfloat y)
892 {
893 /* don't emit duplicate vertices: */
894 if ((n > 0) && (ex[n-1] == x) && (ey[n-1] == y))
895 return;
896 ex[n] = x;
897 ey[n] = y;
898 n++;
899 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500900
Rob Clark0e5a2d02012-08-30 16:47:18 -0500901 /* transform surface to screen space: */
902 for (i = 0; i < 4; i++)
903 weston_surface_to_global_float(es, x[i], y[i], &x[i], &y[i]);
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500904
Rob Clark0e5a2d02012-08-30 16:47:18 -0500905 /* find bounding box: */
906 min_x = max_x = x[0];
907 min_y = max_y = y[0];
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500908
Rob Clark0e5a2d02012-08-30 16:47:18 -0500909 for (i = 1; i < 4; i++) {
910 min_x = min(min_x, x[i]);
911 max_x = max(max_x, x[i]);
912 min_y = min(min_y, y[i]);
913 max_y = max(max_y, y[i]);
914 }
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500915
Rob Clark0e5a2d02012-08-30 16:47:18 -0500916 /* First, simple bounding box check to discard early transformed
917 * surface rects that do not intersect with the clip region:
918 */
919 if ((min_x > cx2) || (max_x < cx1) ||
920 (min_y > cy2) || (max_y < cy1))
921 return 0;
922
923 /* Simple case, bounding box edges are parallel to surface edges,
924 * there will be only four edges. We just need to clip the surface
925 * vertices to the clip rect bounds:
926 */
927 if (!es->transform.enabled) {
928 for (i = 0; i < 4; i++) {
929 ex[n] = clip(x[i], cx1, cx2);
930 ey[n] = clip(y[i], cy1, cy2);
931 n++;
932 }
933 return 4;
934 }
935
936 /* Hard case, transformation applied. We need to find the vertices
937 * of the shape that is the intersection of the clip rect and
938 * transformed surface. This can be anything from 3 to 8 sides.
939 *
940 * Observation: all the resulting vertices will be the intersection
941 * points of the transformed surface and the clip rect, plus the
942 * vertices of the clip rect which are enclosed by the transformed
943 * surface and the vertices of the transformed surface which are
944 * enclosed by the clip rect.
945 *
946 * Observation: there will be zero, one, or two resulting vertices
947 * for each edge of the src rect.
948 *
949 * Loop over four edges of the transformed rect:
950 */
951 for (i = 0; i < 4; i++) {
952 GLfloat x1, y1, x2, y2;
953 int last_n = n;
954
955 x1 = x[i];
956 y1 = y[i];
957
958 /* if this vertex is contained in the clip rect, use it as-is: */
959 if ((cx1 <= x1) && (x1 <= cx2) &&
960 (cy1 <= y1) && (y1 <= cy2))
961 append_vertex(x1, y1);
962
963 /* for remaining, we consider the point as part of a line: */
964 x2 = x[(i+1) % 4];
965 y2 = y[(i+1) % 4];
966
967 if (x1 == x2) {
968 append_vertex(clip(x1, cx1, cx2), clip(y1, cy1, cy2));
969 append_vertex(clip(x2, cx1, cx2), clip(y2, cy1, cy2));
970 } else if (y1 == y2) {
971 append_vertex(clip(x1, cx1, cx2), clip(y1, cy1, cy2));
972 append_vertex(clip(x2, cx1, cx2), clip(y2, cy1, cy2));
973 } else {
974 GLfloat m, c, p;
975 GLfloat tx[2], ty[2];
976 int tn = 0;
977
978 int intersect_horiz(GLfloat y, GLfloat *p)
979 {
980 GLfloat x;
981
982 /* if y does not lie between y1 and y2, no
983 * intersection possible
984 */
985 if (sign(y-y1) == sign(y-y2))
986 return 0;
987
988 x = (y - c) / m;
989
990 /* if x does not lie between cx1 and cx2, no
991 * intersection:
992 */
993 if (sign(x-cx1) == sign(x-cx2))
994 return 0;
995
996 *p = x;
997 return 1;
998 }
999
1000 int intersect_vert(GLfloat x, GLfloat *p)
1001 {
1002 GLfloat y;
1003
1004 if (sign(x-x1) == sign(x-x2))
1005 return 0;
1006
1007 y = m * x + c;
1008
1009 if (sign(y-cy1) == sign(y-cy2))
1010 return 0;
1011
1012 *p = y;
1013 return 1;
1014 }
1015
1016 /* y = mx + c */
1017 m = (y2 - y1) / (x2 - x1);
1018 c = y1 - m * x1;
1019
1020 /* check for up to two intersections with the four edges
1021 * of the clip rect. Note that we don't know the orientation
1022 * of the transformed surface wrt. the clip rect. So if when
1023 * there are two intersection points, we need to put the one
1024 * closest to x1,y1 first:
1025 */
1026
1027 /* check top clip rect edge: */
1028 if (intersect_horiz(cy1, &p)) {
1029 ty[tn] = cy1;
1030 tx[tn] = p;
1031 tn++;
1032 }
1033
1034 /* check right clip rect edge: */
1035 if (intersect_vert(cx2, &p)) {
1036 ty[tn] = p;
1037 tx[tn] = cx2;
1038 tn++;
1039 if (tn == 2)
1040 goto edge_check_done;
1041 }
1042
1043 /* check bottom clip rect edge: */
1044 if (intersect_horiz(cy2, &p)) {
1045 ty[tn] = cy2;
1046 tx[tn] = p;
1047 tn++;
1048 if (tn == 2)
1049 goto edge_check_done;
1050 }
1051
1052 /* check left clip rect edge: */
1053 if (intersect_vert(cx1, &p)) {
1054 ty[tn] = p;
1055 tx[tn] = cx1;
1056 tn++;
1057 }
1058
1059edge_check_done:
1060 if (tn == 1) {
1061 append_vertex(tx[0], ty[0]);
1062 } else if (tn == 2) {
1063 if (dist_squared(x1, y1, tx[0], ty[0]) <
1064 dist_squared(x1, y1, tx[1], ty[1])) {
1065 append_vertex(tx[0], ty[0]);
1066 append_vertex(tx[1], ty[1]);
1067 } else {
1068 append_vertex(tx[1], ty[1]);
1069 append_vertex(tx[0], ty[0]);
1070 }
1071 }
1072
1073 if (n == last_n) {
1074 GLfloat best_x=0, best_y=0;
1075 uint32_t d, best_d = (unsigned int)-1; /* distance squared */
1076 uint32_t max_d = dist_squared(x2, y2,
1077 x[(i+2) % 4], y[(i+2) % 4]);
1078
1079 /* if there are no vertices on this line, it could be that
1080 * there is a vertex of the clip rect that is enclosed by
1081 * the transformed surface. Find the vertex of the clip
1082 * rect that is reached by the shortest line perpendicular
1083 * to the current edge, if any.
1084 *
1085 * slope of perpendicular is 1/m, so
1086 *
1087 * cy = -cx/m + c2
1088 * c2 = cy + cx/m
1089 *
1090 */
1091
1092 int perp_intersect(GLfloat cx, GLfloat cy, uint32_t *d)
1093 {
1094 GLfloat c2 = cy + cx/m;
1095 GLfloat x = (c2 - c) / (m + 1/m);
1096
1097 /* if the x position of the intersection of the
1098 * perpendicular with the transformed edge does
1099 * not lie within the bounds of the edge, then
1100 * no intersection:
1101 */
1102 if (sign(x-x1) == sign(x-x2))
1103 return 0;
1104
1105 *d = dist_squared(cx, cy, x, (m * x) + c);
1106
1107 /* if intersection distance is further away than
1108 * opposite edge of surface region, it is invalid:
1109 */
1110 if (*d > max_d)
1111 return 0;
1112
1113 return 1;
1114 }
1115
1116 if (perp_intersect(cx1, cy1, &d)) {
1117 best_x = cx1;
1118 best_y = cy1;
1119 best_d = d;
1120 }
1121
1122 if (perp_intersect(cx1, cy2, &d) && (d < best_d)) {
1123 best_x = cx1;
1124 best_y = cy2;
1125 best_d = d;
1126 }
1127
1128 if (perp_intersect(cx2, cy2, &d) && (d < best_d)) {
1129 best_x = cx2;
1130 best_y = cy2;
1131 best_d = d;
1132 }
1133
1134 if (perp_intersect(cx2, cy1, &d) && (d < best_d)) {
1135 best_x = cx2;
1136 best_y = cy1;
1137 best_d = d;
1138 }
1139
1140 if (best_d != (unsigned int)-1) // XXX can this happen?
1141 append_vertex(best_x, best_y);
1142 }
1143 }
1144
Kristian Høgsberg525e4c02011-02-14 10:39:54 -05001145 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001146
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001147 return n;
1148}
1149
Rob Clark0e5a2d02012-08-30 16:47:18 -05001150static int
1151texture_region(struct weston_surface *es, pixman_region32_t *region,
1152 pixman_region32_t *surf_region)
1153{
1154 struct weston_compositor *ec = es->compositor;
1155 GLfloat *v, inv_width, inv_height;
1156 unsigned int *vtxcnt, nvtx = 0;
1157 pixman_box32_t *rects, *surf_rects;
1158 int i, j, k, nrects, nsurf;
1159
1160 rects = pixman_region32_rectangles(region, &nrects);
1161 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
1162
1163 /* worst case we can have 10 vertices per rect (ie. clipped into
1164 * an octagon):
1165 */
1166 v = wl_array_add(&ec->vertices, nrects * nsurf * 10 * 4 * sizeof *v);
1167 vtxcnt = wl_array_add(&ec->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
1168
1169 inv_width = 1.0 / es->pitch;
1170 inv_height = 1.0 / es->geometry.height;
1171
1172 for (i = 0; i < nrects; i++) {
1173 pixman_box32_t *rect = &rects[i];
1174 for (j = 0; j < nsurf; j++) {
1175 pixman_box32_t *surf_rect = &surf_rects[j];
1176 GLfloat cx, cy;
1177 GLfloat ex[8], ey[8]; /* edge points in screen space */
1178 int n;
1179
1180 void emit_vertex(GLfloat gx, GLfloat gy)
1181 {
1182 GLfloat sx, sy;
1183 surface_from_global_float(es, gx, gy, &sx, &sy);
1184 /* In groups of 4 attributes, first two are 'position', 2nd two
1185 * are 'texcoord'.
1186 */
1187 *(v++) = gx;
1188 *(v++) = gy;
1189 *(v++) = sx * inv_width;
1190 *(v++) = sy * inv_height;
1191 }
1192
1193 /* The transformed surface, after clipping to the clip region,
1194 * can have as many as eight sides, emitted as a triangle-fan.
1195 * The first vertex is the center, followed by each corner.
1196 *
1197 * If a corner of the transformed surface falls outside of the
1198 * clip region, instead of emitting one vertex for the corner
1199 * of the surface, up to two are emitted for two corresponding
1200 * intersection point(s) between the surface and the clip region.
1201 *
1202 * To do this, we first calculate the (up to eight) points that
1203 * form the intersection of the clip rect and the transformed
1204 * surface. After that we calculate the average to determine
1205 * the center point, and emit the center and edge vertices of
1206 * the fan.
1207 */
1208 n = calculate_edges(es, rect, surf_rect, ex, ey);
1209 if (n < 3)
1210 continue;
1211
1212 /* calculate/emit center point: */
1213 cx = 0;
1214 cy = 0;
1215 for (k = 0; k < n; k++) {
1216 cx += ex[k];
1217 cy += ey[k];
1218 }
1219 cx /= n;
1220 cy /= n;
1221 emit_vertex(cx, cy);
1222
1223 /* then emit edge points: */
1224 for (k = 0; k < n; k++)
1225 emit_vertex(ex[k], ey[k]);
1226
1227 /* and close the fan: */
1228 emit_vertex(ex[0], ey[0]);
1229
1230 vtxcnt[nvtx++] = n + 2;
1231 }
1232 }
1233
1234 return nvtx;
1235}
1236
1237static void
Pekka Paalanen07c91f82012-08-30 16:47:21 -05001238triangle_fan_debug(struct weston_surface *surface, int first, int count)
1239{
1240 struct weston_compositor *compositor = surface->compositor;
1241 int i;
1242 GLushort *buffer;
1243 GLushort *index;
1244 int nelems;
1245 static int color_idx = 0;
1246 static const GLfloat color[][4] = {
1247 { 1.0, 0.0, 0.0, 1.0 },
1248 { 0.0, 1.0, 0.0, 1.0 },
1249 { 0.0, 0.0, 1.0, 1.0 },
1250 { 1.0, 1.0, 1.0, 1.0 },
1251 };
1252
1253 nelems = (count - 1 + count - 2) * 2;
1254
1255 buffer = malloc(sizeof(GLushort) * nelems);
1256 index = buffer;
1257
1258 for (i = 1; i < count; i++) {
1259 *index++ = first;
1260 *index++ = first + i;
1261 }
1262
1263 for (i = 2; i < count; i++) {
1264 *index++ = first + i - 1;
1265 *index++ = first + i;
1266 }
1267
1268 glUseProgram(compositor->solid_shader.program);
1269 glUniform4fv(compositor->solid_shader.color_uniform, 1,
1270 color[color_idx++ % ARRAY_SIZE(color)]);
1271 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
1272 glUseProgram(surface->shader->program);
1273 free(buffer);
1274}
1275
1276static void
Rob Clark0e5a2d02012-08-30 16:47:18 -05001277repaint_region(struct weston_surface *es, pixman_region32_t *region,
1278 pixman_region32_t *surf_region)
1279{
1280 struct weston_compositor *ec = es->compositor;
1281 GLfloat *v;
1282 unsigned int *vtxcnt;
1283 int i, first, nfans;
1284
1285 /* The final region to be painted is the intersection of
1286 * 'region' and 'surf_region'. However, 'region' is in the global
1287 * coordinates, and 'surf_region' is in the surface-local
1288 * corodinates. texture_region() will iterate over all pairs of
1289 * rectangles from both regions, compute the intersection
1290 * polygon for each pair, and store it as a triangle fan if
1291 * it has a non-zero area.
1292 */
1293 nfans = texture_region(es, region, surf_region);
1294
1295 v = ec->vertices.data;
1296 vtxcnt = ec->vtxcnt.data;
1297
1298 /* position: */
1299 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
1300 glEnableVertexAttribArray(0);
1301
1302 /* texcoord: */
1303 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
1304 glEnableVertexAttribArray(1);
1305
1306 for (i = 0, first = 0; i < nfans; i++) {
1307 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
Pekka Paalanen07c91f82012-08-30 16:47:21 -05001308 if (ec->fan_debug)
1309 triangle_fan_debug(es, first, vtxcnt[i]);
Rob Clark0e5a2d02012-08-30 16:47:18 -05001310 first += vtxcnt[i];
1311 }
1312
1313 glDisableVertexAttribArray(1);
1314 glDisableVertexAttribArray(0);
1315
1316 ec->vertices.size = 0;
1317 ec->vtxcnt.size = 0;
1318}
1319
Pekka Paalanen2abe2e62012-09-03 16:48:42 +03001320static void
1321weston_compositor_use_shader(struct weston_compositor *compositor,
1322 struct weston_shader *shader)
1323{
1324 if (compositor->current_shader == shader)
1325 return;
1326
1327 glUseProgram(shader->program);
1328 compositor->current_shader = shader;
1329}
1330
1331static void
1332weston_shader_uniforms(struct weston_shader *shader,
1333 struct weston_surface *surface,
1334 struct weston_output *output)
1335{
1336 int i;
1337
1338 glUniformMatrix4fv(shader->proj_uniform,
1339 1, GL_FALSE, output->matrix.d);
1340 glUniform4fv(shader->color_uniform, 1, surface->color);
1341 glUniform1f(shader->alpha_uniform, surface->alpha);
1342
1343 for (i = 0; i < surface->num_textures; i++)
1344 glUniform1i(shader->tex_uniforms[i], i);
1345}
Rob Clark0e5a2d02012-08-30 16:47:18 -05001346
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05001347WL_EXPORT void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001348weston_surface_draw(struct weston_surface *es, struct weston_output *output,
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001349 pixman_region32_t *damage) /* in global coordinates */
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001350{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001351 struct weston_compositor *ec = es->compositor;
Rob Clark0e5a2d02012-08-30 16:47:18 -05001352 /* repaint bounding region in global coordinates: */
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001353 pixman_region32_t repaint;
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001354 /* non-opaque region in surface coordinates: */
1355 pixman_region32_t surface_blend;
Kristian Høgsberg40caded2011-06-20 19:48:16 -04001356 GLint filter;
Rob Clark0e5a2d02012-08-30 16:47:18 -05001357 int i;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001358
Pekka Paalanena9f8a212012-01-26 11:28:08 +02001359 pixman_region32_init(&repaint);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001360 pixman_region32_intersect(&repaint,
1361 &es->transform.boundingbox, damage);
1362 pixman_region32_subtract(&repaint, &repaint, &es->clip);
Pekka Paalanenc7b45f62012-02-06 12:16:07 +02001363
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001364 if (!pixman_region32_not_empty(&repaint))
Pekka Paalanena9f8a212012-01-26 11:28:08 +02001365 goto out;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001366
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001367 pixman_region32_subtract(&ec->primary_plane.damage,
1368 &ec->primary_plane.damage, &repaint);
1369
Kristian Høgsberg101cb652012-02-17 10:45:16 -05001370 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001371
Pekka Paalanen2abe2e62012-09-03 16:48:42 +03001372 weston_compositor_use_shader(ec, es->shader);
1373 weston_shader_uniforms(es->shader, es, output);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001374
Scott Moreauccbf29d2012-02-22 14:21:41 -07001375 if (es->transform.enabled || output->zoom.active)
Kristian Høgsberg40caded2011-06-20 19:48:16 -04001376 filter = GL_LINEAR;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001377 else
Pekka Paalanenc61eca62012-01-06 14:10:06 +02001378 filter = GL_NEAREST;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001379
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02001380 for (i = 0; i < es->num_textures; i++) {
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02001381 glActiveTexture(GL_TEXTURE0 + i);
Rob Clarke3b95912012-08-31 16:42:18 -05001382 glBindTexture(es->target, es->textures[i]);
1383 glTexParameteri(es->target, GL_TEXTURE_MIN_FILTER, filter);
1384 glTexParameteri(es->target, GL_TEXTURE_MAG_FILTER, filter);
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02001385 }
Kristian Høgsberg40caded2011-06-20 19:48:16 -04001386
Pekka Paalanen2abe2e62012-09-03 16:48:42 +03001387 /* blended region is whole surface minus opaque region: */
1388 pixman_region32_init_rect(&surface_blend, 0, 0,
1389 es->geometry.width, es->geometry.height);
1390 pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
1391
1392 if (pixman_region32_not_empty(&es->opaque)) {
1393 if (es->shader == &ec->texture_shader_rgba) {
1394 /* Special case for RGBA textures with possibly
1395 * bad data in alpha channel: use the shader
1396 * that forces texture alpha = 1.0.
1397 * Xwayland surfaces need this.
1398 */
1399 weston_compositor_use_shader(ec, &ec->texture_shader_rgbx);
1400 weston_shader_uniforms(&ec->texture_shader_rgbx, es, output);
1401 }
1402
1403 if (es->alpha < 1.0)
1404 glEnable(GL_BLEND);
1405 else
1406 glDisable(GL_BLEND);
1407
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001408 repaint_region(es, &repaint, &es->opaque);
Rob Clark0e5a2d02012-08-30 16:47:18 -05001409 }
Pekka Paalanen3df327f2012-01-24 15:53:35 +02001410
Rob Clark0e5a2d02012-08-30 16:47:18 -05001411 if (pixman_region32_not_empty(&surface_blend)) {
Pekka Paalanen2abe2e62012-09-03 16:48:42 +03001412 weston_compositor_use_shader(ec, es->shader);
Rob Clark0e5a2d02012-08-30 16:47:18 -05001413 glEnable(GL_BLEND);
1414 repaint_region(es, &repaint, &surface_blend);
1415 }
Pekka Paalanena9f8a212012-01-26 11:28:08 +02001416
Pekka Paalanen2abe2e62012-09-03 16:48:42 +03001417 pixman_region32_fini(&surface_blend);
1418
Pekka Paalanena9f8a212012-01-26 11:28:08 +02001419out:
Kristian Høgsberg525e4c02011-02-14 10:39:54 -05001420 pixman_region32_fini(&repaint);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001421}
1422
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001423WL_EXPORT void
1424weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001425{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001426 wl_list_remove(&surface->layer_link);
1427 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001428 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001429 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001430}
1431
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001432WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001433weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001434{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001435 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001436
1437 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001438 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001439}
1440
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001441WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001442weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +02001443{
Kristian Høgsberg24596942011-10-24 17:51:02 -04001444 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +02001445 return;
Benjamin Franzke06286262011-05-06 19:12:33 +02001446
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001447 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -05001448 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +02001449}
1450
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001451WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001452weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001453{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001454 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001455
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001456 pixman_region32_union(&compositor->primary_plane.damage,
1457 &compositor->primary_plane.damage,
1458 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001459 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001460}
1461
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001462static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001463fade_frame(struct weston_animation *animation,
1464 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001465{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001466 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001467 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001468 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001469 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001470
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001471 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -06001472 compositor->fade.spring.timestamp = msecs;
1473
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001474 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001475 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001476 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
1477 compositor->fade.spring.current);
1478 weston_surface_damage(surface);
1479
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001480 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001481 compositor->fade.spring.current =
1482 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001483 wl_list_remove(&animation->link);
1484 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001485
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001486 if (compositor->fade.spring.current < 0.001) {
1487 destroy_surface(&surface->surface.resource);
1488 compositor->fade.surface = NULL;
1489 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001490 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001491 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001492 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001493 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001494}
1495
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001496static void
1497update_shm_texture(struct weston_surface *surface)
1498{
1499#ifdef GL_UNPACK_ROW_LENGTH
1500 pixman_box32_t *rectangles;
1501 void *data;
1502 int i, n;
1503#endif
1504
Gwenole Beauchesne023f8552012-04-20 11:07:06 +02001505 glBindTexture(GL_TEXTURE_2D, surface->textures[0]);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001506
1507 if (!surface->compositor->has_unpack_subimage) {
1508 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1509 surface->pitch, surface->buffer->height, 0,
1510 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1511 wl_shm_buffer_get_data(surface->buffer));
1512
1513 return;
1514 }
1515
1516#ifdef GL_UNPACK_ROW_LENGTH
1517 /* Mesa does not define GL_EXT_unpack_subimage */
1518 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch);
1519 data = wl_shm_buffer_get_data(surface->buffer);
1520 rectangles = pixman_region32_rectangles(&surface->damage, &n);
1521 for (i = 0; i < n; i++) {
1522 glPixelStorei(GL_UNPACK_SKIP_PIXELS, rectangles[i].x1);
1523 glPixelStorei(GL_UNPACK_SKIP_ROWS, rectangles[i].y1);
1524 glTexSubImage2D(GL_TEXTURE_2D, 0,
1525 rectangles[i].x1, rectangles[i].y1,
Rob Bradford8f241562012-07-02 17:33:40 +01001526 rectangles[i].x2 - rectangles[i].x1,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001527 rectangles[i].y2 - rectangles[i].y1,
1528 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
1529 }
1530#endif
1531}
1532
1533static void
1534surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001535 pixman_region32_t *opaque)
1536{
1537 if (surface->buffer && wl_buffer_is_shm(surface->buffer))
1538 update_shm_texture(surface);
1539
1540 if (surface->transform.enabled) {
1541 pixman_box32_t *extents;
1542
1543 extents = pixman_region32_extents(&surface->damage);
1544 surface_compute_bbox(surface, extents->x1, extents->y1,
1545 extents->x2 - extents->x1,
1546 extents->y2 - extents->y1,
1547 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001548 pixman_region32_translate(&surface->damage,
1549 -surface->plane->x,
1550 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001551 } else {
1552 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001553 surface->geometry.x - surface->plane->x,
1554 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001555 }
1556
1557 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001558 pixman_region32_union(&surface->plane->damage,
1559 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001560 empty_region(&surface->damage);
1561 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001562 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001563}
1564
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001565static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001566weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001567{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001568 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001569 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001570 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001571 struct weston_animation *animation, *next;
1572 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001573 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001574 pixman_region32_t opaque, output_damage, new_damage;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001575 int32_t width, height;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001576
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001577 weston_compositor_update_drag_surfaces(ec);
1578
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001579 width = output->current->width +
1580 output->border.left + output->border.right;
1581 height = output->current->height +
1582 output->border.top + output->border.bottom;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001583
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001584 glViewport(0, 0, width, height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -05001585
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001586 /* Rebuild the surface list and update surface transforms up front. */
1587 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +02001588 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001589 wl_list_for_each(layer, &ec->layer_list, link) {
1590 wl_list_for_each(es, &layer->surface_list, layer_link) {
1591 weston_surface_update_transform(es);
1592 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +02001593 if (es->output == output) {
1594 wl_list_insert_list(&frame_callback_list,
1595 &es->frame_callback_list);
1596 wl_list_init(&es->frame_callback_list);
1597 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001598 }
1599 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001600
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001601 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001602 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001603 else
1604 wl_list_for_each(es, &ec->surface_list, link)
1605 weston_surface_move_to_plane(es, &ec->primary_plane);
1606
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001607 pixman_region32_init(&opaque);
1608
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001609 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001610 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001611
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -04001612 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001613
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001614 pixman_region32_init(&output_damage);
1615
1616 pixman_region32_init(&new_damage);
1617 pixman_region32_copy(&new_damage, &ec->primary_plane.damage);
1618
1619 pixman_region32_union(&ec->primary_plane.damage,
1620 &ec->primary_plane.damage,
1621 &output->previous_damage);
1622
1623 pixman_region32_intersect(&output->previous_damage,
1624 &new_damage, &output->region);
1625
1626 pixman_region32_intersect(&output_damage,
1627 &ec->primary_plane.damage, &output->region);
1628
1629 pixman_region32_fini(&new_damage);
1630
Scott Moreauccbf29d2012-02-22 14:21:41 -07001631 if (output->dirty)
1632 weston_output_update_matrix(output);
1633
Pekka Paalanen07c91f82012-08-30 16:47:21 -05001634 /* if debugging, redraw everything outside the damage to clean up
1635 * debug lines from the previous draw on this buffer:
1636 */
1637 if (ec->fan_debug) {
1638 pixman_region32_t undamaged;
1639 pixman_region32_init(&undamaged);
1640 pixman_region32_subtract(&undamaged, &output->region,
1641 &output_damage);
1642 ec->fan_debug = 0;
1643 output->repaint(output, &undamaged, 0);
1644 ec->fan_debug = 1;
1645 pixman_region32_fini(&undamaged);
1646 }
1647
1648 output->repaint(output, &output_damage, 1);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001649
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001650 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001651
Kristian Høgsbergef044142011-06-21 15:02:12 -04001652 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001653
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001654 weston_compositor_repick(ec);
1655 wl_event_loop_dispatch(ec->input_loop, 0);
1656
Jonas Ådahldb773762012-06-13 00:01:21 +02001657 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001658 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001659 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001660 }
Jonas Ådahldb773762012-06-13 00:01:21 +02001661 wl_list_init(&frame_callback_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001662
Scott Moreaud64cf212012-06-08 19:40:54 -06001663 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001664 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001665 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001666 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001667}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001668
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001669static int
1670weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001671{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001672 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001673
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001674 wl_event_loop_dispatch(compositor->input_loop, 0);
1675
1676 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001677}
1678
1679WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001680weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001681{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001682 struct weston_compositor *compositor = output->compositor;
1683 struct wl_event_loop *loop =
1684 wl_display_get_event_loop(compositor->wl_display);
1685 int fd;
1686
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001687 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001688 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001689 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001690 return;
1691 }
1692
1693 output->repaint_scheduled = 0;
1694 if (compositor->input_loop_source)
1695 return;
1696
1697 fd = wl_event_loop_get_fd(compositor->input_loop);
1698 compositor->input_loop_source =
1699 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1700 weston_compositor_read_input, compositor);
1701}
1702
1703static void
1704idle_repaint(void *data)
1705{
1706 struct weston_output *output = data;
1707
1708 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001709}
1710
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001711WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001712weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1713{
1714 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001715 if (below != NULL)
1716 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001717}
1718
1719WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001720weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001721{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001722 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001723 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001724
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001725 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001726 return;
1727
Kristian Høgsbergef044142011-06-21 15:02:12 -04001728 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001729 output->repaint_needed = 1;
1730 if (output->repaint_scheduled)
1731 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001732
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001733 wl_event_loop_add_idle(loop, idle_repaint, output);
1734 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001735
1736 if (compositor->input_loop_source) {
1737 wl_event_source_remove(compositor->input_loop_source);
1738 compositor->input_loop_source = NULL;
1739 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001740}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001741
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001742WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001743weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1744{
1745 struct weston_output *output;
1746
1747 wl_list_for_each(output, &compositor->output_list, link)
1748 weston_output_schedule_repaint(output);
1749}
1750
1751WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001752weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001753{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001754 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001755 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001756
Scott Moreau9d1b1122012-06-08 19:40:53 -06001757 output = container_of(compositor->output_list.next,
1758 struct weston_output, link);
1759
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001760 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001761 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001762 return;
1763
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001764 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001765 surface = weston_surface_create(compositor);
1766 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001767 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001768 wl_list_insert(&compositor->fade_layer.surface_list,
1769 &surface->layer_link);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001770 weston_surface_assign_output(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001771 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001772 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001773 }
1774
1775 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001776 if (wl_list_empty(&compositor->fade.animation.link)) {
1777 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001778 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001779 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001780 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001781}
1782
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001783static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001784surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001785{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001786 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001787}
1788
Casey Dahlin96d8a752012-04-19 22:50:07 -04001789static struct wl_resource *
1790find_resource_for_client(struct wl_list *list, struct wl_client *client)
1791{
1792 struct wl_resource *r;
1793
1794 wl_list_for_each(r, list, link) {
1795 if (r->client == client)
1796 return r;
1797 }
1798
1799 return NULL;
1800}
1801
Casey Dahlin9074db52012-04-19 22:50:09 -04001802static void
1803weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1804{
1805 uint32_t different = es->output_mask ^ mask;
1806 uint32_t entered = mask & different;
1807 uint32_t left = es->output_mask & different;
1808 struct weston_output *output;
Rob Bradford8667e772012-05-18 14:13:03 +01001809 struct wl_resource *resource = NULL;
Casey Dahlin9074db52012-04-19 22:50:09 -04001810 struct wl_client *client = es->surface.resource.client;
1811
Scott Moreaua5021522012-08-03 17:11:51 -06001812 es->output_mask = mask;
Casey Dahlin9074db52012-04-19 22:50:09 -04001813 if (es->surface.resource.client == NULL)
1814 return;
1815 if (different == 0)
1816 return;
1817
Casey Dahlin9074db52012-04-19 22:50:09 -04001818 wl_list_for_each(output, &es->compositor->output_list, link) {
1819 if (1 << output->id & different)
1820 resource =
1821 find_resource_for_client(&output->resource_list,
1822 client);
Kristian Høgsbergc7814d22012-07-12 12:34:43 -04001823 if (resource == NULL)
1824 continue;
Casey Dahlin9074db52012-04-19 22:50:09 -04001825 if (1 << output->id & entered)
1826 wl_surface_send_enter(&es->surface.resource, resource);
1827 if (1 << output->id & left)
1828 wl_surface_send_leave(&es->surface.resource, resource);
1829 }
1830}
1831
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001832WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001833weston_surface_assign_output(struct weston_surface *es)
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001834{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001835 struct weston_compositor *ec = es->compositor;
1836 struct weston_output *output, *new_output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001837 pixman_region32_t region;
Casey Dahlin9074db52012-04-19 22:50:09 -04001838 uint32_t max, area, mask;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001839 pixman_box32_t *e;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001840
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001841 new_output = NULL;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001842 max = 0;
Casey Dahlin9074db52012-04-19 22:50:09 -04001843 mask = 0;
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001844 pixman_region32_init(&region);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001845 wl_list_for_each(output, &ec->output_list, link) {
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001846 pixman_region32_intersect(&region, &es->transform.boundingbox,
1847 &output->region);
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001848
1849 e = pixman_region32_extents(&region);
1850 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1851
Casey Dahlin9074db52012-04-19 22:50:09 -04001852 if (area > 0)
1853 mask |= 1 << output->id;
1854
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001855 if (area >= max) {
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001856 new_output = output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001857 max = area;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001858 }
1859 }
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001860 pixman_region32_fini(&region);
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001861
1862 es->output = new_output;
Casey Dahlin9074db52012-04-19 22:50:09 -04001863 weston_surface_update_output_mask(es, mask);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001864}
1865
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001866static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001867surface_attach(struct wl_client *client,
1868 struct wl_resource *resource,
1869 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1870{
1871 struct weston_surface *es = resource->data;
1872 struct wl_buffer *buffer = NULL;
1873
1874 if (buffer_resource)
1875 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001876
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -04001877 weston_surface_attach(&es->surface, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001878
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001879 if (buffer && es->configure)
1880 es->configure(es, sx, sy);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001881}
1882
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001883static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001884surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001885 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001886 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001887{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001888 struct weston_surface *es = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001889
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001890 pixman_region32_union_rect(&es->damage, &es->damage,
1891 x, y, width, height);
Kristian Høgsberg98238702012-08-03 16:29:12 -04001892 weston_surface_schedule_repaint(es);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001893}
1894
Kristian Høgsberg33418202011-08-16 23:01:28 -04001895static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001896destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001897{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001898 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001899
1900 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001901 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001902}
1903
1904static void
1905surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001906 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001907{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001908 struct weston_frame_callback *cb;
1909 struct weston_surface *es = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001910
1911 cb = malloc(sizeof *cb);
1912 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001913 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001914 return;
1915 }
1916
1917 cb->resource.object.interface = &wl_callback_interface;
1918 cb->resource.object.id = callback;
1919 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001920 cb->resource.client = client;
1921 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001922
1923 wl_client_add_resource(client, &cb->resource);
Jonas Ådahldb773762012-06-13 00:01:21 +02001924 wl_list_insert(es->frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001925}
1926
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001927static void
1928surface_set_opaque_region(struct wl_client *client,
1929 struct wl_resource *resource,
1930 struct wl_resource *region_resource)
1931{
1932 struct weston_surface *surface = resource->data;
1933 struct weston_region *region;
1934
1935 pixman_region32_fini(&surface->opaque);
1936
1937 if (region_resource) {
1938 region = region_resource->data;
1939 pixman_region32_init_rect(&surface->opaque, 0, 0,
1940 surface->geometry.width,
1941 surface->geometry.height);
1942 pixman_region32_intersect(&surface->opaque,
1943 &surface->opaque, &region->region);
1944 } else {
1945 pixman_region32_init(&surface->opaque);
1946 }
1947
1948 surface->geometry.dirty = 1;
1949}
1950
1951static void
1952surface_set_input_region(struct wl_client *client,
1953 struct wl_resource *resource,
1954 struct wl_resource *region_resource)
1955{
1956 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001957 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001958
1959 if (region_resource) {
1960 region = region_resource->data;
1961 pixman_region32_init_rect(&surface->input, 0, 0,
1962 surface->geometry.width,
1963 surface->geometry.height);
1964 pixman_region32_intersect(&surface->input,
1965 &surface->input, &region->region);
1966 } else {
1967 pixman_region32_init_rect(&surface->input, 0, 0,
1968 surface->geometry.width,
1969 surface->geometry.height);
1970 }
1971
Kristian Høgsberg98238702012-08-03 16:29:12 -04001972 weston_surface_schedule_repaint(surface);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001973}
1974
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001975static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001976 surface_destroy,
1977 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001978 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001979 surface_frame,
1980 surface_set_opaque_region,
1981 surface_set_input_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001982};
1983
1984static void
1985compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001986 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001987{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001988 struct weston_compositor *ec = resource->data;
1989 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001990
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001991 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001992 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001993 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001994 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001995 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001996
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001997 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001998
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001999 surface->surface.resource.object.id = id;
2000 surface->surface.resource.object.interface = &wl_surface_interface;
2001 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04002002 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002003 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04002004
Kristian Høgsbergb313b022010-12-01 17:07:41 -05002005 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002006}
2007
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002008static void
2009destroy_region(struct wl_resource *resource)
2010{
2011 struct weston_region *region =
2012 container_of(resource, struct weston_region, resource);
2013
2014 pixman_region32_fini(&region->region);
2015 free(region);
2016}
2017
2018static void
2019region_destroy(struct wl_client *client, struct wl_resource *resource)
2020{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002021 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002022}
2023
2024static void
2025region_add(struct wl_client *client, struct wl_resource *resource,
2026 int32_t x, int32_t y, int32_t width, int32_t height)
2027{
2028 struct weston_region *region = resource->data;
2029
2030 pixman_region32_union_rect(&region->region, &region->region,
2031 x, y, width, height);
2032}
2033
2034static void
2035region_subtract(struct wl_client *client, struct wl_resource *resource,
2036 int32_t x, int32_t y, int32_t width, int32_t height)
2037{
2038 struct weston_region *region = resource->data;
2039 pixman_region32_t rect;
2040
2041 pixman_region32_init_rect(&rect, x, y, width, height);
2042 pixman_region32_subtract(&region->region, &region->region, &rect);
2043 pixman_region32_fini(&rect);
2044}
2045
2046static const struct wl_region_interface region_interface = {
2047 region_destroy,
2048 region_add,
2049 region_subtract
2050};
2051
2052static void
2053compositor_create_region(struct wl_client *client,
2054 struct wl_resource *resource, uint32_t id)
2055{
2056 struct weston_region *region;
2057
2058 region = malloc(sizeof *region);
2059 if (region == NULL) {
2060 wl_resource_post_no_memory(resource);
2061 return;
2062 }
2063
2064 region->resource.destroy = destroy_region;
2065
2066 region->resource.object.id = id;
2067 region->resource.object.interface = &wl_region_interface;
2068 region->resource.object.implementation =
2069 (void (**)(void)) &region_interface;
2070 region->resource.data = region;
2071
2072 pixman_region32_init(&region->region);
2073
2074 wl_client_add_resource(client, &region->resource);
2075}
2076
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002077static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002078 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002079 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002080};
2081
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002082WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002083weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002084{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002085 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2086 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002087
2088 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02002089 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002090}
2091
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002092static void
2093weston_compositor_dpms_on(struct weston_compositor *compositor)
2094{
2095 struct weston_output *output;
2096
2097 wl_list_for_each(output, &compositor->output_list, link)
2098 if (output->set_dpms)
2099 output->set_dpms(output, WESTON_DPMS_ON);
2100}
2101
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002102WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002103weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002104{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002105 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
2106 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002107 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002108 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002109 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002110 }
2111}
2112
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002113static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002114weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002115{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002116 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002117 compositor->idle_inhibit++;
2118}
2119
2120static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002121weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002122{
2123 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002124 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002125}
2126
2127static int
2128idle_handler(void *data)
2129{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002130 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002131
2132 if (compositor->idle_inhibit)
2133 return 1;
2134
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002135 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002136
2137 return 1;
2138}
2139
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002140WL_EXPORT void
2141weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2142{
2143 pixman_region32_init(&plane->damage);
2144 plane->x = x;
2145 plane->y = y;
2146}
2147
2148WL_EXPORT void
2149weston_plane_release(struct weston_plane *plane)
2150{
2151 pixman_region32_fini(&plane->damage);
2152}
2153
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002154static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002155weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002156
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002157static void
Daniel Stone37816df2012-05-16 18:45:18 +01002158clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002159{
Daniel Stone37816df2012-05-16 18:45:18 +01002160 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002161 struct weston_output *output, *prev = NULL;
2162 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002163
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002164 x = wl_fixed_to_int(*fx);
2165 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01002166 old_x = wl_fixed_to_int(seat->seat.pointer->x);
2167 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002168
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002169 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002170 if (pixman_region32_contains_point(&output->region,
2171 x, y, NULL))
2172 valid = 1;
2173 if (pixman_region32_contains_point(&output->region,
2174 old_x, old_y, NULL))
2175 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002176 }
Daniel Stone37816df2012-05-16 18:45:18 +01002177
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002178 if (!valid) {
2179 if (x < prev->x)
2180 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06002181 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002182 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06002183 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002184 if (y < prev->y)
2185 *fy = wl_fixed_from_int(prev->y);
2186 else if (y >= prev->y + prev->current->height)
2187 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06002188 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002189 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002190}
2191
2192WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002193notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002194{
2195 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002196 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002197 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002198 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002199 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002200
2201 weston_compositor_activity(ec);
2202
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002203 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002204
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002205 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002206
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002207 pointer->x = x;
2208 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002209
2210 ix = wl_fixed_to_int(x);
2211 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002212
Scott Moreauccbf29d2012-02-22 14:21:41 -07002213 wl_list_for_each(output, &ec->output_list, link)
2214 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002215 pixman_region32_contains_point(&output->region,
2216 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06002217 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002218
Daniel Stone37816df2012-05-16 18:45:18 +01002219 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002220 interface = pointer->grab->interface;
2221 interface->motion(pointer->grab, time,
2222 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002223
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002224 if (seat->sprite) {
2225 weston_surface_set_position(seat->sprite,
2226 ix - seat->hotspot_x,
2227 iy - seat->hotspot_y);
2228 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002229 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002230}
2231
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002232WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002233weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002234 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05002235{
Daniel Stone37816df2012-05-16 18:45:18 +01002236 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002237
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03002238 if (seat->seat.keyboard) {
2239 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
2240 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04002241 }
2242
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002243 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05002244}
2245
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002246WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002247notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002248 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05002249{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002250 struct weston_compositor *compositor = seat->compositor;
2251 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01002252 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002253 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002254 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05002255
Daniel Stone4dbadb12012-05-30 16:31:51 +01002256 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002257 if (compositor->ping_handler && focus)
2258 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002259 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002260 if (pointer->button_count == 0) {
2261 pointer->grab_button = button;
2262 pointer->grab_time = time;
2263 pointer->grab_x = pointer->x;
2264 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002265 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002266 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002267 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002268 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002269 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002270 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002271
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002272 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002273 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05002274
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002275 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05002276
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002277 if (pointer->button_count == 1)
2278 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002279 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05002280}
2281
Scott Moreau210d0792012-03-22 10:47:01 -06002282WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002283notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01002284 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002285{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002286 struct weston_compositor *compositor = seat->compositor;
2287 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01002288 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002289 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002290 uint32_t serial = wl_display_next_serial(compositor->wl_display);
2291
2292 if (compositor->ping_handler && focus)
2293 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06002294
2295 weston_compositor_activity(compositor);
2296
Scott Moreau6a3633d2012-03-20 08:47:59 -06002297 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002298 weston_compositor_run_axis_binding(compositor, seat,
2299 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06002300 else
2301 return;
2302
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002303 if (pointer->focus_resource)
2304 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01002305 value);
Scott Moreau210d0792012-03-22 10:47:01 -06002306}
2307
Daniel Stone05d58682012-06-22 13:21:38 +01002308WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002309notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002310{
Daniel Stone05d58682012-06-22 13:21:38 +01002311 struct wl_keyboard *keyboard = &seat->keyboard;
2312 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01002313 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002314 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01002315 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01002316 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01002317
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002318 /* Serialize and update our internal state, checking to see if it's
2319 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01002320 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
2321 XKB_STATE_DEPRESSED);
2322 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
2323 XKB_STATE_LATCHED);
2324 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
2325 XKB_STATE_LOCKED);
2326 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01002327 XKB_STATE_EFFECTIVE);
2328
Daniel Stone71c38772012-06-22 13:21:30 +01002329 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
2330 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
2331 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
2332 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01002333 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01002334
Daniel Stone71c38772012-06-22 13:21:30 +01002335 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
2336 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
2337 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
2338 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05002339
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002340 /* And update the modifier_state for bindings. */
2341 mods_lookup = mods_depressed | mods_latched;
2342 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01002343 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002344 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01002345 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002346 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01002347 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002348 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04002349 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
2350 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01002351
Daniel Stone8c8164f2012-05-30 16:31:45 +01002352 /* Finally, notify the compositor that LEDs have changed. */
2353 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01002354 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01002355 leds |= LED_NUM_LOCK;
2356 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01002357 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01002358 leds |= LED_CAPS_LOCK;
2359 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01002360 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01002361 leds |= LED_SCROLL_LOCK;
2362 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01002363 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01002364 seat->xkb_state.leds = leds;
2365
Daniel Stone05d58682012-06-22 13:21:38 +01002366 if (changed) {
2367 grab->interface->modifiers(grab,
2368 serial,
2369 keyboard->modifiers.mods_depressed,
2370 keyboard->modifiers.mods_latched,
2371 keyboard->modifiers.mods_locked,
2372 keyboard->modifiers.group);
2373 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002374}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04002375
Daniel Stone05d58682012-06-22 13:21:38 +01002376static void
2377update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002378 enum wl_keyboard_key_state state)
2379{
2380 enum xkb_key_direction direction;
2381
2382 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2383 direction = XKB_KEY_DOWN;
2384 else
2385 direction = XKB_KEY_UP;
2386
2387 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
2388 * broken keycode system, which starts at 8. */
2389 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
2390
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002391 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002392}
2393
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002394WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002395notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01002396 enum wl_keyboard_key_state state,
2397 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002398{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002399 struct weston_compositor *compositor = seat->compositor;
2400 struct wl_keyboard *keyboard = seat->seat.keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01002401 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002402 (struct weston_surface *) keyboard->focus;
2403 struct wl_keyboard_grab *grab = keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002404 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002405 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002406
Daniel Stonec9785ea2012-05-30 16:31:52 +01002407 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002408 if (compositor->ping_handler && focus)
2409 compositor->ping_handler(focus, serial);
2410
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002411 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002412 keyboard->grab_key = key;
2413 keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07002414 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002415 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07002416 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002417
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002418 end = keyboard->keys.data + keyboard->keys.size;
2419 for (k = keyboard->keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01002420 if (*k == key) {
2421 /* Ignore server-generated repeats. */
2422 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2423 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002424 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01002425 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002426 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002427 keyboard->keys.size = (void *) end - keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002428 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002429 k = wl_array_add(&keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002430 *k = key;
2431 }
Ray Strodee96dcb82008-12-20 02:00:49 -05002432
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002433 if (grab == &keyboard->default_grab) {
2434 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002435 state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002436 grab = keyboard->grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06002437 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05002438
Daniel Stone7ace3902012-05-30 16:31:40 +01002439 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01002440
2441 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002442 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01002443 wl_display_get_serial(compositor->wl_display),
2444 key,
2445 state);
2446 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002447}
2448
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002449WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002450notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01002451 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002452{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002453 struct weston_compositor *compositor = seat->compositor;
2454 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002455
2456 if (output) {
Daniel Stone37816df2012-05-16 18:45:18 +01002457 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002458 x - pointer->x,
2459 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002460
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002461 pointer->x = x;
2462 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002463 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002464 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002465 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002466 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03002467 /* FIXME: We should call wl_pointer_set_focus(seat,
2468 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002469 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002470}
2471
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002472static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002473destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002474{
Daniel Stone37816df2012-05-16 18:45:18 +01002475 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002476
Daniel Stone37816df2012-05-16 18:45:18 +01002477 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002478 saved_kbd_focus_listener);
2479
Daniel Stone37816df2012-05-16 18:45:18 +01002480 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002481}
2482
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002483WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002484notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002485 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002486{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002487 struct weston_compositor *compositor = seat->compositor;
2488 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04002489 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002490 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05002491
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002492 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002493 wl_array_copy(&keyboard->keys, keys);
2494 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002495 weston_compositor_idle_inhibit(compositor);
2496 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002497 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002498 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002499 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002500
Daniel Stoneef172672012-06-22 13:21:32 +01002501 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002502 wl_array_for_each(k, &keyboard->keys) {
2503 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01002504 WL_KEYBOARD_KEY_STATE_PRESSED);
2505 }
2506
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002507 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002508
2509 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002510 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2511 wl_keyboard_set_focus(keyboard, surface);
2512 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002513 }
2514}
2515
2516WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002517notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01002518{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002519 struct weston_compositor *compositor = seat->compositor;
2520 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002521 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002522
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002523 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002524 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002525 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002526 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002527 WL_KEYBOARD_KEY_STATE_RELEASED);
2528 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002529
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002530 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002531
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002532 if (keyboard->focus) {
2533 seat->saved_kbd_focus = keyboard->focus;
2534 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01002535 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002536 wl_signal_add(&keyboard->focus->resource.destroy_signal,
2537 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002538 }
2539
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002540 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002541 /* FIXME: We really need keyboard grab cancel here to
2542 * let the grab shut down properly. As it is we leak
2543 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002544 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002545}
2546
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002547static void
Daniel Stone37816df2012-05-16 18:45:18 +01002548touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002549{
Daniel Stone37816df2012-05-16 18:45:18 +01002550 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002551 struct wl_resource *resource;
2552
Pekka Paalanen56464252012-07-31 13:21:08 +03002553 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002554 return;
2555
Pekka Paalanen56464252012-07-31 13:21:08 +03002556 if (seat->touch->focus_resource)
2557 wl_list_remove(&seat->touch->focus_listener.link);
2558 seat->touch->focus = NULL;
2559 seat->touch->focus_resource = NULL;
2560
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002561 if (surface) {
2562 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01002563 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04002564 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002565 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02002566 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002567 return;
2568 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002569
Daniel Stone37816df2012-05-16 18:45:18 +01002570 seat->touch->focus = surface;
2571 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03002572 wl_signal_add(&resource->destroy_signal,
2573 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002574 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002575}
2576
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002577/**
2578 * notify_touch - emulates button touches and notifies surfaces accordingly.
2579 *
2580 * It assumes always the correct cycle sequence until it gets here: touch_down
2581 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2582 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002583 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002584 */
2585WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002586notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002587 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002588{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002589 struct weston_compositor *ec = seat->compositor;
2590 struct wl_touch *touch = seat->seat.touch;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002591 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002592 wl_fixed_t sx, sy;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002593 uint32_t serial = 0;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002594
2595 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01002596 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002597 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002598
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002599 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002600
2601 /* the first finger down picks the surface, and all further go
2602 * to that surface for the remainder of the touch session i.e.
2603 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002604 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002605 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002606 touch_set_focus(seat, &es->surface);
2607 } else if (touch->focus) {
2608 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002609 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002610 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002611
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002612 if (touch->focus_resource && touch->focus)
2613 wl_touch_send_down(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002614 serial, time,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002615 &touch->focus->resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002616 touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002617 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002618 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002619 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002620 if (!es)
2621 break;
2622
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002623 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002624 if (touch->focus_resource)
2625 wl_touch_send_motion(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002626 time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002627 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002628 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002629 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002630 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002631
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002632 if (touch->focus_resource)
2633 wl_touch_send_up(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002634 serial, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002635 if (seat->num_tp == 0)
2636 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002637 break;
2638 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002639}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002640
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04002641static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002642pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
2643{
2644 struct weston_seat *seat = container_of(listener, struct weston_seat,
2645 sprite_destroy_listener);
2646
2647 seat->sprite = NULL;
2648}
2649
2650static void
2651pointer_cursor_surface_configure(struct weston_surface *es,
2652 int32_t dx, int32_t dy)
2653{
2654 struct weston_seat *seat = es->private;
2655 int x, y;
2656
2657 assert(es == seat->sprite);
2658
2659 seat->hotspot_x -= dx;
2660 seat->hotspot_y -= dy;
2661
2662 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
2663 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
2664
2665 weston_surface_configure(seat->sprite, x, y,
2666 es->buffer->width, es->buffer->height);
2667
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03002668 empty_region(&es->input);
2669
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002670 if (!weston_surface_is_mapped(es)) {
2671 wl_list_insert(&es->compositor->cursor_layer.surface_list,
2672 &es->layer_link);
2673 weston_surface_assign_output(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002674 }
2675}
2676
2677static void
2678pointer_unmap_sprite(struct weston_seat *seat)
2679{
2680 if (weston_surface_is_mapped(seat->sprite))
2681 weston_surface_unmap(seat->sprite);
2682
2683 wl_list_remove(&seat->sprite_destroy_listener.link);
2684 seat->sprite->configure = NULL;
2685 seat->sprite->private = NULL;
2686 seat->sprite = NULL;
2687}
2688
2689static void
2690pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2691 uint32_t serial, struct wl_resource *surface_resource,
2692 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002693{
Daniel Stone37816df2012-05-16 18:45:18 +01002694 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002695 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002696
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002697 if (surface_resource)
2698 surface = container_of(surface_resource->data,
2699 struct weston_surface, surface);
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002700
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04002701 if (seat->seat.pointer->focus == NULL)
2702 return;
2703 if (seat->seat.pointer->focus->resource.client != client)
2704 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002705 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002706 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002707
2708 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002709 if (surface->configure) {
2710 wl_resource_post_error(&surface->surface.resource,
2711 WL_DISPLAY_ERROR_INVALID_OBJECT,
2712 "surface->configure already "
2713 "set");
2714 return;
2715 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002716 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002717
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002718 if (seat->sprite)
2719 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002720
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002721 if (!surface)
2722 return;
2723
2724 wl_signal_add(&surface->surface.resource.destroy_signal,
2725 &seat->sprite_destroy_listener);
2726
2727 surface->configure = pointer_cursor_surface_configure;
2728 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002729 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002730 seat->hotspot_x = x;
2731 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002732
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04002733 if (surface->buffer)
2734 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002735}
2736
Daniel Stone37816df2012-05-16 18:45:18 +01002737static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002738 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002739};
2740
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002741static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002742handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002743{
Daniel Stone37816df2012-05-16 18:45:18 +01002744 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002745
Daniel Stone37816df2012-05-16 18:45:18 +01002746 seat = container_of(listener, struct weston_seat,
2747 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002748
Daniel Stone37816df2012-05-16 18:45:18 +01002749 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002750}
2751
Casey Dahlin9074db52012-04-19 22:50:09 -04002752static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002753{
2754 wl_list_remove(&resource->link);
2755 free(resource);
2756}
2757
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002758static void
Daniel Stone37816df2012-05-16 18:45:18 +01002759seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2760 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002761{
Daniel Stone37816df2012-05-16 18:45:18 +01002762 struct weston_seat *seat = resource->data;
2763 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002764
Daniel Stone37816df2012-05-16 18:45:18 +01002765 if (!seat->seat.pointer)
2766 return;
2767
2768 cr = wl_client_add_object(client, &wl_pointer_interface,
2769 &pointer_interface, id, seat);
2770 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002771 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002772
2773 if (seat->seat.pointer->focus &&
2774 seat->seat.pointer->focus->resource.client == client) {
2775 struct weston_surface *surface;
2776 wl_fixed_t sx, sy;
2777
2778 surface = (struct weston_surface *) seat->seat.pointer->focus;
2779 weston_surface_from_global_fixed(surface,
2780 seat->seat.pointer->x,
2781 seat->seat.pointer->y,
2782 &sx,
2783 &sy);
2784 wl_pointer_set_focus(seat->seat.pointer,
2785 seat->seat.pointer->focus,
2786 sx,
2787 sy);
2788 }
Daniel Stone37816df2012-05-16 18:45:18 +01002789}
2790
2791static void
2792seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2793 uint32_t id)
2794{
2795 struct weston_seat *seat = resource->data;
2796 struct wl_resource *cr;
2797
2798 if (!seat->seat.keyboard)
2799 return;
2800
2801 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2802 seat);
2803 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002804 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002805
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002806 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2807 seat->xkb_info.keymap_fd,
2808 seat->xkb_info.keymap_size);
2809
Daniel Stone17b13bd2012-05-30 16:31:39 +01002810 if (seat->seat.keyboard->focus &&
2811 seat->seat.keyboard->focus->resource.client == client) {
2812 wl_keyboard_set_focus(seat->seat.keyboard,
2813 seat->seat.keyboard->focus);
2814 }
Daniel Stone37816df2012-05-16 18:45:18 +01002815}
2816
2817static void
2818seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2819 uint32_t id)
2820{
2821 struct weston_seat *seat = resource->data;
2822 struct wl_resource *cr;
2823
2824 if (!seat->seat.touch)
2825 return;
2826
2827 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2828 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002829 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002830}
2831
2832static const struct wl_seat_interface seat_interface = {
2833 seat_get_pointer,
2834 seat_get_keyboard,
2835 seat_get_touch,
2836};
2837
2838static void
2839bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2840{
2841 struct wl_seat *seat = data;
2842 struct wl_resource *resource;
2843 enum wl_seat_capability caps = 0;
2844
2845 resource = wl_client_add_object(client, &wl_seat_interface,
2846 &seat_interface, id, data);
2847 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002848 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002849
2850 if (seat->pointer)
2851 caps |= WL_SEAT_CAPABILITY_POINTER;
2852 if (seat->keyboard)
2853 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2854 if (seat->touch)
2855 caps |= WL_SEAT_CAPABILITY_TOUCH;
2856
2857 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002858}
2859
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002860static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002861device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002862{
Daniel Stone37816df2012-05-16 18:45:18 +01002863 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002864
Daniel Stone37816df2012-05-16 18:45:18 +01002865 seat = container_of(listener, struct weston_seat,
2866 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002867
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002868 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002869}
2870
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002871static void weston_compositor_xkb_init(struct weston_compositor *ec,
2872 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002873{
Daniel Stonee379da92012-05-30 16:32:04 +01002874 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002875 ec->xkb_context = xkb_context_new(0);
2876 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002877 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002878 exit(1);
2879 }
Daniel Stone994679a2012-05-30 16:31:43 +01002880 }
2881
2882 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002883 ec->xkb_names = *names;
2884 if (!ec->xkb_names.rules)
2885 ec->xkb_names.rules = strdup("evdev");
2886 if (!ec->xkb_names.model)
2887 ec->xkb_names.model = strdup("pc105");
2888 if (!ec->xkb_names.layout)
2889 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002890}
2891
Daniel Stonee379da92012-05-30 16:32:04 +01002892static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2893{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002894 if (xkb_info->keymap)
2895 xkb_map_unref(xkb_info->keymap);
2896
2897 if (xkb_info->keymap_area)
2898 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2899 if (xkb_info->keymap_fd >= 0)
2900 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002901}
2902
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002903static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2904{
Daniel Stonee379da92012-05-30 16:32:04 +01002905 free((char *) ec->xkb_names.rules);
2906 free((char *) ec->xkb_names.model);
2907 free((char *) ec->xkb_names.layout);
2908 free((char *) ec->xkb_names.variant);
2909 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002910
Daniel Stonee379da92012-05-30 16:32:04 +01002911 xkb_info_destroy(&ec->xkb_info);
2912 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002913}
2914
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002915static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002916weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002917{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002918 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002919
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002920 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2921 XKB_MOD_NAME_SHIFT);
2922 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2923 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002924 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2925 XKB_MOD_NAME_CTRL);
2926 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2927 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002928 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2929 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002930 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2931 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002932 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002933
2934 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2935 XKB_LED_NAME_NUM);
2936 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2937 XKB_LED_NAME_CAPS);
2938 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2939 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002940
2941 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2942 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002943 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002944 exit(EXIT_FAILURE);
2945 }
2946 xkb_info->keymap_size = strlen(keymap_str) + 1;
2947
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002948 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002949 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002950 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002951 (unsigned long) xkb_info->keymap_size);
2952 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002953 }
2954
2955 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2956 PROT_READ | PROT_WRITE,
2957 MAP_SHARED, xkb_info->keymap_fd, 0);
2958 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002959 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002960 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002961 goto err_dev_zero;
2962 }
2963 strcpy(xkb_info->keymap_area, keymap_str);
2964 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002965
2966 return;
2967
2968err_dev_zero:
2969 close(xkb_info->keymap_fd);
2970 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002971err_keymap_str:
2972 free(keymap_str);
2973 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002974}
2975
2976static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002977weston_compositor_build_global_keymap(struct weston_compositor *ec)
2978{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002979 if (ec->xkb_info.keymap != NULL)
2980 return;
2981
Daniel Stonee379da92012-05-30 16:32:04 +01002982 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002983 &ec->xkb_names,
2984 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002985 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002986 weston_log("failed to compile global XKB keymap\n");
2987 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002988 "options %s",
2989 ec->xkb_names.rules, ec->xkb_names.model,
2990 ec->xkb_names.layout, ec->xkb_names.variant,
2991 ec->xkb_names.options);
2992 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002993 }
2994
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002995 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002996}
2997
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002998WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002999weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01003000{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01003001 if (seat->has_keyboard)
3002 return;
Daniel Stone994679a2012-05-30 16:31:43 +01003003
Daniel Stonebb1df6a2012-05-30 16:32:06 +01003004 if (keymap != NULL) {
3005 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003006 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01003007 }
3008 else {
3009 weston_compositor_build_global_keymap(seat->compositor);
3010 seat->xkb_info = seat->compositor->xkb_info;
3011 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
3012 }
Daniel Stoned65b9092012-05-30 16:32:05 +01003013
3014 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
3015 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003016 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01003017 exit(1);
3018 }
3019
Daniel Stone71c38772012-06-22 13:21:30 +01003020 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01003021
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01003022 wl_keyboard_init(&seat->keyboard);
3023 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
3024
3025 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01003026}
3027
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003028WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01003029weston_seat_init_pointer(struct weston_seat *seat)
3030{
3031 if (seat->has_pointer)
3032 return;
3033
3034 wl_pointer_init(&seat->pointer);
3035 wl_seat_set_pointer(&seat->seat, &seat->pointer);
3036
3037 seat->has_pointer = 1;
3038}
3039
3040WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01003041weston_seat_init_touch(struct weston_seat *seat)
3042{
3043 if (seat->has_touch)
3044 return;
3045
3046 wl_touch_init(&seat->touch);
3047 wl_seat_set_touch(&seat->seat, &seat->touch);
3048
3049 seat->has_touch = 1;
3050}
3051
3052WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01003053weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05003054{
Daniel Stone37816df2012-05-16 18:45:18 +01003055 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01003056 seat->has_pointer = 0;
3057 seat->has_keyboard = 0;
3058 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05003059
Daniel Stone37816df2012-05-16 18:45:18 +01003060 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
3061 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003062
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003063 seat->sprite = NULL;
3064 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04003065
Daniel Stone37816df2012-05-16 18:45:18 +01003066 seat->compositor = ec;
3067 seat->hotspot_x = 16;
3068 seat->hotspot_y = 16;
3069 seat->modifier_state = 0;
3070 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05003071
Daniel Stone37816df2012-05-16 18:45:18 +01003072 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003073 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02003074
Daniel Stone37816df2012-05-16 18:45:18 +01003075 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03003076
Daniel Stone37816df2012-05-16 18:45:18 +01003077 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
3078 wl_signal_add(&seat->seat.drag_icon_signal,
3079 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04003080
3081 clipboard_create(seat);
Jan Arne Petersene829adc2012-08-10 16:47:22 +02003082 input_method_create(ec, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05003083}
3084
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003085WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01003086weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003087{
Daniel Stone37816df2012-05-16 18:45:18 +01003088 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003089 /* The global object is destroyed at wl_display_destroy() time. */
3090
Daniel Stone37816df2012-05-16 18:45:18 +01003091 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003092 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003093
Daniel Stone74419a22012-05-30 16:32:02 +01003094 if (seat->xkb_state.state != NULL)
3095 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01003096 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01003097
Daniel Stone37816df2012-05-16 18:45:18 +01003098 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003099}
3100
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003101static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003102drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
3103{
3104 weston_surface_configure(es,
3105 es->geometry.x + sx, es->geometry.y + sy,
3106 es->buffer->width, es->buffer->height);
3107}
3108
3109static int
Daniel Stone37816df2012-05-16 18:45:18 +01003110device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003111 struct weston_surface *surface)
3112{
Daniel Stone37816df2012-05-16 18:45:18 +01003113 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003114
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003115 if (surface->configure) {
3116 wl_resource_post_error(&surface->surface.resource,
3117 WL_DISPLAY_ERROR_INVALID_OBJECT,
3118 "surface->configure already set");
3119 return 0;
3120 }
3121
Daniel Stone37816df2012-05-16 18:45:18 +01003122 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003123
Daniel Stone37816df2012-05-16 18:45:18 +01003124 weston_surface_set_position(ws->drag_surface,
3125 wl_fixed_to_double(seat->pointer->x),
3126 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003127
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003128 surface->configure = drag_surface_configure;
3129
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003130 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01003131 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003132
3133 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003134}
3135
3136static void
Daniel Stone37816df2012-05-16 18:45:18 +01003137device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003138{
Daniel Stone37816df2012-05-16 18:45:18 +01003139 seat->drag_surface->configure = NULL;
3140 undef_region(&seat->drag_surface->input);
3141 wl_list_remove(&seat->drag_surface_destroy_listener.link);
3142 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003143}
3144
3145static void
Daniel Stone37816df2012-05-16 18:45:18 +01003146device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003147{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003148 struct wl_list *list;
3149
Daniel Stone37816df2012-05-16 18:45:18 +01003150 if (weston_surface_is_mapped(seat->drag_surface) ||
3151 !seat->drag_surface->buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003152 return;
3153
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003154 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
3155 list = &seat->sprite->layer_link;
3156 else
3157 list = &seat->compositor->cursor_layer.surface_list;
3158
3159 wl_list_insert(list, &seat->drag_surface->layer_link);
Daniel Stone37816df2012-05-16 18:45:18 +01003160 weston_surface_assign_output(seat->drag_surface);
3161 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003162}
3163
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003164static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003165weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01003166 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003167{
3168 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003169
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003170 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003171 return;
3172
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003173 if (seat->drag_surface && seat->seat.drag_surface &&
3174 (&seat->drag_surface->surface.resource !=
3175 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003176 /* between calls to this funcion we got a new drag_surface */
3177 surface_changed = 1;
3178
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003179 if (!seat->seat.drag_surface || surface_changed) {
3180 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003181 if (!surface_changed)
3182 return;
3183 }
3184
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003185 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003186 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003187 seat->seat.drag_surface;
3188 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003189 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003190 }
3191
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003192 /* the client may not have attached a buffer to the drag surface
3193 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003194 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003195
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02003196 /* the client may have attached a buffer with a different size to
3197 * the drag surface, causing the input region to be reset */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003198 if (region_is_undefined(&seat->drag_surface->input))
3199 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02003200
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003201 if (!dx && !dy)
3202 return;
3203
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003204 weston_surface_set_position(seat->drag_surface,
3205 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
3206 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003207}
3208
3209WL_EXPORT void
3210weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
3211{
Daniel Stone4dab5db2012-05-30 16:31:53 +01003212 struct weston_seat *seat;
3213
3214 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003215 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003216}
3217
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003218static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003219bind_output(struct wl_client *client,
3220 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003221{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003222 struct weston_output *output = data;
3223 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003224 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003225
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003226 resource = wl_client_add_object(client,
3227 &wl_output_interface, NULL, id, data);
3228
Casey Dahlin9074db52012-04-19 22:50:09 -04003229 wl_list_insert(&output->resource_list, &resource->link);
3230 resource->destroy = unbind_resource;
3231
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003232 wl_output_send_geometry(resource,
3233 output->x,
3234 output->y,
3235 output->mm_width,
3236 output->mm_height,
3237 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003238 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003239 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003240
3241 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003242 wl_output_send_mode(resource,
3243 mode->flags,
3244 mode->width,
3245 mode->height,
3246 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003247 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003248}
3249
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003250static const char vertex_shader[] =
3251 "uniform mat4 proj;\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05003252 "attribute vec2 position;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003253 "attribute vec2 texcoord;\n"
3254 "varying vec2 v_texcoord;\n"
3255 "void main()\n"
3256 "{\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05003257 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003258 " v_texcoord = texcoord;\n"
3259 "}\n";
3260
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02003261/* Declare common fragment shader uniforms */
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003262#define FRAGMENT_CONVERT_YUV \
Rob Clark0e5a2d02012-08-30 16:47:18 -05003263 " y *= alpha;\n" \
3264 " u *= alpha;\n" \
3265 " v *= alpha;\n" \
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003266 " gl_FragColor.r = y + 1.59602678 * v;\n" \
3267 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
3268 " gl_FragColor.b = y + 2.01723214 * u;\n" \
Rob Clark0e5a2d02012-08-30 16:47:18 -05003269 " gl_FragColor.a = alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003270
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02003271static const char texture_fragment_shader_rgba[] =
Kristian Høgsbergdfce71d2010-12-07 20:19:10 -05003272 "precision mediump float;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003273 "varying vec2 v_texcoord;\n"
3274 "uniform sampler2D tex;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003275 "uniform float alpha;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003276 "void main()\n"
3277 "{\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003278 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003279 "}\n";
3280
Pekka Paalanen6b5585b2012-08-30 16:47:19 -05003281static const char texture_fragment_shader_rgbx[] =
3282 "precision mediump float;\n"
3283 "varying vec2 v_texcoord;\n"
3284 "uniform sampler2D tex;\n"
3285 "uniform float alpha;\n"
3286 "void main()\n"
3287 "{\n"
3288 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
3289 " gl_FragColor.a = alpha;\n"
3290 "}\n";
3291
Rob Clarke3b95912012-08-31 16:42:18 -05003292static const char texture_fragment_shader_egl_external[] =
3293 "#extension GL_OES_EGL_image_external : require\n"
3294 "precision mediump float;\n"
3295 "varying vec2 v_texcoord;\n"
3296 "uniform samplerExternalOES tex;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003297 "uniform float alpha;\n"
Rob Clarke3b95912012-08-31 16:42:18 -05003298 "void main()\n"
3299 "{\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003300 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Rob Clarke3b95912012-08-31 16:42:18 -05003301 "}\n";
3302
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003303static const char texture_fragment_shader_y_uv[] =
3304 "precision mediump float;\n"
3305 "uniform sampler2D tex;\n"
3306 "uniform sampler2D tex1;\n"
3307 "varying vec2 v_texcoord;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003308 "uniform float alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003309 "void main() {\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003310 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
3311 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
3312 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
3313 FRAGMENT_CONVERT_YUV
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003314 "}\n";
3315
3316static const char texture_fragment_shader_y_u_v[] =
3317 "precision mediump float;\n"
3318 "uniform sampler2D tex;\n"
3319 "uniform sampler2D tex1;\n"
3320 "uniform sampler2D tex2;\n"
3321 "varying vec2 v_texcoord;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003322 "uniform float alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003323 "void main() {\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003324 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
3325 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
3326 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
3327 FRAGMENT_CONVERT_YUV
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003328 "}\n";
3329
3330static const char texture_fragment_shader_y_xuxv[] =
3331 "precision mediump float;\n"
3332 "uniform sampler2D tex;\n"
3333 "uniform sampler2D tex1;\n"
3334 "varying vec2 v_texcoord;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003335 "uniform float alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003336 "void main() {\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003337 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
3338 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
3339 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
3340 FRAGMENT_CONVERT_YUV
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003341 "}\n";
3342
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003343static const char solid_fragment_shader[] =
3344 "precision mediump float;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003345 "uniform vec4 color;\n"
Kristian Høgsbergdbae80e2012-03-29 11:34:39 -04003346 "uniform float alpha;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003347 "void main()\n"
3348 "{\n"
Kristian Høgsbergdbae80e2012-03-29 11:34:39 -04003349 " gl_FragColor = alpha * color\n;"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003350 "}\n";
3351
Kristian Høgsberga9468212010-06-14 21:03:11 -04003352static int
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003353compile_shader(GLenum type, const char *source)
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003354{
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003355 GLuint s;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003356 char msg[512];
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003357 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003358
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003359 s = glCreateShader(type);
3360 glShaderSource(s, 1, &source, NULL);
3361 glCompileShader(s);
3362 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003363 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003364 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
Martin Minarik6d118362012-06-07 18:01:59 +02003365 weston_log("shader info: %s\n", msg);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003366 return GL_NONE;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003367 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003368
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003369 return s;
3370}
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003371
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003372static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003373weston_shader_init(struct weston_shader *shader,
3374 const char *vertex_source, const char *fragment_source)
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003375{
3376 char msg[512];
3377 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003378
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003379 shader->vertex_shader =
3380 compile_shader(GL_VERTEX_SHADER, vertex_source);
3381 shader->fragment_shader =
3382 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
3383
3384 shader->program = glCreateProgram();
3385 glAttachShader(shader->program, shader->vertex_shader);
3386 glAttachShader(shader->program, shader->fragment_shader);
3387 glBindAttribLocation(shader->program, 0, "position");
3388 glBindAttribLocation(shader->program, 1, "texcoord");
3389
3390 glLinkProgram(shader->program);
3391 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003392 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003393 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
Martin Minarik6d118362012-06-07 18:01:59 +02003394 weston_log("link info: %s\n", msg);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003395 return -1;
3396 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003397
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003398 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02003399 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
3400 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
3401 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
Kristian Høgsberg541e5552011-12-14 09:24:11 -05003402 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
Pekka Paalanenf55f5442012-02-02 16:49:05 +02003403 shader->color_uniform = glGetUniformLocation(shader->program, "color");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003404
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003405 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003406}
3407
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003408WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003409weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003410{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003411 struct weston_compositor *c = output->compositor;
3412
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003413 pixman_region32_fini(&output->region);
Kristian Høgsberg8b72f602011-06-23 20:46:34 -04003414 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003415 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003416
3417 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003418}
3419
Scott Moreau1bad5db2012-08-18 01:04:05 -06003420static void
3421weston_output_compute_transform(struct weston_output *output)
3422{
3423 struct weston_matrix transform;
3424 int flip;
3425
3426 weston_matrix_init(&transform);
3427
3428 switch(output->transform) {
3429 case WL_OUTPUT_TRANSFORM_FLIPPED:
3430 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3431 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3432 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3433 flip = -1;
3434 break;
3435 default:
3436 flip = 1;
3437 break;
3438 }
3439
3440 switch(output->transform) {
3441 case WL_OUTPUT_TRANSFORM_NORMAL:
3442 case WL_OUTPUT_TRANSFORM_FLIPPED:
3443 transform.d[0] = flip;
3444 transform.d[1] = 0;
3445 transform.d[4] = 0;
3446 transform.d[5] = 1;
3447 break;
3448 case WL_OUTPUT_TRANSFORM_90:
3449 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3450 transform.d[0] = 0;
3451 transform.d[1] = -flip;
3452 transform.d[4] = 1;
3453 transform.d[5] = 0;
3454 break;
3455 case WL_OUTPUT_TRANSFORM_180:
3456 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3457 transform.d[0] = -flip;
3458 transform.d[1] = 0;
3459 transform.d[4] = 0;
3460 transform.d[5] = -1;
3461 break;
3462 case WL_OUTPUT_TRANSFORM_270:
3463 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3464 transform.d[0] = 0;
3465 transform.d[1] = flip;
3466 transform.d[4] = -1;
3467 transform.d[5] = 0;
3468 break;
3469 default:
3470 break;
3471 }
3472
3473 weston_matrix_multiply(&output->matrix, &transform);
3474}
3475
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003476WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003477weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003478{
Scott Moreau850ca422012-05-21 15:21:25 -06003479 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003480 struct weston_matrix camera;
3481 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003482
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003483 weston_matrix_init(&output->matrix);
3484 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003485 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
3486 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003487
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003488 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003489 2.0 / (output->width + output->border.left + output->border.right),
3490 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
3491
3492 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003493
Scott Moreauccbf29d2012-02-22 14:21:41 -07003494 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003495 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003496 weston_matrix_init(&camera);
3497 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06003498 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06003499 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003500 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003501 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003502 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003503 weston_matrix_multiply(&output->matrix, &modelview);
3504 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003505
Scott Moreauccbf29d2012-02-22 14:21:41 -07003506 output->dirty = 0;
3507}
3508
Scott Moreau1bad5db2012-08-18 01:04:05 -06003509static void
3510weston_output_transform_init(struct weston_output *output, uint32_t transform)
3511{
3512 output->transform = transform;
3513
3514 switch (transform) {
3515 case WL_OUTPUT_TRANSFORM_90:
3516 case WL_OUTPUT_TRANSFORM_270:
3517 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3518 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3519 /* Swap width and height */
3520 output->width = output->current->height;
3521 output->height = output->current->width;
3522 break;
3523 case WL_OUTPUT_TRANSFORM_NORMAL:
3524 case WL_OUTPUT_TRANSFORM_180:
3525 case WL_OUTPUT_TRANSFORM_FLIPPED:
3526 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3527 output->width = output->current->width;
3528 output->height = output->current->height;
3529 break;
3530 default:
3531 break;
3532 }
3533}
3534
Scott Moreauccbf29d2012-02-22 14:21:41 -07003535WL_EXPORT void
3536weston_output_move(struct weston_output *output, int x, int y)
3537{
3538 output->x = x;
3539 output->y = y;
3540
3541 pixman_region32_init(&output->previous_damage);
3542 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003543 output->width,
3544 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003545}
3546
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003547WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003548weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003549 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003550{
3551 output->compositor = c;
3552 output->x = x;
3553 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05003554 output->border.top = 0;
3555 output->border.bottom = 0;
3556 output->border.left = 0;
3557 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003558 output->mm_width = width;
3559 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003560 output->dirty = 1;
3561
Scott Moreau1bad5db2012-08-18 01:04:05 -06003562 if (transform != WL_OUTPUT_TRANSFORM_NORMAL)
3563 output->disable_planes++;
3564
3565 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06003566 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003567
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003568 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003569 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003570
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003571 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003572 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003573 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003574
Casey Dahlin58ba1372012-04-19 22:50:08 -04003575 output->id = ffs(~output->compositor->output_id_pool) - 1;
3576 output->compositor->output_id_pool |= 1 << output->id;
3577
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003578 output->global =
3579 wl_display_add_global(c->wl_display, &wl_output_interface,
3580 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003581}
3582
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003583static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003584compositor_bind(struct wl_client *client,
3585 void *data, uint32_t version, uint32_t id)
3586{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003587 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003588
3589 wl_client_add_object(client, &wl_compositor_interface,
3590 &compositor_interface, id, compositor);
3591}
3592
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003593static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003594log_uname(void)
3595{
3596 struct utsname usys;
3597
3598 uname(&usys);
3599
3600 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3601 usys.version, usys.machine);
3602}
3603
3604static void
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003605log_extensions(const char *name, const char *extensions)
3606{
3607 const char *p, *end;
3608 int l;
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003609 int len;
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003610
3611 l = weston_log("%s:", name);
3612 p = extensions;
3613 while (*p) {
3614 end = strchrnul(p, ' ');
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003615 len = end - p;
3616 if (l + len > 78)
Pekka Paalanen4e4167d2012-06-11 14:06:05 +03003617 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003618 len, p);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003619 else
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003620 l += weston_log_continue(" %.*s", len, p);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003621 for (p = end; isspace(*p); p++)
3622 ;
3623 }
3624 weston_log_continue("\n");
3625}
3626
Pekka Paalanen4e4167d2012-06-11 14:06:05 +03003627static void
3628log_egl_gl_info(EGLDisplay egldpy)
3629{
3630 const char *str;
3631
3632 str = eglQueryString(egldpy, EGL_VERSION);
3633 weston_log("EGL version: %s\n", str ? str : "(null)");
3634
3635 str = eglQueryString(egldpy, EGL_VENDOR);
3636 weston_log("EGL vendor: %s\n", str ? str : "(null)");
3637
3638 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
3639 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
3640
3641 str = eglQueryString(egldpy, EGL_EXTENSIONS);
3642 log_extensions("EGL extensions", str ? str : "(null)");
3643
3644 str = (char *)glGetString(GL_VERSION);
3645 weston_log("GL version: %s\n", str ? str : "(null)");
3646
3647 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
3648 weston_log("GLSL version: %s\n", str ? str : "(null)");
3649
3650 str = (char *)glGetString(GL_VENDOR);
3651 weston_log("GL vendor: %s\n", str ? str : "(null)");
3652
3653 str = (char *)glGetString(GL_RENDERER);
3654 weston_log("GL renderer: %s\n", str ? str : "(null)");
3655
3656 str = (char *)glGetString(GL_EXTENSIONS);
3657 log_extensions("GL extensions", str ? str : "(null)");
3658}
3659
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003660WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003661weston_compositor_init(struct weston_compositor *ec,
3662 struct wl_display *display,
3663 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003664 char *argv[],
3665 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003666{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003667 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003668 struct xkb_rule_names xkb_names;
3669 const struct config_key keyboard_config_keys[] = {
3670 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
3671 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
3672 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
3673 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
3674 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
3675 };
3676 const struct config_section cs[] = {
3677 { "keyboard",
3678 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
3679 };
3680
3681 memset(&xkb_names, 0, sizeof(xkb_names));
3682 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003683
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003684 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003685 wl_signal_init(&ec->destroy_signal);
3686 wl_signal_init(&ec->activate_signal);
3687 wl_signal_init(&ec->lock_signal);
3688 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003689 wl_signal_init(&ec->show_input_panel_signal);
3690 wl_signal_init(&ec->hide_input_panel_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01003691 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003692
Casey Dahlin58ba1372012-04-19 22:50:08 -04003693 ec->output_id_pool = 0;
3694
Kristian Høgsberga8873122011-11-23 10:39:34 -05003695 if (!wl_display_add_global(display, &wl_compositor_interface,
3696 ec, compositor_bind))
3697 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003698
Daniel Stone725c2c32012-06-22 14:04:36 +01003699 wl_list_init(&ec->surface_list);
3700 wl_list_init(&ec->layer_list);
3701 wl_list_init(&ec->seat_list);
3702 wl_list_init(&ec->output_list);
3703 wl_list_init(&ec->key_binding_list);
3704 wl_list_init(&ec->button_binding_list);
3705 wl_list_init(&ec->axis_binding_list);
3706 wl_list_init(&ec->fade.animation.link);
3707
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003708 weston_plane_init(&ec->primary_plane, 0, 0);
3709
Daniel Stone725c2c32012-06-22 14:04:36 +01003710 weston_compositor_xkb_init(ec, &xkb_names);
3711
3712 ec->ping_handler = NULL;
3713
3714 screenshooter_create(ec);
3715 text_cursor_position_notifier_create(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003716
3717 wl_data_device_manager_init(ec->wl_display);
3718
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003719 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003720
Daniel Stone725c2c32012-06-22 14:04:36 +01003721 loop = wl_display_get_event_loop(ec->wl_display);
3722 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3723 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3724
3725 ec->input_loop = wl_event_loop_create();
3726
3727 return 0;
3728}
3729
3730WL_EXPORT int
3731weston_compositor_init_gl(struct weston_compositor *ec)
3732{
3733 const char *extensions;
Rob Clarke3b95912012-08-31 16:42:18 -05003734 int has_egl_image_external = 0;
Daniel Stone725c2c32012-06-22 14:04:36 +01003735
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003736 log_egl_gl_info(ec->egl_display);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003737
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003738 ec->image_target_texture_2d =
3739 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3740 ec->image_target_renderbuffer_storage = (void *)
3741 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
3742 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3743 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3744 ec->bind_display =
3745 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
3746 ec->unbind_display =
3747 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02003748 ec->query_buffer =
3749 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003750
3751 extensions = (const char *) glGetString(GL_EXTENSIONS);
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003752 if (!extensions) {
Martin Minarik6d118362012-06-07 18:01:59 +02003753 weston_log("Retrieving GL extension string failed.\n");
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003754 return -1;
3755 }
3756
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003757 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
Martin Minarik6d118362012-06-07 18:01:59 +02003758 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003759 return -1;
3760 }
3761
Pekka Paalanena1d57db2012-04-17 15:02:08 +03003762 if (strstr(extensions, "GL_EXT_read_format_bgra"))
3763 ec->read_format = GL_BGRA_EXT;
3764 else
3765 ec->read_format = GL_RGBA;
Kristian Høgsbergf9247dd2012-03-27 15:25:46 -04003766
Pekka Paalanen52bfbaa2012-04-11 16:19:37 +03003767 if (strstr(extensions, "GL_EXT_unpack_subimage"))
3768 ec->has_unpack_subimage = 1;
Kristian Høgsbergfb6de222012-03-27 17:10:13 -04003769
Rob Clarke3b95912012-08-31 16:42:18 -05003770 if (strstr(extensions, "GL_OES_EGL_image_external"))
3771 has_egl_image_external = 1;
3772
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003773 extensions =
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003774 (const char *) eglQueryString(ec->egl_display, EGL_EXTENSIONS);
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003775 if (!extensions) {
Martin Minarik6d118362012-06-07 18:01:59 +02003776 weston_log("Retrieving EGL extension string failed.\n");
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003777 return -1;
3778 }
3779
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003780 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
3781 ec->has_bind_display = 1;
3782 if (ec->has_bind_display)
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003783 ec->bind_display(ec->egl_display, ec->wl_display);
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04003784
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003785 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003786 ec->fade.animation.frame = fade_frame;
Kristian Høgsberg3555d092011-04-11 13:58:13 -04003787
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003788 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3789 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3790
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003791 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003792
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02003793 if (weston_shader_init(&ec->texture_shader_rgba,
3794 vertex_shader, texture_fragment_shader_rgba) < 0)
Kristian Høgsberga9468212010-06-14 21:03:11 -04003795 return -1;
Pekka Paalanen6b5585b2012-08-30 16:47:19 -05003796 if (weston_shader_init(&ec->texture_shader_rgbx,
3797 vertex_shader, texture_fragment_shader_rgbx) < 0)
3798 return -1;
Rob Clarke3b95912012-08-31 16:42:18 -05003799 if (has_egl_image_external &&
3800 weston_shader_init(&ec->texture_shader_egl_external,
3801 vertex_shader, texture_fragment_shader_egl_external) < 0)
3802 return -1;
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003803 if (weston_shader_init(&ec->texture_shader_y_uv,
3804 vertex_shader, texture_fragment_shader_y_uv) < 0)
3805 return -1;
3806 if (weston_shader_init(&ec->texture_shader_y_u_v,
3807 vertex_shader, texture_fragment_shader_y_u_v) < 0)
3808 return -1;
3809 if (weston_shader_init(&ec->texture_shader_y_xuxv,
3810 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
3811 return -1;
Pekka Paalanenf55f5442012-02-02 16:49:05 +02003812 if (weston_shader_init(&ec->solid_shader,
3813 vertex_shader, solid_fragment_shader) < 0)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003814 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05003815
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003816 weston_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003817
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003818 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003819}
3820
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003821WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003822weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003823{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003824 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003825
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003826 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003827 if (ec->input_loop_source)
3828 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003829
Matt Roper361d2ad2011-08-29 13:52:23 -07003830 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003831 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003832 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003833
Daniel Stone325fc2d2012-05-30 16:31:58 +01003834 weston_binding_list_destroy_all(&ec->key_binding_list);
3835 weston_binding_list_destroy_all(&ec->button_binding_list);
3836 weston_binding_list_destroy_all(&ec->axis_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003837
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003838 weston_plane_release(&ec->primary_plane);
3839
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003840 wl_array_release(&ec->vertices);
3841 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05003842 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003843
3844 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07003845}
3846
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003847static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003848{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003849 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003850
Martin Minarik6d118362012-06-07 18:01:59 +02003851 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003852 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003853
3854 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003855}
3856
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003857static void
3858on_segv_signal(int s, siginfo_t *siginfo, void *context)
3859{
3860 void *buffer[32];
3861 int i, count;
3862 Dl_info info;
3863
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003864 /* This SIGSEGV handler will do a best-effort backtrace, and
3865 * then call the backend restore function, which will switch
3866 * back to the vt we launched from or ungrab X etc and then
3867 * raise SIGTRAP. If we run weston under gdb from X or a
3868 * different vt, and tell gdb "handle SIGSEGV nostop", this
3869 * will allow weston to switch back to gdb on crash and then
3870 * gdb will catch the crash with SIGTRAP. */
3871
Martin Minarik6d118362012-06-07 18:01:59 +02003872 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003873
3874 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3875 for (i = 0; i < count; i++) {
3876 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02003877 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003878 (long) buffer[i],
3879 info.dli_sname ? info.dli_sname : "--",
3880 info.dli_fname);
3881 }
3882
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003883 segv_compositor->restore(segv_compositor);
3884
3885 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003886}
3887
3888
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003889static void *
3890load_module(const char *name, const char *entrypoint, void **handle)
3891{
3892 char path[PATH_MAX];
3893 void *module, *init;
3894
3895 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003896 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003897 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003898 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003899
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003900 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003901 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003902 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003903 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003904 return NULL;
3905 }
3906
3907 init = dlsym(module, entrypoint);
3908 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003909 weston_log("Failed to lookup init function: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003910 return NULL;
3911 }
3912
3913 return init;
3914}
3915
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003916static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003917 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3918
3919static const char xdg_wrong_message[] =
3920 "fatal: environment variable XDG_RUNTIME_DIR\n"
3921 "is set to \"%s\", which is not a directory.\n";
3922
3923static const char xdg_wrong_mode_message[] =
3924 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3925 "correctly. Unix access mode must be 0700 but is %o,\n"
3926 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003927 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003928
3929static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003930 "Refer to your distribution on how to get it, or\n"
3931 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3932 "on how to implement it.\n";
3933
Martin Minarik37032f82012-06-18 20:15:18 +02003934static void
3935verify_xdg_runtime_dir(void)
3936{
3937 char *dir = getenv("XDG_RUNTIME_DIR");
3938 struct stat s;
3939
3940 if (!dir) {
3941 weston_log(xdg_error_message);
3942 weston_log_continue(xdg_detail_message);
3943 exit(EXIT_FAILURE);
3944 }
3945
3946 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3947 weston_log(xdg_wrong_message, dir);
3948 weston_log_continue(xdg_detail_message);
3949 exit(EXIT_FAILURE);
3950 }
3951
3952 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3953 weston_log(xdg_wrong_mode_message,
3954 dir, s.st_mode & 0777, s.st_uid);
3955 weston_log_continue(xdg_detail_message);
3956 }
3957}
3958
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003959static int
3960usage(int error_code)
3961{
3962 fprintf(stderr,
3963 "Usage: weston [OPTIONS]\n\n"
3964 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3965 "Weston supports multiple backends, and depending on which backend is in use\n"
3966 "different options will be accepted.\n\n"
3967
3968
3969 "Core options:\n\n"
3970 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3971 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3972 " -S, --socket=NAME\tName of socket to listen on\n"
3973 " -i, --idle-time=SECS\tIdle time in seconds\n"
3974 " --xserver\t\tEnable X server integration\n"
3975 " --module\t\tLoad the specified module\n"
3976 " --log==FILE\t\tLog to the given file\n"
3977 " -h, --help\t\tThis help message\n\n");
3978
3979 fprintf(stderr,
3980 "Options for drm-backend.so:\n\n"
3981 " --connector=ID\tBring up only this connector\n"
3982 " --seat=SEAT\t\tThe seat that weston should run on\n"
3983 " --tty=TTY\t\tThe tty to use\n"
3984 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3985
3986 fprintf(stderr,
3987 "Options for x11-backend.so:\n\n"
3988 " --width=WIDTH\t\tWidth of X window\n"
3989 " --height=HEIGHT\tHeight of X window\n"
3990 " --fullscreen\t\tRun in fullscreen mode\n"
3991 " --output-count=COUNT\tCreate multiple outputs\n"
3992 " --no-input\t\tDont create input devices\n\n");
3993
3994 fprintf(stderr,
3995 "Options for wayland-backend.so:\n\n"
3996 " --width=WIDTH\t\tWidth of Wayland surface\n"
3997 " --height=HEIGHT\tHeight of Wayland surface\n"
3998 " --display=DISPLAY\tWayland display to connect to\n\n");
3999
4000 exit(error_code);
4001}
4002
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004003int main(int argc, char *argv[])
4004{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004005 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004006 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004007 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004008 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004009 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004010 struct sigaction segv_action;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04004011 void *shell_module, *backend_module, *xserver_module;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004012 int (*module_init)(struct weston_compositor *ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004013 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004014 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04004015 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004016 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004017 char *backend = NULL;
4018 char *shell = NULL;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004019 char *module = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004020 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004021 int32_t idle_time = 300;
Scott Moreaue17f6102012-04-25 10:03:06 -06004022 int32_t xserver = 0;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004023 int32_t help = 0;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004024 char *socket_name = NULL;
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004025 char *config_file;
4026
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004027 const struct config_key shell_config_keys[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004028 { "type", CONFIG_KEY_STRING, &shell },
4029 };
4030
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004031 const struct config_section cs[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004032 { "shell",
4033 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
4034 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004035
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004036 const struct weston_option core_options[] = {
4037 { WESTON_OPTION_STRING, "backend", 'B', &backend },
4038 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4039 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004040 { WESTON_OPTION_BOOLEAN, "xserver", 0, &xserver },
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004041 { WESTON_OPTION_STRING, "module", 0, &module },
Martin Minarik19e6f262012-06-07 13:08:46 +02004042 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004043 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004044 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004045
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004046 argc = parse_options(core_options,
4047 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004048
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004049 if (help)
4050 usage(EXIT_SUCCESS);
4051
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004052 weston_log_file_open(log);
4053
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004054 weston_log("%s\n"
4055 STAMP_SPACE "%s\n"
4056 STAMP_SPACE "Bug reports to: %s\n"
4057 STAMP_SPACE "Build: %s\n",
4058 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004059 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004060 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004061
Martin Minarik37032f82012-06-18 20:15:18 +02004062 verify_xdg_runtime_dir();
4063
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004064 display = wl_display_create();
4065
Tiago Vignatti2116b892011-08-08 05:52:59 -07004066 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004067 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4068 display);
4069 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4070 display);
4071 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4072 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004073
4074 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004075 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4076 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004077
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004078 if (!backend) {
4079 if (getenv("WAYLAND_DISPLAY"))
4080 backend = "wayland-backend.so";
4081 else if (getenv("DISPLAY"))
4082 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004083 else
4084 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004085 }
4086
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004087 config_file = config_file_path("weston.ini");
4088 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004089
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004090 backend_init = load_module(backend, "backend_init", &backend_module);
4091 if (!backend_init)
4092 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004093
Daniel Stonec1be8e52012-06-01 11:14:02 -04004094 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004095 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004096 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004097 exit(EXIT_FAILURE);
4098 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004099
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004100 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4101 segv_action.sa_sigaction = on_segv_signal;
4102 sigemptyset(&segv_action.sa_mask);
4103 sigaction(SIGSEGV, &segv_action, NULL);
4104 segv_compositor = ec;
4105
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004106 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03004107 weston_log("fatal: unhandled option: %s\n", argv[i]);
4108 if (argv[1]) {
4109 ret = EXIT_FAILURE;
4110 goto out;
4111 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004112
Daniel Stonec1be8e52012-06-01 11:14:02 -04004113 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01004114
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004115 ec->option_idle_time = idle_time;
4116 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004117
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004118 module_init = NULL;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04004119 if (xserver)
Tiago Vignatti629ce232012-05-23 23:04:14 +03004120 module_init = load_module("xwayland.so",
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004121 "weston_xserver_init",
4122 &xserver_module);
Pekka Paalanen33616392012-07-30 16:56:57 +03004123 if (module_init && module_init(ec) < 0) {
4124 ret = EXIT_FAILURE;
4125 goto out;
4126 }
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04004127
Kristian Høgsberg33d75092012-08-13 13:56:03 -04004128 if (socket_name)
4129 setenv("WAYLAND_DISPLAY", socket_name, 1);
4130
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004131 if (!shell)
4132 shell = "desktop-shell.so";
4133 module_init = load_module(shell, "shell_init", &shell_module);
Pekka Paalanen33616392012-07-30 16:56:57 +03004134 if (!module_init || module_init(ec) < 0) {
4135 ret = EXIT_FAILURE;
4136 goto out;
4137 }
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004138
4139
4140 module_init = NULL;
4141 if (module)
4142 module_init = load_module(module, "module_init", NULL);
Pekka Paalanen33616392012-07-30 16:56:57 +03004143 if (module_init && module_init(ec) < 0) {
4144 ret = EXIT_FAILURE;
4145 goto out;
4146 }
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004147
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004148 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004149 weston_log("fatal: failed to add socket: %m\n");
4150 ret = EXIT_FAILURE;
4151 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004152 }
4153
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004154 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004155 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004156
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004157 wl_display_run(display);
4158
4159 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004160 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05004161 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004162
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004163 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004164
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004165 if (ec->has_bind_display)
Kristian Høgsberg362b6722012-06-18 15:13:51 -04004166 ec->unbind_display(ec->egl_display, display);
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004167
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004168 for (i = ARRAY_LENGTH(signals); i;)
4169 wl_event_source_remove(signals[--i]);
4170
Daniel Stone855028f2012-05-01 20:37:10 +01004171 weston_compositor_xkb_destroy(ec);
4172
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004173 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004174 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004175
Martin Minarik19e6f262012-06-07 13:08:46 +02004176 weston_log_file_close();
4177
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004178 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004179}