blob: 52d81647a737382e94a09c88e8ab0d7cd4141601 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanend581a8f2012-01-27 16:25:16 +02004 * Copyright © 2012 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04006 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050015 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040016 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Kristian Høgsberg06bc2642010-12-01 09:50:16 -050025#define _GNU_SOURCE
26
Kristian Høgsberga9410222011-01-14 17:22:35 -050027#include "config.h"
28
Daniel Stoneb7452fe2012-06-01 12:14:06 +010029#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040030#include <stdio.h>
31#include <string.h>
32#include <stdlib.h>
33#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010034#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050035#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020036#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040037#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010038#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040039#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020040#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020041#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020042#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040043#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050044#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040045#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040046#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040047#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050048#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040049#include <sys/time.h>
50#include <time.h>
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -040051#include <ctype.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050052
Pekka Paalanen50719bc2011-11-22 14:18:50 +020053#include <wayland-server.h>
Kristian Høgsberg82863022010-06-04 21:52:02 -040054#include "compositor.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030055#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040056#include "git-version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050057
Kristian Høgsberg27da5382011-06-21 17:32:25 -040058static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040059static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040060
61static int
62sigchld_handler(int signal_number, void *data)
63{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050064 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040065 int status;
66 pid_t pid;
67
Bill Spitzak027b9622012-03-17 15:22:03 -070068 pid = waitpid(-1, &status, WNOHANG);
69 if (!pid)
70 return 1;
71
Kristian Høgsberg27da5382011-06-21 17:32:25 -040072 wl_list_for_each(p, &child_process_list, link) {
73 if (p->pid == pid)
74 break;
75 }
76
77 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020078 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040079 return 1;
80 }
81
82 wl_list_remove(&p->link);
83 p->cleanup(p, status);
84
85 return 1;
86}
87
Alex Wu2dda6042012-04-17 17:20:47 +080088WL_EXPORT int
89weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode)
90{
91 if (!output->switch_mode)
92 return -1;
93
94 return output->switch_mode(output, mode);
95}
96
Kristian Høgsberg27da5382011-06-21 17:32:25 -040097WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050098weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -040099{
100 wl_list_insert(&child_process_list, &process->link);
101}
102
Benjamin Franzke06286262011-05-06 19:12:33 +0200103static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200104child_client_exec(int sockfd, const char *path)
105{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500106 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200107 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200108 sigset_t allsigs;
109
110 /* do not give our signal mask to the new process */
111 sigfillset(&allsigs);
112 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200113
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500114 /* Launch clients as the user. */
115 seteuid(getuid());
116
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500117 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
118 * non-CLOEXEC fd to pass through exec. */
119 clientfd = dup(sockfd);
120 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200121 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500122 return;
123 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200124
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500125 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200126 setenv("WAYLAND_SOCKET", s, 1);
127
128 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200129 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200130 path);
131}
132
133WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500134weston_client_launch(struct weston_compositor *compositor,
135 struct weston_process *proc,
136 const char *path,
137 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200138{
139 int sv[2];
140 pid_t pid;
141 struct wl_client *client;
142
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300143 weston_log("launching '%s'\n", path);
144
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300145 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200146 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200147 "socketpair failed while launching '%s': %m\n",
148 path);
149 return NULL;
150 }
151
152 pid = fork();
153 if (pid == -1) {
154 close(sv[0]);
155 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200156 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200157 "fork failed while launching '%s': %m\n", path);
158 return NULL;
159 }
160
161 if (pid == 0) {
162 child_client_exec(sv[1], path);
163 exit(-1);
164 }
165
166 close(sv[1]);
167
168 client = wl_client_create(compositor->wl_display, sv[0]);
169 if (!client) {
170 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200171 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200172 "wl_client_create failed while launching '%s'.\n",
173 path);
174 return NULL;
175 }
176
177 proc->pid = pid;
178 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500179 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200180
181 return client;
182}
183
184static void
Kristian Høgsberga85b4fb2012-06-20 09:24:35 -0400185update_shm_texture(struct weston_surface *surface);
186
187static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400188surface_handle_buffer_destroy(struct wl_listener *listener, void *data)
Benjamin Franzke06286262011-05-06 19:12:33 +0200189{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500190 struct weston_surface *es =
191 container_of(listener, struct weston_surface,
192 buffer_destroy_listener);
Benjamin Franzke06286262011-05-06 19:12:33 +0200193
Kristian Høgsberga85b4fb2012-06-20 09:24:35 -0400194 if (es->buffer && wl_buffer_is_shm(es->buffer))
195 update_shm_texture(es);
196
Kristian Høgsberg24596942011-10-24 17:51:02 -0400197 es->buffer = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200198}
199
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500200static const pixman_region32_data_t undef_region_data;
201
202static void
203undef_region(pixman_region32_t *region)
204{
Kristian Høgsberg66a099b2012-05-29 16:49:45 -0400205 if (region->data != &undef_region_data)
206 pixman_region32_fini(region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500207 region->data = (pixman_region32_data_t *) &undef_region_data;
208}
209
210static int
211region_is_undefined(pixman_region32_t *region)
212{
213 return region->data == &undef_region_data;
214}
215
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200216static void
217empty_region(pixman_region32_t *region)
218{
219 if (!region_is_undefined(region))
220 pixman_region32_fini(region);
221
222 pixman_region32_init(region);
223}
224
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500225WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500226weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500227{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500228 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400229
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200230 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400231 if (surface == NULL)
232 return NULL;
233
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400234 wl_signal_init(&surface->surface.resource.destroy_signal);
Kristian Høgsberg48891542012-02-23 17:38:33 -0500235
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500236 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500237 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500238
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400239 surface->surface.resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400240
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500241 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400242 surface->alpha = 1.0;
Kristian Høgsberg0b0c2bb2012-05-15 23:16:53 -0400243 surface->opaque_rect[0] = 0.0;
244 surface->opaque_rect[1] = 0.0;
245 surface->opaque_rect[2] = 0.0;
246 surface->opaque_rect[3] = 0.0;
Juan Zhao46436612012-03-20 01:48:46 +0800247 surface->pitch = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400248
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200249 surface->num_textures = 0;
250 surface->num_images = 0;
251
Benjamin Franzke06286262011-05-06 19:12:33 +0200252 surface->buffer = NULL;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400253 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400254 surface->plane = &compositor->primary_plane;
Benjamin Franzke06286262011-05-06 19:12:33 +0200255
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400256 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500257 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500258 pixman_region32_init(&surface->clip);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500259 undef_region(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200260 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500261 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400262
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400263 surface->buffer_destroy_listener.notify =
264 surface_handle_buffer_destroy;
Benjamin Franzke06286262011-05-06 19:12:33 +0200265
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200266 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200267 wl_list_insert(&surface->geometry.transformation_list,
268 &surface->transform.position.link);
269 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200270 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200271 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500272
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400273 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500274}
275
Alex Wu8811bf92012-02-28 18:07:54 +0800276WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500277weston_surface_set_color(struct weston_surface *surface,
278 GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
279{
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500280 surface->color[0] = red;
281 surface->color[1] = green;
282 surface->color[2] = blue;
283 surface->color[3] = alpha;
284 surface->shader = &surface->compositor->solid_shader;
285}
286
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400287WL_EXPORT void
288weston_surface_to_global_float(struct weston_surface *surface,
289 GLfloat sx, GLfloat sy, GLfloat *x, GLfloat *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200290{
291 if (surface->transform.enabled) {
292 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
293
294 weston_matrix_transform(&surface->transform.matrix, &v);
295
296 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200297 weston_log("warning: numerical instability in "
Pekka Paalanenece8a012012-02-08 15:23:15 +0200298 "weston_surface_to_global(), divisor = %g\n",
299 v.f[3]);
300 *x = 0;
301 *y = 0;
302 return;
303 }
304
305 *x = v.f[0] / v.f[3];
306 *y = v.f[1] / v.f[3];
307 } else {
308 *x = sx + surface->geometry.x;
309 *y = sy + surface->geometry.y;
310 }
311}
312
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500313WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400314weston_surface_move_to_plane(struct weston_surface *surface,
315 struct weston_plane *plane)
316{
317 if (surface->plane == plane)
318 return;
319
320 weston_surface_damage_below(surface);
321 surface->plane = plane;
322 weston_surface_damage(surface);
323}
324
325WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500326weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200327{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500328 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200329
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500330 pixman_region32_init(&damage);
331 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
332 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400333 pixman_region32_union(&surface->plane->damage,
334 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500335 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200336}
337
338static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200339surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
340 int32_t width, int32_t height,
341 pixman_region32_t *bbox)
342{
Pekka Paalanen219b9822012-02-08 15:38:37 +0200343 GLfloat min_x = HUGE_VALF, min_y = HUGE_VALF;
344 GLfloat max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200345 int32_t s[4][2] = {
346 { sx, sy },
347 { sx, sy + height },
348 { sx + width, sy },
349 { sx + width, sy + height }
350 };
Pekka Paalanen219b9822012-02-08 15:38:37 +0200351 GLfloat int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200352 int i;
353
354 for (i = 0; i < 4; ++i) {
Pekka Paalanen219b9822012-02-08 15:38:37 +0200355 GLfloat x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400356 weston_surface_to_global_float(surface,
357 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200358 if (x < min_x)
359 min_x = x;
360 if (x > max_x)
361 max_x = x;
362 if (y < min_y)
363 min_y = y;
364 if (y > max_y)
365 max_y = y;
366 }
367
Pekka Paalanen219b9822012-02-08 15:38:37 +0200368 int_x = floorf(min_x);
369 int_y = floorf(min_y);
370 pixman_region32_init_rect(bbox, int_x, int_y,
371 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200372}
373
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200374static void
375weston_surface_update_transform_disable(struct weston_surface *surface)
376{
377 surface->transform.enabled = 0;
378
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200379 /* round off fractions when not transformed */
380 surface->geometry.x = roundf(surface->geometry.x);
381 surface->geometry.y = roundf(surface->geometry.y);
382
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200383 pixman_region32_init_rect(&surface->transform.boundingbox,
384 surface->geometry.x,
385 surface->geometry.y,
386 surface->geometry.width,
387 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500388
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400389 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500390 pixman_region32_copy(&surface->transform.opaque,
391 &surface->opaque);
392 pixman_region32_translate(&surface->transform.opaque,
393 surface->geometry.x,
394 surface->geometry.y);
395 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200396}
397
398static int
399weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200400{
401 struct weston_matrix *matrix = &surface->transform.matrix;
402 struct weston_matrix *inverse = &surface->transform.inverse;
403 struct weston_transform *tform;
404
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200405 surface->transform.enabled = 1;
406
407 /* Otherwise identity matrix, but with x and y translation. */
408 surface->transform.position.matrix.d[12] = surface->geometry.x;
409 surface->transform.position.matrix.d[13] = surface->geometry.y;
410
411 weston_matrix_init(matrix);
412 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
413 weston_matrix_multiply(matrix, &tform->matrix);
414
415 if (weston_matrix_invert(inverse, matrix) < 0) {
416 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200417 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200418 " transformation not invertible.\n", surface);
419 return -1;
420 }
421
422 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
423 surface->geometry.height,
424 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500425
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200426 return 0;
427}
428
429WL_EXPORT void
430weston_surface_update_transform(struct weston_surface *surface)
431{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200432 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200433 return;
434
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200435 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200436
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500437 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200438
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200439 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500440 pixman_region32_fini(&surface->transform.opaque);
441 pixman_region32_init(&surface->transform.opaque);
442
443 if (region_is_undefined(&surface->input))
444 pixman_region32_init_rect(&surface->input, 0, 0,
445 surface->geometry.width,
446 surface->geometry.height);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200447
Pekka Paalanencd403622012-01-25 13:37:39 +0200448 /* transform.position is always in transformation_list */
449 if (surface->geometry.transformation_list.next ==
450 &surface->transform.position.link &&
451 surface->geometry.transformation_list.prev ==
452 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200453 weston_surface_update_transform_disable(surface);
454 } else {
455 if (weston_surface_update_transform_enable(surface) < 0)
456 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200457 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200458
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400459 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200460
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300461 if (weston_surface_is_mapped(surface))
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +0200462 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200463}
464
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200465WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100466weston_surface_to_global_fixed(struct weston_surface *surface,
467 wl_fixed_t sx, wl_fixed_t sy,
468 wl_fixed_t *x, wl_fixed_t *y)
469{
470 GLfloat xf, yf;
471
472 weston_surface_to_global_float(surface,
473 wl_fixed_to_double(sx),
474 wl_fixed_to_double(sy),
475 &xf, &yf);
476 *x = wl_fixed_from_double(xf);
477 *y = wl_fixed_from_double(yf);
478}
479
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200480static void
481surface_from_global_float(struct weston_surface *surface,
Daniel Stonebd3489b2012-05-08 17:17:53 +0100482 GLfloat x, GLfloat y, GLfloat *sx, GLfloat *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200483{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200484 if (surface->transform.enabled) {
485 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
486
487 weston_matrix_transform(&surface->transform.inverse, &v);
488
489 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200490 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200491 "weston_surface_from_global(), divisor = %g\n",
492 v.f[3]);
493 *sx = 0;
494 *sy = 0;
495 return;
496 }
497
Pekka Paalanencd403622012-01-25 13:37:39 +0200498 *sx = v.f[0] / v.f[3];
499 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200500 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200501 *sx = x - surface->geometry.x;
502 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200503 }
504}
505
506WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100507weston_surface_from_global_fixed(struct weston_surface *surface,
508 wl_fixed_t x, wl_fixed_t y,
509 wl_fixed_t *sx, wl_fixed_t *sy)
510{
511 GLfloat sxf, syf;
512
Daniel Stonebd3489b2012-05-08 17:17:53 +0100513 surface_from_global_float(surface,
514 wl_fixed_to_double(x),
515 wl_fixed_to_double(y),
516 &sxf, &syf);
517 *sx = wl_fixed_from_double(sxf);
518 *sy = wl_fixed_from_double(syf);
519}
520
521WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200522weston_surface_from_global(struct weston_surface *surface,
523 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
524{
525 GLfloat sxf, syf;
526
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200527 surface_from_global_float(surface, x, y, &sxf, &syf);
528 *sx = floorf(sxf);
529 *sy = floorf(syf);
530}
531
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400532WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400533weston_surface_schedule_repaint(struct weston_surface *surface)
534{
535 struct weston_output *output;
536
537 wl_list_for_each(output, &surface->compositor->output_list, link)
538 if (surface->output_mask & (1 << output->id))
539 weston_output_schedule_repaint(output);
540}
541
542WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500543weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500544{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400545 pixman_region32_union_rect(&surface->damage, &surface->damage,
546 0, 0, surface->geometry.width,
547 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200548
Kristian Høgsberg98238702012-08-03 16:29:12 -0400549 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500550}
551
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400552WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500553weston_surface_configure(struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200554 GLfloat x, GLfloat y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400555{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200556 surface->geometry.x = x;
557 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200558 surface->geometry.width = width;
559 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200560 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400561}
562
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200563WL_EXPORT void
564weston_surface_set_position(struct weston_surface *surface,
565 GLfloat x, GLfloat y)
566{
567 surface->geometry.x = x;
568 surface->geometry.y = y;
569 surface->geometry.dirty = 1;
570}
571
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300572WL_EXPORT int
573weston_surface_is_mapped(struct weston_surface *surface)
574{
575 if (surface->output)
576 return 1;
577 else
578 return 0;
579}
580
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400581WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500582weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500583{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400584 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500585
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400586 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500587
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400588 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500589}
590
Tiago Vignatti9d393522012-02-10 16:26:19 +0200591static struct weston_surface *
592weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100593 wl_fixed_t x, wl_fixed_t y,
594 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200595{
596 struct weston_surface *surface;
597
598 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100599 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500600 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100601 wl_fixed_to_int(*sx),
602 wl_fixed_to_int(*sy),
603 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200604 return surface;
605 }
606
607 return NULL;
608}
609
610static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400611weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500612{
Scott Moreau447013d2012-02-18 05:05:29 -0700613 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500614 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400615 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500616
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400617 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100618 return;
619
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400620 surface = weston_compositor_pick_surface(seat->compositor,
621 pointer->x,
622 pointer->y,
623 &pointer->current_x,
624 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500625
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400626 if (&surface->surface != pointer->current) {
627 interface = pointer->grab->interface;
628 pointer->current = &surface->surface;
629 interface->focus(pointer->grab, &surface->surface,
630 pointer->current_x,
631 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500632 }
633
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400634 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500635 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100636 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400637 pointer->x,
638 pointer->y,
639 &pointer->grab->x,
640 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500641}
642
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400643static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500644weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400645{
Daniel Stone37816df2012-05-16 18:45:18 +0100646 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400647
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500648 if (!compositor->focus)
649 return;
650
Daniel Stone37816df2012-05-16 18:45:18 +0100651 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400652 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400653}
654
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400655WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500656weston_surface_unmap(struct weston_surface *surface)
657{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100658 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500659
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500660 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500661 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500662 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500663
Daniel Stone4dab5db2012-05-30 16:31:53 +0100664 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100665 if (seat->seat.keyboard &&
666 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100667 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100668 if (seat->seat.pointer &&
669 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100670 wl_pointer_set_focus(seat->seat.pointer,
671 NULL,
672 wl_fixed_from_int(0),
673 wl_fixed_from_int(0));
674 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500675
Kristian Høgsberg98238702012-08-03 16:29:12 -0400676 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500677}
678
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400679struct weston_frame_callback {
680 struct wl_resource resource;
681 struct wl_list link;
682};
683
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500684static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400685destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500686{
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200687 int i;
688
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500689 struct weston_surface *surface =
690 container_of(resource,
691 struct weston_surface, surface.resource);
692 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400693 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400694
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300695 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500696 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400697
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200698 glDeleteTextures(surface->num_textures, surface->textures);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200699
Benjamin Franzke06286262011-05-06 19:12:33 +0200700 if (surface->buffer)
701 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400702
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200703 for (i = 0; i < surface->num_images; i++)
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400704 compositor->destroy_image(compositor->egl_display,
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200705 surface->images[i]);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200706
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200707 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200708 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500709 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500710 pixman_region32_fini(&surface->clip);
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500711 if (!region_is_undefined(&surface->input))
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500712 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200713
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400714 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
715 wl_resource_destroy(&cb->resource);
716
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400717 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500718}
719
Alex Wu8811bf92012-02-28 18:07:54 +0800720WL_EXPORT void
721weston_surface_destroy(struct weston_surface *surface)
722{
723 /* Not a valid way to destroy a client surface */
724 assert(surface->surface.resource.client == NULL);
725
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400726 wl_signal_emit(&surface->surface.resource.destroy_signal,
727 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800728 destroy_surface(&surface->surface.resource);
729}
730
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100731static void
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200732ensure_textures(struct weston_surface *es, int num_textures)
733{
734 int i;
735
736 if (num_textures <= es->num_textures)
737 return;
738
739 for (i = es->num_textures; i < num_textures; i++) {
740 glGenTextures(1, &es->textures[i]);
Rob Clarke3b95912012-08-31 16:42:18 -0500741 glBindTexture(es->target, es->textures[i]);
742 glTexParameteri(es->target,
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200743 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
Rob Clarke3b95912012-08-31 16:42:18 -0500744 glTexParameteri(es->target,
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200745 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
746 }
747 es->num_textures = num_textures;
Rob Clarke3b95912012-08-31 16:42:18 -0500748 glBindTexture(es->target, 0);
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200749}
750
751static void
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -0400752weston_surface_attach(struct wl_surface *surface, struct wl_buffer *buffer)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100753{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500754 struct weston_surface *es = (struct weston_surface *) surface;
755 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400756 EGLint attribs[3], format;
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200757 int i, num_planes;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100758
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300759 if (es->buffer) {
760 weston_buffer_post_release(es->buffer);
761 wl_list_remove(&es->buffer_destroy_listener.link);
762 }
763
764 es->buffer = buffer;
765
766 if (!buffer) {
767 if (weston_surface_is_mapped(es))
768 weston_surface_unmap(es);
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200769 for (i = 0; i < es->num_images; i++) {
770 ec->destroy_image(ec->egl_display, es->images[i]);
771 es->images[i] = NULL;
Kristian Høgsbergb8ceaaa2012-06-19 15:41:12 -0400772 }
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200773 es->num_images = 0;
774 glDeleteTextures(es->num_textures, es->textures);
775 es->num_textures = 0;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300776 return;
777 }
778
779 buffer->busy_count++;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400780 wl_signal_add(&es->buffer->resource.destroy_signal,
781 &es->buffer_destroy_listener);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300782
783 if (es->geometry.width != buffer->width ||
784 es->geometry.height != buffer->height) {
785 undef_region(&es->input);
786 pixman_region32_fini(&es->opaque);
787 pixman_region32_init(&es->opaque);
788 }
789
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100790 if (wl_buffer_is_shm(buffer)) {
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200791 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
Rob Clarke3b95912012-08-31 16:42:18 -0500792 es->target = GL_TEXTURE_2D;
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200793
794 ensure_textures(es, 1);
795 glBindTexture(GL_TEXTURE_2D, es->textures[0]);
Kristian Høgsbergfb6de222012-03-27 17:10:13 -0400796 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
797 es->pitch, es->buffer->height, 0,
798 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
Kristian Høgsbergd6ad1222012-05-17 11:11:15 -0400799 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
Pekka Paalanen6b5585b2012-08-30 16:47:19 -0500800 es->shader = &ec->texture_shader_rgbx;
Kristian Høgsbergd6ad1222012-05-17 11:11:15 -0400801 else
Pekka Paalanen6b5585b2012-08-30 16:47:19 -0500802 es->shader = &ec->texture_shader_rgba;
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200803 } else if (ec->query_buffer(ec->egl_display, buffer,
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400804 EGL_TEXTURE_FORMAT, &format)) {
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200805 for (i = 0; i < es->num_images; i++)
Kristian Høgsberg971cbc82012-07-17 14:21:25 -0400806 ec->destroy_image(ec->egl_display, es->images[i]);
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200807 es->num_images = 0;
Rob Clarke3b95912012-08-31 16:42:18 -0500808 es->target = GL_TEXTURE_2D;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400809 switch (format) {
810 case EGL_TEXTURE_RGB:
811 case EGL_TEXTURE_RGBA:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200812 default:
813 num_planes = 1;
814 es->shader = &ec->texture_shader_rgba;
815 break;
Rob Clarke3b95912012-08-31 16:42:18 -0500816 case EGL_TEXTURE_EXTERNAL_WL:
817 num_planes = 1;
818 es->target = GL_TEXTURE_EXTERNAL_OES;
819 es->shader = &ec->texture_shader_egl_external;
820 break;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400821 case EGL_TEXTURE_Y_UV_WL:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200822 num_planes = 2;
823 es->shader = &ec->texture_shader_y_uv;
824 break;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400825 case EGL_TEXTURE_Y_U_V_WL:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200826 num_planes = 3;
827 es->shader = &ec->texture_shader_y_u_v;
828 break;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400829 case EGL_TEXTURE_Y_XUXV_WL:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200830 num_planes = 2;
831 es->shader = &ec->texture_shader_y_xuxv;
832 break;
833 }
834
835 ensure_textures(es, num_planes);
836 for (i = 0; i < num_planes; i++) {
837 attribs[0] = EGL_WAYLAND_PLANE_WL;
838 attribs[1] = i;
839 attribs[2] = EGL_NONE;
840 es->images[i] = ec->create_image(ec->egl_display,
841 NULL,
842 EGL_WAYLAND_BUFFER_WL,
843 buffer, attribs);
Rob Clark48cd58b2012-08-13 17:39:17 -0500844 if (!es->images[i]) {
845 weston_log("failed to create img for plane %d\n", i);
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200846 continue;
Rob Clark48cd58b2012-08-13 17:39:17 -0500847 }
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200848 es->num_images++;
849
850 glActiveTexture(GL_TEXTURE0 + i);
Rob Clarke3b95912012-08-31 16:42:18 -0500851 glBindTexture(es->target, es->textures[i]);
852 ec->image_target_texture_2d(es->target,
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200853 es->images[i]);
854 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400855
Ander Conselvan de Oliveira9390ae32012-03-27 17:36:38 +0300856 es->pitch = buffer->width;
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200857 } else {
Rob Clark48cd58b2012-08-13 17:39:17 -0500858 weston_log("unhandled buffer type!\n");
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100859 }
860}
861
Rob Clark0e5a2d02012-08-30 16:47:18 -0500862
863#define max(a, b) (((a) > (b)) ? (a) : (b))
864#define min(a, b) (((a) > (b)) ? (b) : (a))
865#define clip(x, a, b) min(max(x, a), b)
866#define sign(x) ((x) >= 0)
867
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400868static int
Rob Clark0e5a2d02012-08-30 16:47:18 -0500869calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
870 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500871{
Rob Clark0e5a2d02012-08-30 16:47:18 -0500872 int i, n = 0;
873 GLfloat min_x, max_x, min_y, max_y;
874 GLfloat x[4] = {
875 surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1,
876 };
877 GLfloat y[4] = {
878 surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2,
879 };
880 GLfloat cx1 = rect->x1;
881 GLfloat cx2 = rect->x2;
882 GLfloat cy1 = rect->y1;
883 GLfloat cy2 = rect->y2;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500884
Rob Clark0e5a2d02012-08-30 16:47:18 -0500885 GLfloat dist_squared(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
886 {
887 GLfloat dx = (x1 - x2);
888 GLfloat dy = (y1 - y2);
889 return dx * dx + dy * dy;
890 }
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400891
Rob Clark0e5a2d02012-08-30 16:47:18 -0500892 void append_vertex(GLfloat x, GLfloat y)
893 {
894 /* don't emit duplicate vertices: */
895 if ((n > 0) && (ex[n-1] == x) && (ey[n-1] == y))
896 return;
897 ex[n] = x;
898 ey[n] = y;
899 n++;
900 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500901
Rob Clark0e5a2d02012-08-30 16:47:18 -0500902 /* transform surface to screen space: */
903 for (i = 0; i < 4; i++)
904 weston_surface_to_global_float(es, x[i], y[i], &x[i], &y[i]);
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500905
Rob Clark0e5a2d02012-08-30 16:47:18 -0500906 /* find bounding box: */
907 min_x = max_x = x[0];
908 min_y = max_y = y[0];
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500909
Rob Clark0e5a2d02012-08-30 16:47:18 -0500910 for (i = 1; i < 4; i++) {
911 min_x = min(min_x, x[i]);
912 max_x = max(max_x, x[i]);
913 min_y = min(min_y, y[i]);
914 max_y = max(max_y, y[i]);
915 }
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500916
Rob Clark0e5a2d02012-08-30 16:47:18 -0500917 /* First, simple bounding box check to discard early transformed
918 * surface rects that do not intersect with the clip region:
919 */
920 if ((min_x > cx2) || (max_x < cx1) ||
921 (min_y > cy2) || (max_y < cy1))
922 return 0;
923
924 /* Simple case, bounding box edges are parallel to surface edges,
925 * there will be only four edges. We just need to clip the surface
926 * vertices to the clip rect bounds:
927 */
928 if (!es->transform.enabled) {
929 for (i = 0; i < 4; i++) {
930 ex[n] = clip(x[i], cx1, cx2);
931 ey[n] = clip(y[i], cy1, cy2);
932 n++;
933 }
934 return 4;
935 }
936
937 /* Hard case, transformation applied. We need to find the vertices
938 * of the shape that is the intersection of the clip rect and
939 * transformed surface. This can be anything from 3 to 8 sides.
940 *
941 * Observation: all the resulting vertices will be the intersection
942 * points of the transformed surface and the clip rect, plus the
943 * vertices of the clip rect which are enclosed by the transformed
944 * surface and the vertices of the transformed surface which are
945 * enclosed by the clip rect.
946 *
947 * Observation: there will be zero, one, or two resulting vertices
948 * for each edge of the src rect.
949 *
950 * Loop over four edges of the transformed rect:
951 */
952 for (i = 0; i < 4; i++) {
953 GLfloat x1, y1, x2, y2;
954 int last_n = n;
955
956 x1 = x[i];
957 y1 = y[i];
958
959 /* if this vertex is contained in the clip rect, use it as-is: */
960 if ((cx1 <= x1) && (x1 <= cx2) &&
961 (cy1 <= y1) && (y1 <= cy2))
962 append_vertex(x1, y1);
963
964 /* for remaining, we consider the point as part of a line: */
965 x2 = x[(i+1) % 4];
966 y2 = y[(i+1) % 4];
967
968 if (x1 == x2) {
969 append_vertex(clip(x1, cx1, cx2), clip(y1, cy1, cy2));
970 append_vertex(clip(x2, cx1, cx2), clip(y2, cy1, cy2));
971 } else if (y1 == y2) {
972 append_vertex(clip(x1, cx1, cx2), clip(y1, cy1, cy2));
973 append_vertex(clip(x2, cx1, cx2), clip(y2, cy1, cy2));
974 } else {
975 GLfloat m, c, p;
976 GLfloat tx[2], ty[2];
977 int tn = 0;
978
979 int intersect_horiz(GLfloat y, GLfloat *p)
980 {
981 GLfloat x;
982
983 /* if y does not lie between y1 and y2, no
984 * intersection possible
985 */
986 if (sign(y-y1) == sign(y-y2))
987 return 0;
988
989 x = (y - c) / m;
990
991 /* if x does not lie between cx1 and cx2, no
992 * intersection:
993 */
994 if (sign(x-cx1) == sign(x-cx2))
995 return 0;
996
997 *p = x;
998 return 1;
999 }
1000
1001 int intersect_vert(GLfloat x, GLfloat *p)
1002 {
1003 GLfloat y;
1004
1005 if (sign(x-x1) == sign(x-x2))
1006 return 0;
1007
1008 y = m * x + c;
1009
1010 if (sign(y-cy1) == sign(y-cy2))
1011 return 0;
1012
1013 *p = y;
1014 return 1;
1015 }
1016
1017 /* y = mx + c */
1018 m = (y2 - y1) / (x2 - x1);
1019 c = y1 - m * x1;
1020
1021 /* check for up to two intersections with the four edges
1022 * of the clip rect. Note that we don't know the orientation
1023 * of the transformed surface wrt. the clip rect. So if when
1024 * there are two intersection points, we need to put the one
1025 * closest to x1,y1 first:
1026 */
1027
1028 /* check top clip rect edge: */
1029 if (intersect_horiz(cy1, &p)) {
1030 ty[tn] = cy1;
1031 tx[tn] = p;
1032 tn++;
1033 }
1034
1035 /* check right clip rect edge: */
1036 if (intersect_vert(cx2, &p)) {
1037 ty[tn] = p;
1038 tx[tn] = cx2;
1039 tn++;
1040 if (tn == 2)
1041 goto edge_check_done;
1042 }
1043
1044 /* check bottom clip rect edge: */
1045 if (intersect_horiz(cy2, &p)) {
1046 ty[tn] = cy2;
1047 tx[tn] = p;
1048 tn++;
1049 if (tn == 2)
1050 goto edge_check_done;
1051 }
1052
1053 /* check left clip rect edge: */
1054 if (intersect_vert(cx1, &p)) {
1055 ty[tn] = p;
1056 tx[tn] = cx1;
1057 tn++;
1058 }
1059
1060edge_check_done:
1061 if (tn == 1) {
1062 append_vertex(tx[0], ty[0]);
1063 } else if (tn == 2) {
1064 if (dist_squared(x1, y1, tx[0], ty[0]) <
1065 dist_squared(x1, y1, tx[1], ty[1])) {
1066 append_vertex(tx[0], ty[0]);
1067 append_vertex(tx[1], ty[1]);
1068 } else {
1069 append_vertex(tx[1], ty[1]);
1070 append_vertex(tx[0], ty[0]);
1071 }
1072 }
1073
1074 if (n == last_n) {
1075 GLfloat best_x=0, best_y=0;
1076 uint32_t d, best_d = (unsigned int)-1; /* distance squared */
1077 uint32_t max_d = dist_squared(x2, y2,
1078 x[(i+2) % 4], y[(i+2) % 4]);
1079
1080 /* if there are no vertices on this line, it could be that
1081 * there is a vertex of the clip rect that is enclosed by
1082 * the transformed surface. Find the vertex of the clip
1083 * rect that is reached by the shortest line perpendicular
1084 * to the current edge, if any.
1085 *
1086 * slope of perpendicular is 1/m, so
1087 *
1088 * cy = -cx/m + c2
1089 * c2 = cy + cx/m
1090 *
1091 */
1092
1093 int perp_intersect(GLfloat cx, GLfloat cy, uint32_t *d)
1094 {
1095 GLfloat c2 = cy + cx/m;
1096 GLfloat x = (c2 - c) / (m + 1/m);
1097
1098 /* if the x position of the intersection of the
1099 * perpendicular with the transformed edge does
1100 * not lie within the bounds of the edge, then
1101 * no intersection:
1102 */
1103 if (sign(x-x1) == sign(x-x2))
1104 return 0;
1105
1106 *d = dist_squared(cx, cy, x, (m * x) + c);
1107
1108 /* if intersection distance is further away than
1109 * opposite edge of surface region, it is invalid:
1110 */
1111 if (*d > max_d)
1112 return 0;
1113
1114 return 1;
1115 }
1116
1117 if (perp_intersect(cx1, cy1, &d)) {
1118 best_x = cx1;
1119 best_y = cy1;
1120 best_d = d;
1121 }
1122
1123 if (perp_intersect(cx1, cy2, &d) && (d < best_d)) {
1124 best_x = cx1;
1125 best_y = cy2;
1126 best_d = d;
1127 }
1128
1129 if (perp_intersect(cx2, cy2, &d) && (d < best_d)) {
1130 best_x = cx2;
1131 best_y = cy2;
1132 best_d = d;
1133 }
1134
1135 if (perp_intersect(cx2, cy1, &d) && (d < best_d)) {
1136 best_x = cx2;
1137 best_y = cy1;
1138 best_d = d;
1139 }
1140
1141 if (best_d != (unsigned int)-1) // XXX can this happen?
1142 append_vertex(best_x, best_y);
1143 }
1144 }
1145
Kristian Høgsberg525e4c02011-02-14 10:39:54 -05001146 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001147
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001148 return n;
1149}
1150
Rob Clark0e5a2d02012-08-30 16:47:18 -05001151static int
1152texture_region(struct weston_surface *es, pixman_region32_t *region,
1153 pixman_region32_t *surf_region)
1154{
1155 struct weston_compositor *ec = es->compositor;
1156 GLfloat *v, inv_width, inv_height;
1157 unsigned int *vtxcnt, nvtx = 0;
1158 pixman_box32_t *rects, *surf_rects;
1159 int i, j, k, nrects, nsurf;
1160
1161 rects = pixman_region32_rectangles(region, &nrects);
1162 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
1163
1164 /* worst case we can have 10 vertices per rect (ie. clipped into
1165 * an octagon):
1166 */
1167 v = wl_array_add(&ec->vertices, nrects * nsurf * 10 * 4 * sizeof *v);
1168 vtxcnt = wl_array_add(&ec->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
1169
1170 inv_width = 1.0 / es->pitch;
1171 inv_height = 1.0 / es->geometry.height;
1172
1173 for (i = 0; i < nrects; i++) {
1174 pixman_box32_t *rect = &rects[i];
1175 for (j = 0; j < nsurf; j++) {
1176 pixman_box32_t *surf_rect = &surf_rects[j];
1177 GLfloat cx, cy;
1178 GLfloat ex[8], ey[8]; /* edge points in screen space */
1179 int n;
1180
1181 void emit_vertex(GLfloat gx, GLfloat gy)
1182 {
1183 GLfloat sx, sy;
1184 surface_from_global_float(es, gx, gy, &sx, &sy);
1185 /* In groups of 4 attributes, first two are 'position', 2nd two
1186 * are 'texcoord'.
1187 */
1188 *(v++) = gx;
1189 *(v++) = gy;
1190 *(v++) = sx * inv_width;
1191 *(v++) = sy * inv_height;
1192 }
1193
1194 /* The transformed surface, after clipping to the clip region,
1195 * can have as many as eight sides, emitted as a triangle-fan.
1196 * The first vertex is the center, followed by each corner.
1197 *
1198 * If a corner of the transformed surface falls outside of the
1199 * clip region, instead of emitting one vertex for the corner
1200 * of the surface, up to two are emitted for two corresponding
1201 * intersection point(s) between the surface and the clip region.
1202 *
1203 * To do this, we first calculate the (up to eight) points that
1204 * form the intersection of the clip rect and the transformed
1205 * surface. After that we calculate the average to determine
1206 * the center point, and emit the center and edge vertices of
1207 * the fan.
1208 */
1209 n = calculate_edges(es, rect, surf_rect, ex, ey);
1210 if (n < 3)
1211 continue;
1212
1213 /* calculate/emit center point: */
1214 cx = 0;
1215 cy = 0;
1216 for (k = 0; k < n; k++) {
1217 cx += ex[k];
1218 cy += ey[k];
1219 }
1220 cx /= n;
1221 cy /= n;
1222 emit_vertex(cx, cy);
1223
1224 /* then emit edge points: */
1225 for (k = 0; k < n; k++)
1226 emit_vertex(ex[k], ey[k]);
1227
1228 /* and close the fan: */
1229 emit_vertex(ex[0], ey[0]);
1230
1231 vtxcnt[nvtx++] = n + 2;
1232 }
1233 }
1234
1235 return nvtx;
1236}
1237
1238static void
1239repaint_region(struct weston_surface *es, pixman_region32_t *region,
1240 pixman_region32_t *surf_region)
1241{
1242 struct weston_compositor *ec = es->compositor;
1243 GLfloat *v;
1244 unsigned int *vtxcnt;
1245 int i, first, nfans;
1246
1247 /* The final region to be painted is the intersection of
1248 * 'region' and 'surf_region'. However, 'region' is in the global
1249 * coordinates, and 'surf_region' is in the surface-local
1250 * corodinates. texture_region() will iterate over all pairs of
1251 * rectangles from both regions, compute the intersection
1252 * polygon for each pair, and store it as a triangle fan if
1253 * it has a non-zero area.
1254 */
1255 nfans = texture_region(es, region, surf_region);
1256
1257 v = ec->vertices.data;
1258 vtxcnt = ec->vtxcnt.data;
1259
1260 /* position: */
1261 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
1262 glEnableVertexAttribArray(0);
1263
1264 /* texcoord: */
1265 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
1266 glEnableVertexAttribArray(1);
1267
1268 for (i = 0, first = 0; i < nfans; i++) {
1269 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
1270 first += vtxcnt[i];
1271 }
1272
1273 glDisableVertexAttribArray(1);
1274 glDisableVertexAttribArray(0);
1275
1276 ec->vertices.size = 0;
1277 ec->vtxcnt.size = 0;
1278}
1279
1280
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05001281WL_EXPORT void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001282weston_surface_draw(struct weston_surface *es, struct weston_output *output,
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001283 pixman_region32_t *damage) /* in global coordinates */
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001284{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001285 struct weston_compositor *ec = es->compositor;
Rob Clark0e5a2d02012-08-30 16:47:18 -05001286 /* repaint bounding region in global coordinates: */
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001287 pixman_region32_t repaint;
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001288 /* non-opaque region in surface coordinates: */
1289 pixman_region32_t surface_blend;
Kristian Høgsberg40caded2011-06-20 19:48:16 -04001290 GLint filter;
Rob Clark0e5a2d02012-08-30 16:47:18 -05001291 int i;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001292
Pekka Paalanena9f8a212012-01-26 11:28:08 +02001293 pixman_region32_init(&repaint);
Rob Clark0e5a2d02012-08-30 16:47:18 -05001294 pixman_region32_init(&surface_blend);
1295
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001296 pixman_region32_intersect(&repaint,
1297 &es->transform.boundingbox, damage);
1298 pixman_region32_subtract(&repaint, &repaint, &es->clip);
Pekka Paalanenc7b45f62012-02-06 12:16:07 +02001299
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001300 if (!pixman_region32_not_empty(&repaint))
Pekka Paalanena9f8a212012-01-26 11:28:08 +02001301 goto out;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001302
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001303 pixman_region32_subtract(&ec->primary_plane.damage,
1304 &ec->primary_plane.damage, &repaint);
1305
Kristian Høgsberg101cb652012-02-17 10:45:16 -05001306 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001307
1308 /* blended region is whole surface minus opaque region: */
1309 pixman_region32_init_rect(&surface_blend, 0, 0,
1310 es->geometry.width, es->geometry.height);
1311 if (es->alpha >= 1.0)
Rob Clark0e5a2d02012-08-30 16:47:18 -05001312 pixman_region32_subtract(&surface_blend, &surface_blend,
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001313 &es->opaque);
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001314
Kristian Høgsberg07632622012-01-25 23:02:06 -05001315 if (ec->current_shader != es->shader) {
1316 glUseProgram(es->shader->program);
Kristian Høgsberg07632622012-01-25 23:02:06 -05001317 ec->current_shader = es->shader;
1318 }
1319
Kristian Høgsberg0452abc2012-01-31 15:38:36 -05001320 glUniformMatrix4fv(es->shader->proj_uniform,
1321 1, GL_FALSE, output->matrix.d);
Pekka Paalanenf55f5442012-02-02 16:49:05 +02001322 glUniform4fv(es->shader->color_uniform, 1, es->color);
Kristian Høgsberga416fa12012-05-21 14:06:52 -04001323 glUniform1f(es->shader->alpha_uniform, es->alpha);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001324
Scott Moreauccbf29d2012-02-22 14:21:41 -07001325 if (es->transform.enabled || output->zoom.active)
Kristian Høgsberg40caded2011-06-20 19:48:16 -04001326 filter = GL_LINEAR;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001327 else
Pekka Paalanenc61eca62012-01-06 14:10:06 +02001328 filter = GL_NEAREST;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001329
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02001330 for (i = 0; i < es->num_textures; i++) {
1331 glUniform1i(es->shader->tex_uniforms[i], i);
1332 glActiveTexture(GL_TEXTURE0 + i);
Rob Clarke3b95912012-08-31 16:42:18 -05001333 glBindTexture(es->target, es->textures[i]);
1334 glTexParameteri(es->target, GL_TEXTURE_MIN_FILTER, filter);
1335 glTexParameteri(es->target, GL_TEXTURE_MAG_FILTER, filter);
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02001336 }
Kristian Høgsberg40caded2011-06-20 19:48:16 -04001337
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001338 if (pixman_region32_not_empty(&es->opaque) && es->alpha >= 1.0) {
Rob Clark0e5a2d02012-08-30 16:47:18 -05001339 glDisable(GL_BLEND);
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001340 repaint_region(es, &repaint, &es->opaque);
Rob Clark0e5a2d02012-08-30 16:47:18 -05001341 }
Pekka Paalanen3df327f2012-01-24 15:53:35 +02001342
Rob Clark0e5a2d02012-08-30 16:47:18 -05001343 if (pixman_region32_not_empty(&surface_blend)) {
1344 glEnable(GL_BLEND);
1345 repaint_region(es, &repaint, &surface_blend);
1346 }
Pekka Paalanena9f8a212012-01-26 11:28:08 +02001347
1348out:
Kristian Høgsberg525e4c02011-02-14 10:39:54 -05001349 pixman_region32_fini(&repaint);
Rob Clark0e5a2d02012-08-30 16:47:18 -05001350 pixman_region32_fini(&surface_blend);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001351}
1352
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001353WL_EXPORT void
1354weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001355{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001356 wl_list_remove(&surface->layer_link);
1357 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001358 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001359 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001360}
1361
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001362WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001363weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001364{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001365 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001366
1367 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001368 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001369}
1370
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001371WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001372weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +02001373{
Kristian Høgsberg24596942011-10-24 17:51:02 -04001374 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +02001375 return;
Benjamin Franzke06286262011-05-06 19:12:33 +02001376
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001377 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -05001378 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +02001379}
1380
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001381WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001382weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001383{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001384 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001385
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001386 pixman_region32_union(&compositor->primary_plane.damage,
1387 &compositor->primary_plane.damage,
1388 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001389 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001390}
1391
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001392static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001393fade_frame(struct weston_animation *animation,
1394 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001395{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001396 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001397 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001398 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001399 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001400
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001401 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -06001402 compositor->fade.spring.timestamp = msecs;
1403
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001404 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001405 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001406 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
1407 compositor->fade.spring.current);
1408 weston_surface_damage(surface);
1409
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001410 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001411 compositor->fade.spring.current =
1412 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001413 wl_list_remove(&animation->link);
1414 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001415
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001416 if (compositor->fade.spring.current < 0.001) {
1417 destroy_surface(&surface->surface.resource);
1418 compositor->fade.surface = NULL;
1419 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001420 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001421 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001422 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001423 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001424}
1425
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001426static void
1427update_shm_texture(struct weston_surface *surface)
1428{
1429#ifdef GL_UNPACK_ROW_LENGTH
1430 pixman_box32_t *rectangles;
1431 void *data;
1432 int i, n;
1433#endif
1434
Gwenole Beauchesne023f8552012-04-20 11:07:06 +02001435 glBindTexture(GL_TEXTURE_2D, surface->textures[0]);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001436
1437 if (!surface->compositor->has_unpack_subimage) {
1438 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1439 surface->pitch, surface->buffer->height, 0,
1440 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1441 wl_shm_buffer_get_data(surface->buffer));
1442
1443 return;
1444 }
1445
1446#ifdef GL_UNPACK_ROW_LENGTH
1447 /* Mesa does not define GL_EXT_unpack_subimage */
1448 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch);
1449 data = wl_shm_buffer_get_data(surface->buffer);
1450 rectangles = pixman_region32_rectangles(&surface->damage, &n);
1451 for (i = 0; i < n; i++) {
1452 glPixelStorei(GL_UNPACK_SKIP_PIXELS, rectangles[i].x1);
1453 glPixelStorei(GL_UNPACK_SKIP_ROWS, rectangles[i].y1);
1454 glTexSubImage2D(GL_TEXTURE_2D, 0,
1455 rectangles[i].x1, rectangles[i].y1,
Rob Bradford8f241562012-07-02 17:33:40 +01001456 rectangles[i].x2 - rectangles[i].x1,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001457 rectangles[i].y2 - rectangles[i].y1,
1458 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
1459 }
1460#endif
1461}
1462
1463static void
1464surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001465 pixman_region32_t *opaque)
1466{
1467 if (surface->buffer && wl_buffer_is_shm(surface->buffer))
1468 update_shm_texture(surface);
1469
1470 if (surface->transform.enabled) {
1471 pixman_box32_t *extents;
1472
1473 extents = pixman_region32_extents(&surface->damage);
1474 surface_compute_bbox(surface, extents->x1, extents->y1,
1475 extents->x2 - extents->x1,
1476 extents->y2 - extents->y1,
1477 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001478 pixman_region32_translate(&surface->damage,
1479 -surface->plane->x,
1480 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001481 } else {
1482 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001483 surface->geometry.x - surface->plane->x,
1484 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001485 }
1486
1487 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001488 pixman_region32_union(&surface->plane->damage,
1489 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001490 empty_region(&surface->damage);
1491 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001492 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001493}
1494
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001495static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001496weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001497{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001498 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001499 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001500 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001501 struct weston_animation *animation, *next;
1502 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001503 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001504 pixman_region32_t opaque, output_damage, new_damage;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001505 int32_t width, height;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001506
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001507 weston_compositor_update_drag_surfaces(ec);
1508
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001509 width = output->current->width +
1510 output->border.left + output->border.right;
1511 height = output->current->height +
1512 output->border.top + output->border.bottom;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001513
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001514 glViewport(0, 0, width, height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -05001515
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001516 /* Rebuild the surface list and update surface transforms up front. */
1517 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +02001518 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001519 wl_list_for_each(layer, &ec->layer_list, link) {
1520 wl_list_for_each(es, &layer->surface_list, layer_link) {
1521 weston_surface_update_transform(es);
1522 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +02001523 if (es->output == output) {
1524 wl_list_insert_list(&frame_callback_list,
1525 &es->frame_callback_list);
1526 wl_list_init(&es->frame_callback_list);
1527 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001528 }
1529 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001530
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001531 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001532 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001533 else
1534 wl_list_for_each(es, &ec->surface_list, link)
1535 weston_surface_move_to_plane(es, &ec->primary_plane);
1536
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001537 pixman_region32_init(&opaque);
1538
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001539 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001540 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001541
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -04001542 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001543
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001544 pixman_region32_init(&output_damage);
1545
1546 pixman_region32_init(&new_damage);
1547 pixman_region32_copy(&new_damage, &ec->primary_plane.damage);
1548
1549 pixman_region32_union(&ec->primary_plane.damage,
1550 &ec->primary_plane.damage,
1551 &output->previous_damage);
1552
1553 pixman_region32_intersect(&output->previous_damage,
1554 &new_damage, &output->region);
1555
1556 pixman_region32_intersect(&output_damage,
1557 &ec->primary_plane.damage, &output->region);
1558
1559 pixman_region32_fini(&new_damage);
1560
Scott Moreauccbf29d2012-02-22 14:21:41 -07001561 if (output->dirty)
1562 weston_output_update_matrix(output);
1563
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001564 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001565
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001566 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001567
Kristian Høgsbergef044142011-06-21 15:02:12 -04001568 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001569
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001570 weston_compositor_repick(ec);
1571 wl_event_loop_dispatch(ec->input_loop, 0);
1572
Jonas Ådahldb773762012-06-13 00:01:21 +02001573 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001574 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001575 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001576 }
Jonas Ådahldb773762012-06-13 00:01:21 +02001577 wl_list_init(&frame_callback_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001578
Scott Moreaud64cf212012-06-08 19:40:54 -06001579 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001580 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001581 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001582 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001583}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001584
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001585static int
1586weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001587{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001588 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001589
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001590 wl_event_loop_dispatch(compositor->input_loop, 0);
1591
1592 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001593}
1594
1595WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001596weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001597{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001598 struct weston_compositor *compositor = output->compositor;
1599 struct wl_event_loop *loop =
1600 wl_display_get_event_loop(compositor->wl_display);
1601 int fd;
1602
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001603 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001604 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001605 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001606 return;
1607 }
1608
1609 output->repaint_scheduled = 0;
1610 if (compositor->input_loop_source)
1611 return;
1612
1613 fd = wl_event_loop_get_fd(compositor->input_loop);
1614 compositor->input_loop_source =
1615 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1616 weston_compositor_read_input, compositor);
1617}
1618
1619static void
1620idle_repaint(void *data)
1621{
1622 struct weston_output *output = data;
1623
1624 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001625}
1626
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001627WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001628weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1629{
1630 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001631 if (below != NULL)
1632 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001633}
1634
1635WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001636weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001637{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001638 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001639 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001640
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001641 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001642 return;
1643
Kristian Høgsbergef044142011-06-21 15:02:12 -04001644 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001645 output->repaint_needed = 1;
1646 if (output->repaint_scheduled)
1647 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001648
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001649 wl_event_loop_add_idle(loop, idle_repaint, output);
1650 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001651
1652 if (compositor->input_loop_source) {
1653 wl_event_source_remove(compositor->input_loop_source);
1654 compositor->input_loop_source = NULL;
1655 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001656}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001657
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001658WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001659weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1660{
1661 struct weston_output *output;
1662
1663 wl_list_for_each(output, &compositor->output_list, link)
1664 weston_output_schedule_repaint(output);
1665}
1666
1667WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001668weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001669{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001670 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001671 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001672
Scott Moreau9d1b1122012-06-08 19:40:53 -06001673 output = container_of(compositor->output_list.next,
1674 struct weston_output, link);
1675
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001676 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001677 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001678 return;
1679
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001680 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001681 surface = weston_surface_create(compositor);
1682 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001683 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001684 wl_list_insert(&compositor->fade_layer.surface_list,
1685 &surface->layer_link);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001686 weston_surface_assign_output(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001687 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001688 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001689 }
1690
1691 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001692 if (wl_list_empty(&compositor->fade.animation.link)) {
1693 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001694 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001695 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001696 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001697}
1698
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001699static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001700surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001701{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001702 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001703}
1704
Casey Dahlin96d8a752012-04-19 22:50:07 -04001705static struct wl_resource *
1706find_resource_for_client(struct wl_list *list, struct wl_client *client)
1707{
1708 struct wl_resource *r;
1709
1710 wl_list_for_each(r, list, link) {
1711 if (r->client == client)
1712 return r;
1713 }
1714
1715 return NULL;
1716}
1717
Casey Dahlin9074db52012-04-19 22:50:09 -04001718static void
1719weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1720{
1721 uint32_t different = es->output_mask ^ mask;
1722 uint32_t entered = mask & different;
1723 uint32_t left = es->output_mask & different;
1724 struct weston_output *output;
Rob Bradford8667e772012-05-18 14:13:03 +01001725 struct wl_resource *resource = NULL;
Casey Dahlin9074db52012-04-19 22:50:09 -04001726 struct wl_client *client = es->surface.resource.client;
1727
Scott Moreaua5021522012-08-03 17:11:51 -06001728 es->output_mask = mask;
Casey Dahlin9074db52012-04-19 22:50:09 -04001729 if (es->surface.resource.client == NULL)
1730 return;
1731 if (different == 0)
1732 return;
1733
Casey Dahlin9074db52012-04-19 22:50:09 -04001734 wl_list_for_each(output, &es->compositor->output_list, link) {
1735 if (1 << output->id & different)
1736 resource =
1737 find_resource_for_client(&output->resource_list,
1738 client);
Kristian Høgsbergc7814d22012-07-12 12:34:43 -04001739 if (resource == NULL)
1740 continue;
Casey Dahlin9074db52012-04-19 22:50:09 -04001741 if (1 << output->id & entered)
1742 wl_surface_send_enter(&es->surface.resource, resource);
1743 if (1 << output->id & left)
1744 wl_surface_send_leave(&es->surface.resource, resource);
1745 }
1746}
1747
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001748WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001749weston_surface_assign_output(struct weston_surface *es)
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001750{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001751 struct weston_compositor *ec = es->compositor;
1752 struct weston_output *output, *new_output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001753 pixman_region32_t region;
Casey Dahlin9074db52012-04-19 22:50:09 -04001754 uint32_t max, area, mask;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001755 pixman_box32_t *e;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001756
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001757 new_output = NULL;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001758 max = 0;
Casey Dahlin9074db52012-04-19 22:50:09 -04001759 mask = 0;
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001760 pixman_region32_init(&region);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001761 wl_list_for_each(output, &ec->output_list, link) {
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001762 pixman_region32_intersect(&region, &es->transform.boundingbox,
1763 &output->region);
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001764
1765 e = pixman_region32_extents(&region);
1766 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1767
Casey Dahlin9074db52012-04-19 22:50:09 -04001768 if (area > 0)
1769 mask |= 1 << output->id;
1770
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001771 if (area >= max) {
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001772 new_output = output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001773 max = area;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001774 }
1775 }
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001776 pixman_region32_fini(&region);
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001777
1778 es->output = new_output;
Casey Dahlin9074db52012-04-19 22:50:09 -04001779 weston_surface_update_output_mask(es, mask);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001780}
1781
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001782static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001783surface_attach(struct wl_client *client,
1784 struct wl_resource *resource,
1785 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1786{
1787 struct weston_surface *es = resource->data;
1788 struct wl_buffer *buffer = NULL;
1789
1790 if (buffer_resource)
1791 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001792
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -04001793 weston_surface_attach(&es->surface, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001794
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001795 if (buffer && es->configure)
1796 es->configure(es, sx, sy);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001797}
1798
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001799static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001800surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001801 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001802 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001803{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001804 struct weston_surface *es = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001805
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001806 pixman_region32_union_rect(&es->damage, &es->damage,
1807 x, y, width, height);
Kristian Høgsberg98238702012-08-03 16:29:12 -04001808 weston_surface_schedule_repaint(es);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001809}
1810
Kristian Høgsberg33418202011-08-16 23:01:28 -04001811static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001812destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001813{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001814 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001815
1816 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001817 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001818}
1819
1820static void
1821surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001822 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001823{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001824 struct weston_frame_callback *cb;
1825 struct weston_surface *es = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001826
1827 cb = malloc(sizeof *cb);
1828 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001829 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001830 return;
1831 }
1832
1833 cb->resource.object.interface = &wl_callback_interface;
1834 cb->resource.object.id = callback;
1835 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001836 cb->resource.client = client;
1837 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001838
1839 wl_client_add_resource(client, &cb->resource);
Jonas Ådahldb773762012-06-13 00:01:21 +02001840 wl_list_insert(es->frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001841}
1842
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001843static void
1844surface_set_opaque_region(struct wl_client *client,
1845 struct wl_resource *resource,
1846 struct wl_resource *region_resource)
1847{
1848 struct weston_surface *surface = resource->data;
1849 struct weston_region *region;
1850
1851 pixman_region32_fini(&surface->opaque);
1852
1853 if (region_resource) {
1854 region = region_resource->data;
1855 pixman_region32_init_rect(&surface->opaque, 0, 0,
1856 surface->geometry.width,
1857 surface->geometry.height);
1858 pixman_region32_intersect(&surface->opaque,
1859 &surface->opaque, &region->region);
1860 } else {
1861 pixman_region32_init(&surface->opaque);
1862 }
1863
1864 surface->geometry.dirty = 1;
1865}
1866
1867static void
1868surface_set_input_region(struct wl_client *client,
1869 struct wl_resource *resource,
1870 struct wl_resource *region_resource)
1871{
1872 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001873 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001874
1875 if (region_resource) {
1876 region = region_resource->data;
1877 pixman_region32_init_rect(&surface->input, 0, 0,
1878 surface->geometry.width,
1879 surface->geometry.height);
1880 pixman_region32_intersect(&surface->input,
1881 &surface->input, &region->region);
1882 } else {
1883 pixman_region32_init_rect(&surface->input, 0, 0,
1884 surface->geometry.width,
1885 surface->geometry.height);
1886 }
1887
Kristian Høgsberg98238702012-08-03 16:29:12 -04001888 weston_surface_schedule_repaint(surface);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001889}
1890
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001891static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001892 surface_destroy,
1893 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001894 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001895 surface_frame,
1896 surface_set_opaque_region,
1897 surface_set_input_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001898};
1899
1900static void
1901compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001902 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001903{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001904 struct weston_compositor *ec = resource->data;
1905 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001906
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001907 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001908 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001909 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001910 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001911 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001912
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001913 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001914
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001915 surface->surface.resource.object.id = id;
1916 surface->surface.resource.object.interface = &wl_surface_interface;
1917 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001918 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001919 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001920
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001921 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001922}
1923
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001924static void
1925destroy_region(struct wl_resource *resource)
1926{
1927 struct weston_region *region =
1928 container_of(resource, struct weston_region, resource);
1929
1930 pixman_region32_fini(&region->region);
1931 free(region);
1932}
1933
1934static void
1935region_destroy(struct wl_client *client, struct wl_resource *resource)
1936{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001937 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001938}
1939
1940static void
1941region_add(struct wl_client *client, struct wl_resource *resource,
1942 int32_t x, int32_t y, int32_t width, int32_t height)
1943{
1944 struct weston_region *region = resource->data;
1945
1946 pixman_region32_union_rect(&region->region, &region->region,
1947 x, y, width, height);
1948}
1949
1950static void
1951region_subtract(struct wl_client *client, struct wl_resource *resource,
1952 int32_t x, int32_t y, int32_t width, int32_t height)
1953{
1954 struct weston_region *region = resource->data;
1955 pixman_region32_t rect;
1956
1957 pixman_region32_init_rect(&rect, x, y, width, height);
1958 pixman_region32_subtract(&region->region, &region->region, &rect);
1959 pixman_region32_fini(&rect);
1960}
1961
1962static const struct wl_region_interface region_interface = {
1963 region_destroy,
1964 region_add,
1965 region_subtract
1966};
1967
1968static void
1969compositor_create_region(struct wl_client *client,
1970 struct wl_resource *resource, uint32_t id)
1971{
1972 struct weston_region *region;
1973
1974 region = malloc(sizeof *region);
1975 if (region == NULL) {
1976 wl_resource_post_no_memory(resource);
1977 return;
1978 }
1979
1980 region->resource.destroy = destroy_region;
1981
1982 region->resource.object.id = id;
1983 region->resource.object.interface = &wl_region_interface;
1984 region->resource.object.implementation =
1985 (void (**)(void)) &region_interface;
1986 region->resource.data = region;
1987
1988 pixman_region32_init(&region->region);
1989
1990 wl_client_add_resource(client, &region->resource);
1991}
1992
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001993static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001994 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001995 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001996};
1997
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001998WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001999weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002000{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002001 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2002 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002003
2004 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02002005 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002006}
2007
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002008static void
2009weston_compositor_dpms_on(struct weston_compositor *compositor)
2010{
2011 struct weston_output *output;
2012
2013 wl_list_for_each(output, &compositor->output_list, link)
2014 if (output->set_dpms)
2015 output->set_dpms(output, WESTON_DPMS_ON);
2016}
2017
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002018WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002019weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002020{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002021 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
2022 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002023 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002024 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002025 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002026 }
2027}
2028
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002029static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002030weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002031{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002032 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002033 compositor->idle_inhibit++;
2034}
2035
2036static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002037weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002038{
2039 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002040 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002041}
2042
2043static int
2044idle_handler(void *data)
2045{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002046 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002047
2048 if (compositor->idle_inhibit)
2049 return 1;
2050
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002051 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002052
2053 return 1;
2054}
2055
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002056WL_EXPORT void
2057weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2058{
2059 pixman_region32_init(&plane->damage);
2060 plane->x = x;
2061 plane->y = y;
2062}
2063
2064WL_EXPORT void
2065weston_plane_release(struct weston_plane *plane)
2066{
2067 pixman_region32_fini(&plane->damage);
2068}
2069
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002070static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002071weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002072
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002073static void
Daniel Stone37816df2012-05-16 18:45:18 +01002074clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002075{
Daniel Stone37816df2012-05-16 18:45:18 +01002076 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002077 struct weston_output *output, *prev = NULL;
2078 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002079
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002080 x = wl_fixed_to_int(*fx);
2081 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01002082 old_x = wl_fixed_to_int(seat->seat.pointer->x);
2083 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002084
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002085 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002086 if (pixman_region32_contains_point(&output->region,
2087 x, y, NULL))
2088 valid = 1;
2089 if (pixman_region32_contains_point(&output->region,
2090 old_x, old_y, NULL))
2091 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002092 }
Daniel Stone37816df2012-05-16 18:45:18 +01002093
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002094 if (!valid) {
2095 if (x < prev->x)
2096 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06002097 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002098 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06002099 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002100 if (y < prev->y)
2101 *fy = wl_fixed_from_int(prev->y);
2102 else if (y >= prev->y + prev->current->height)
2103 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06002104 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002105 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002106}
2107
2108WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002109notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002110{
2111 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002112 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002113 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002114 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002115 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002116
2117 weston_compositor_activity(ec);
2118
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002119 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002120
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002121 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002122
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002123 pointer->x = x;
2124 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002125
2126 ix = wl_fixed_to_int(x);
2127 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002128
Scott Moreauccbf29d2012-02-22 14:21:41 -07002129 wl_list_for_each(output, &ec->output_list, link)
2130 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002131 pixman_region32_contains_point(&output->region,
2132 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06002133 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002134
Daniel Stone37816df2012-05-16 18:45:18 +01002135 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002136 interface = pointer->grab->interface;
2137 interface->motion(pointer->grab, time,
2138 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002139
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002140 if (seat->sprite) {
2141 weston_surface_set_position(seat->sprite,
2142 ix - seat->hotspot_x,
2143 iy - seat->hotspot_y);
2144 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002145 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002146}
2147
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002148WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002149weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002150 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05002151{
Daniel Stone37816df2012-05-16 18:45:18 +01002152 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002153
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03002154 if (seat->seat.keyboard) {
2155 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
2156 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04002157 }
2158
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002159 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05002160}
2161
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002162WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002163notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002164 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05002165{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002166 struct weston_compositor *compositor = seat->compositor;
2167 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01002168 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002169 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002170 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05002171
Daniel Stone4dbadb12012-05-30 16:31:51 +01002172 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002173 if (compositor->ping_handler && focus)
2174 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002175 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002176 if (pointer->button_count == 0) {
2177 pointer->grab_button = button;
2178 pointer->grab_time = time;
2179 pointer->grab_x = pointer->x;
2180 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002181 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002182 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002183 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002184 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002185 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002186 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002187
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002188 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002189 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05002190
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002191 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05002192
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002193 if (pointer->button_count == 1)
2194 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002195 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05002196}
2197
Scott Moreau210d0792012-03-22 10:47:01 -06002198WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002199notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01002200 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002201{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002202 struct weston_compositor *compositor = seat->compositor;
2203 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01002204 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002205 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002206 uint32_t serial = wl_display_next_serial(compositor->wl_display);
2207
2208 if (compositor->ping_handler && focus)
2209 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06002210
2211 weston_compositor_activity(compositor);
2212
Scott Moreau6a3633d2012-03-20 08:47:59 -06002213 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002214 weston_compositor_run_axis_binding(compositor, seat,
2215 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06002216 else
2217 return;
2218
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002219 if (pointer->focus_resource)
2220 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01002221 value);
Scott Moreau210d0792012-03-22 10:47:01 -06002222}
2223
Daniel Stone05d58682012-06-22 13:21:38 +01002224WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002225notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002226{
Daniel Stone05d58682012-06-22 13:21:38 +01002227 struct wl_keyboard *keyboard = &seat->keyboard;
2228 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01002229 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002230 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01002231 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01002232 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01002233
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002234 /* Serialize and update our internal state, checking to see if it's
2235 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01002236 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
2237 XKB_STATE_DEPRESSED);
2238 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
2239 XKB_STATE_LATCHED);
2240 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
2241 XKB_STATE_LOCKED);
2242 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01002243 XKB_STATE_EFFECTIVE);
2244
Daniel Stone71c38772012-06-22 13:21:30 +01002245 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
2246 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
2247 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
2248 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01002249 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01002250
Daniel Stone71c38772012-06-22 13:21:30 +01002251 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
2252 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
2253 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
2254 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05002255
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002256 /* And update the modifier_state for bindings. */
2257 mods_lookup = mods_depressed | mods_latched;
2258 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01002259 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002260 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01002261 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002262 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01002263 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002264 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04002265 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
2266 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01002267
Daniel Stone8c8164f2012-05-30 16:31:45 +01002268 /* Finally, notify the compositor that LEDs have changed. */
2269 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01002270 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01002271 leds |= LED_NUM_LOCK;
2272 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01002273 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01002274 leds |= LED_CAPS_LOCK;
2275 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01002276 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01002277 leds |= LED_SCROLL_LOCK;
2278 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01002279 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01002280 seat->xkb_state.leds = leds;
2281
Daniel Stone05d58682012-06-22 13:21:38 +01002282 if (changed) {
2283 grab->interface->modifiers(grab,
2284 serial,
2285 keyboard->modifiers.mods_depressed,
2286 keyboard->modifiers.mods_latched,
2287 keyboard->modifiers.mods_locked,
2288 keyboard->modifiers.group);
2289 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002290}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04002291
Daniel Stone05d58682012-06-22 13:21:38 +01002292static void
2293update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002294 enum wl_keyboard_key_state state)
2295{
2296 enum xkb_key_direction direction;
2297
2298 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2299 direction = XKB_KEY_DOWN;
2300 else
2301 direction = XKB_KEY_UP;
2302
2303 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
2304 * broken keycode system, which starts at 8. */
2305 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
2306
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002307 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002308}
2309
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002310WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002311notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01002312 enum wl_keyboard_key_state state,
2313 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002314{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002315 struct weston_compositor *compositor = seat->compositor;
2316 struct wl_keyboard *keyboard = seat->seat.keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01002317 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002318 (struct weston_surface *) keyboard->focus;
2319 struct wl_keyboard_grab *grab = keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002320 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002321 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002322
Daniel Stonec9785ea2012-05-30 16:31:52 +01002323 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002324 if (compositor->ping_handler && focus)
2325 compositor->ping_handler(focus, serial);
2326
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002327 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002328 keyboard->grab_key = key;
2329 keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07002330 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002331 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07002332 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002333
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002334 end = keyboard->keys.data + keyboard->keys.size;
2335 for (k = keyboard->keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01002336 if (*k == key) {
2337 /* Ignore server-generated repeats. */
2338 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2339 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002340 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01002341 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002342 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002343 keyboard->keys.size = (void *) end - keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002344 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002345 k = wl_array_add(&keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002346 *k = key;
2347 }
Ray Strodee96dcb82008-12-20 02:00:49 -05002348
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002349 if (grab == &keyboard->default_grab) {
2350 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002351 state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002352 grab = keyboard->grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06002353 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05002354
Daniel Stone7ace3902012-05-30 16:31:40 +01002355 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01002356
2357 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002358 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01002359 wl_display_get_serial(compositor->wl_display),
2360 key,
2361 state);
2362 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002363}
2364
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002365WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002366notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01002367 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002368{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002369 struct weston_compositor *compositor = seat->compositor;
2370 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002371
2372 if (output) {
Daniel Stone37816df2012-05-16 18:45:18 +01002373 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002374 x - pointer->x,
2375 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002376
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002377 pointer->x = x;
2378 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002379 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002380 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002381 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002382 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03002383 /* FIXME: We should call wl_pointer_set_focus(seat,
2384 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002385 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002386}
2387
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002388static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002389destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002390{
Daniel Stone37816df2012-05-16 18:45:18 +01002391 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002392
Daniel Stone37816df2012-05-16 18:45:18 +01002393 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002394 saved_kbd_focus_listener);
2395
Daniel Stone37816df2012-05-16 18:45:18 +01002396 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002397}
2398
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002399WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002400notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002401 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002402{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002403 struct weston_compositor *compositor = seat->compositor;
2404 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04002405 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002406 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05002407
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002408 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002409 wl_array_copy(&keyboard->keys, keys);
2410 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002411 weston_compositor_idle_inhibit(compositor);
2412 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002413 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002414 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002415 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002416
Daniel Stoneef172672012-06-22 13:21:32 +01002417 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002418 wl_array_for_each(k, &keyboard->keys) {
2419 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01002420 WL_KEYBOARD_KEY_STATE_PRESSED);
2421 }
2422
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002423 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002424
2425 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002426 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2427 wl_keyboard_set_focus(keyboard, surface);
2428 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002429 }
2430}
2431
2432WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002433notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01002434{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002435 struct weston_compositor *compositor = seat->compositor;
2436 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002437 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002438
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002439 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002440 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002441 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002442 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002443 WL_KEYBOARD_KEY_STATE_RELEASED);
2444 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002445
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002446 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002447
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002448 if (keyboard->focus) {
2449 seat->saved_kbd_focus = keyboard->focus;
2450 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01002451 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002452 wl_signal_add(&keyboard->focus->resource.destroy_signal,
2453 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002454 }
2455
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002456 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002457 /* FIXME: We really need keyboard grab cancel here to
2458 * let the grab shut down properly. As it is we leak
2459 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002460 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002461}
2462
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002463static void
Daniel Stone37816df2012-05-16 18:45:18 +01002464touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002465{
Daniel Stone37816df2012-05-16 18:45:18 +01002466 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002467 struct wl_resource *resource;
2468
Pekka Paalanen56464252012-07-31 13:21:08 +03002469 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002470 return;
2471
Pekka Paalanen56464252012-07-31 13:21:08 +03002472 if (seat->touch->focus_resource)
2473 wl_list_remove(&seat->touch->focus_listener.link);
2474 seat->touch->focus = NULL;
2475 seat->touch->focus_resource = NULL;
2476
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002477 if (surface) {
2478 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01002479 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04002480 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002481 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02002482 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002483 return;
2484 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002485
Daniel Stone37816df2012-05-16 18:45:18 +01002486 seat->touch->focus = surface;
2487 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03002488 wl_signal_add(&resource->destroy_signal,
2489 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002490 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002491}
2492
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002493/**
2494 * notify_touch - emulates button touches and notifies surfaces accordingly.
2495 *
2496 * It assumes always the correct cycle sequence until it gets here: touch_down
2497 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2498 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002499 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002500 */
2501WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002502notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002503 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002504{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002505 struct weston_compositor *ec = seat->compositor;
2506 struct wl_touch *touch = seat->seat.touch;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002507 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002508 wl_fixed_t sx, sy;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002509 uint32_t serial = 0;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002510
2511 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01002512 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002513 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002514
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002515 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002516
2517 /* the first finger down picks the surface, and all further go
2518 * to that surface for the remainder of the touch session i.e.
2519 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002520 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002521 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002522 touch_set_focus(seat, &es->surface);
2523 } else if (touch->focus) {
2524 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002525 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002526 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002527
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002528 if (touch->focus_resource && touch->focus)
2529 wl_touch_send_down(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002530 serial, time,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002531 &touch->focus->resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002532 touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002533 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002534 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002535 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002536 if (!es)
2537 break;
2538
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002539 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002540 if (touch->focus_resource)
2541 wl_touch_send_motion(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002542 time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002543 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002544 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002545 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002546 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002547
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002548 if (touch->focus_resource)
2549 wl_touch_send_up(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002550 serial, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002551 if (seat->num_tp == 0)
2552 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002553 break;
2554 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002555}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002556
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04002557static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002558pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
2559{
2560 struct weston_seat *seat = container_of(listener, struct weston_seat,
2561 sprite_destroy_listener);
2562
2563 seat->sprite = NULL;
2564}
2565
2566static void
2567pointer_cursor_surface_configure(struct weston_surface *es,
2568 int32_t dx, int32_t dy)
2569{
2570 struct weston_seat *seat = es->private;
2571 int x, y;
2572
2573 assert(es == seat->sprite);
2574
2575 seat->hotspot_x -= dx;
2576 seat->hotspot_y -= dy;
2577
2578 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
2579 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
2580
2581 weston_surface_configure(seat->sprite, x, y,
2582 es->buffer->width, es->buffer->height);
2583
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03002584 empty_region(&es->input);
2585
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002586 if (!weston_surface_is_mapped(es)) {
2587 wl_list_insert(&es->compositor->cursor_layer.surface_list,
2588 &es->layer_link);
2589 weston_surface_assign_output(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002590 }
2591}
2592
2593static void
2594pointer_unmap_sprite(struct weston_seat *seat)
2595{
2596 if (weston_surface_is_mapped(seat->sprite))
2597 weston_surface_unmap(seat->sprite);
2598
2599 wl_list_remove(&seat->sprite_destroy_listener.link);
2600 seat->sprite->configure = NULL;
2601 seat->sprite->private = NULL;
2602 seat->sprite = NULL;
2603}
2604
2605static void
2606pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2607 uint32_t serial, struct wl_resource *surface_resource,
2608 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002609{
Daniel Stone37816df2012-05-16 18:45:18 +01002610 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002611 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002612
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002613 if (surface_resource)
2614 surface = container_of(surface_resource->data,
2615 struct weston_surface, surface);
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002616
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04002617 if (seat->seat.pointer->focus == NULL)
2618 return;
2619 if (seat->seat.pointer->focus->resource.client != client)
2620 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002621 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002622 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002623
2624 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002625 if (surface->configure) {
2626 wl_resource_post_error(&surface->surface.resource,
2627 WL_DISPLAY_ERROR_INVALID_OBJECT,
2628 "surface->configure already "
2629 "set");
2630 return;
2631 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002632 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002633
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002634 if (seat->sprite)
2635 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002636
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002637 if (!surface)
2638 return;
2639
2640 wl_signal_add(&surface->surface.resource.destroy_signal,
2641 &seat->sprite_destroy_listener);
2642
2643 surface->configure = pointer_cursor_surface_configure;
2644 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002645 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002646 seat->hotspot_x = x;
2647 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002648
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04002649 if (surface->buffer)
2650 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002651}
2652
Daniel Stone37816df2012-05-16 18:45:18 +01002653static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002654 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002655};
2656
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002657static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002658handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002659{
Daniel Stone37816df2012-05-16 18:45:18 +01002660 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002661
Daniel Stone37816df2012-05-16 18:45:18 +01002662 seat = container_of(listener, struct weston_seat,
2663 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002664
Daniel Stone37816df2012-05-16 18:45:18 +01002665 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002666}
2667
Casey Dahlin9074db52012-04-19 22:50:09 -04002668static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002669{
2670 wl_list_remove(&resource->link);
2671 free(resource);
2672}
2673
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002674static void
Daniel Stone37816df2012-05-16 18:45:18 +01002675seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2676 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002677{
Daniel Stone37816df2012-05-16 18:45:18 +01002678 struct weston_seat *seat = resource->data;
2679 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002680
Daniel Stone37816df2012-05-16 18:45:18 +01002681 if (!seat->seat.pointer)
2682 return;
2683
2684 cr = wl_client_add_object(client, &wl_pointer_interface,
2685 &pointer_interface, id, seat);
2686 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002687 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002688
2689 if (seat->seat.pointer->focus &&
2690 seat->seat.pointer->focus->resource.client == client) {
2691 struct weston_surface *surface;
2692 wl_fixed_t sx, sy;
2693
2694 surface = (struct weston_surface *) seat->seat.pointer->focus;
2695 weston_surface_from_global_fixed(surface,
2696 seat->seat.pointer->x,
2697 seat->seat.pointer->y,
2698 &sx,
2699 &sy);
2700 wl_pointer_set_focus(seat->seat.pointer,
2701 seat->seat.pointer->focus,
2702 sx,
2703 sy);
2704 }
Daniel Stone37816df2012-05-16 18:45:18 +01002705}
2706
2707static void
2708seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2709 uint32_t id)
2710{
2711 struct weston_seat *seat = resource->data;
2712 struct wl_resource *cr;
2713
2714 if (!seat->seat.keyboard)
2715 return;
2716
2717 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2718 seat);
2719 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002720 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002721
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002722 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2723 seat->xkb_info.keymap_fd,
2724 seat->xkb_info.keymap_size);
2725
Daniel Stone17b13bd2012-05-30 16:31:39 +01002726 if (seat->seat.keyboard->focus &&
2727 seat->seat.keyboard->focus->resource.client == client) {
2728 wl_keyboard_set_focus(seat->seat.keyboard,
2729 seat->seat.keyboard->focus);
2730 }
Daniel Stone37816df2012-05-16 18:45:18 +01002731}
2732
2733static void
2734seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2735 uint32_t id)
2736{
2737 struct weston_seat *seat = resource->data;
2738 struct wl_resource *cr;
2739
2740 if (!seat->seat.touch)
2741 return;
2742
2743 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2744 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002745 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002746}
2747
2748static const struct wl_seat_interface seat_interface = {
2749 seat_get_pointer,
2750 seat_get_keyboard,
2751 seat_get_touch,
2752};
2753
2754static void
2755bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2756{
2757 struct wl_seat *seat = data;
2758 struct wl_resource *resource;
2759 enum wl_seat_capability caps = 0;
2760
2761 resource = wl_client_add_object(client, &wl_seat_interface,
2762 &seat_interface, id, data);
2763 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002764 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002765
2766 if (seat->pointer)
2767 caps |= WL_SEAT_CAPABILITY_POINTER;
2768 if (seat->keyboard)
2769 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2770 if (seat->touch)
2771 caps |= WL_SEAT_CAPABILITY_TOUCH;
2772
2773 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002774}
2775
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002776static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002777device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002778{
Daniel Stone37816df2012-05-16 18:45:18 +01002779 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002780
Daniel Stone37816df2012-05-16 18:45:18 +01002781 seat = container_of(listener, struct weston_seat,
2782 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002783
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002784 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002785}
2786
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002787static void weston_compositor_xkb_init(struct weston_compositor *ec,
2788 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002789{
Daniel Stonee379da92012-05-30 16:32:04 +01002790 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002791 ec->xkb_context = xkb_context_new(0);
2792 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002793 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002794 exit(1);
2795 }
Daniel Stone994679a2012-05-30 16:31:43 +01002796 }
2797
2798 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002799 ec->xkb_names = *names;
2800 if (!ec->xkb_names.rules)
2801 ec->xkb_names.rules = strdup("evdev");
2802 if (!ec->xkb_names.model)
2803 ec->xkb_names.model = strdup("pc105");
2804 if (!ec->xkb_names.layout)
2805 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002806}
2807
Daniel Stonee379da92012-05-30 16:32:04 +01002808static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2809{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002810 if (xkb_info->keymap)
2811 xkb_map_unref(xkb_info->keymap);
2812
2813 if (xkb_info->keymap_area)
2814 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2815 if (xkb_info->keymap_fd >= 0)
2816 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002817}
2818
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002819static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2820{
Daniel Stonee379da92012-05-30 16:32:04 +01002821 free((char *) ec->xkb_names.rules);
2822 free((char *) ec->xkb_names.model);
2823 free((char *) ec->xkb_names.layout);
2824 free((char *) ec->xkb_names.variant);
2825 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002826
Daniel Stonee379da92012-05-30 16:32:04 +01002827 xkb_info_destroy(&ec->xkb_info);
2828 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002829}
2830
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002831static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002832weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002833{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002834 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002835
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002836 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2837 XKB_MOD_NAME_SHIFT);
2838 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2839 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002840 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2841 XKB_MOD_NAME_CTRL);
2842 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2843 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002844 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2845 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002846 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2847 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002848 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002849
2850 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2851 XKB_LED_NAME_NUM);
2852 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2853 XKB_LED_NAME_CAPS);
2854 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2855 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002856
2857 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2858 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002859 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002860 exit(EXIT_FAILURE);
2861 }
2862 xkb_info->keymap_size = strlen(keymap_str) + 1;
2863
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002864 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002865 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002866 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002867 (unsigned long) xkb_info->keymap_size);
2868 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002869 }
2870
2871 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2872 PROT_READ | PROT_WRITE,
2873 MAP_SHARED, xkb_info->keymap_fd, 0);
2874 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002875 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002876 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002877 goto err_dev_zero;
2878 }
2879 strcpy(xkb_info->keymap_area, keymap_str);
2880 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002881
2882 return;
2883
2884err_dev_zero:
2885 close(xkb_info->keymap_fd);
2886 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002887err_keymap_str:
2888 free(keymap_str);
2889 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002890}
2891
2892static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002893weston_compositor_build_global_keymap(struct weston_compositor *ec)
2894{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002895 if (ec->xkb_info.keymap != NULL)
2896 return;
2897
Daniel Stonee379da92012-05-30 16:32:04 +01002898 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002899 &ec->xkb_names,
2900 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002901 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002902 weston_log("failed to compile global XKB keymap\n");
2903 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002904 "options %s",
2905 ec->xkb_names.rules, ec->xkb_names.model,
2906 ec->xkb_names.layout, ec->xkb_names.variant,
2907 ec->xkb_names.options);
2908 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002909 }
2910
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002911 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002912}
2913
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002914WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002915weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002916{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002917 if (seat->has_keyboard)
2918 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002919
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002920 if (keymap != NULL) {
2921 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002922 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002923 }
2924 else {
2925 weston_compositor_build_global_keymap(seat->compositor);
2926 seat->xkb_info = seat->compositor->xkb_info;
2927 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2928 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002929
2930 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2931 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002932 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01002933 exit(1);
2934 }
2935
Daniel Stone71c38772012-06-22 13:21:30 +01002936 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002937
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002938 wl_keyboard_init(&seat->keyboard);
2939 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2940
2941 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01002942}
2943
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002944WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002945weston_seat_init_pointer(struct weston_seat *seat)
2946{
2947 if (seat->has_pointer)
2948 return;
2949
2950 wl_pointer_init(&seat->pointer);
2951 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2952
2953 seat->has_pointer = 1;
2954}
2955
2956WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002957weston_seat_init_touch(struct weston_seat *seat)
2958{
2959 if (seat->has_touch)
2960 return;
2961
2962 wl_touch_init(&seat->touch);
2963 wl_seat_set_touch(&seat->seat, &seat->touch);
2964
2965 seat->has_touch = 1;
2966}
2967
2968WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002969weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002970{
Daniel Stone37816df2012-05-16 18:45:18 +01002971 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002972 seat->has_pointer = 0;
2973 seat->has_keyboard = 0;
2974 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002975
Daniel Stone37816df2012-05-16 18:45:18 +01002976 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2977 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002978
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002979 seat->sprite = NULL;
2980 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002981
Daniel Stone37816df2012-05-16 18:45:18 +01002982 seat->compositor = ec;
2983 seat->hotspot_x = 16;
2984 seat->hotspot_y = 16;
2985 seat->modifier_state = 0;
2986 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002987
Daniel Stone37816df2012-05-16 18:45:18 +01002988 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002989 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002990
Daniel Stone37816df2012-05-16 18:45:18 +01002991 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002992
Daniel Stone37816df2012-05-16 18:45:18 +01002993 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2994 wl_signal_add(&seat->seat.drag_icon_signal,
2995 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002996
2997 clipboard_create(seat);
Jan Arne Petersene829adc2012-08-10 16:47:22 +02002998 input_method_create(ec, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002999}
3000
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003001WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01003002weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003003{
Daniel Stone37816df2012-05-16 18:45:18 +01003004 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003005 /* The global object is destroyed at wl_display_destroy() time. */
3006
Daniel Stone37816df2012-05-16 18:45:18 +01003007 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003008 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003009
Daniel Stone74419a22012-05-30 16:32:02 +01003010 if (seat->xkb_state.state != NULL)
3011 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01003012 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01003013
Daniel Stone37816df2012-05-16 18:45:18 +01003014 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003015}
3016
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003017static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003018drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
3019{
3020 weston_surface_configure(es,
3021 es->geometry.x + sx, es->geometry.y + sy,
3022 es->buffer->width, es->buffer->height);
3023}
3024
3025static int
Daniel Stone37816df2012-05-16 18:45:18 +01003026device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003027 struct weston_surface *surface)
3028{
Daniel Stone37816df2012-05-16 18:45:18 +01003029 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003030
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003031 if (surface->configure) {
3032 wl_resource_post_error(&surface->surface.resource,
3033 WL_DISPLAY_ERROR_INVALID_OBJECT,
3034 "surface->configure already set");
3035 return 0;
3036 }
3037
Daniel Stone37816df2012-05-16 18:45:18 +01003038 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003039
Daniel Stone37816df2012-05-16 18:45:18 +01003040 weston_surface_set_position(ws->drag_surface,
3041 wl_fixed_to_double(seat->pointer->x),
3042 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003043
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003044 surface->configure = drag_surface_configure;
3045
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003046 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01003047 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003048
3049 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003050}
3051
3052static void
Daniel Stone37816df2012-05-16 18:45:18 +01003053device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003054{
Daniel Stone37816df2012-05-16 18:45:18 +01003055 seat->drag_surface->configure = NULL;
3056 undef_region(&seat->drag_surface->input);
3057 wl_list_remove(&seat->drag_surface_destroy_listener.link);
3058 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003059}
3060
3061static void
Daniel Stone37816df2012-05-16 18:45:18 +01003062device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003063{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003064 struct wl_list *list;
3065
Daniel Stone37816df2012-05-16 18:45:18 +01003066 if (weston_surface_is_mapped(seat->drag_surface) ||
3067 !seat->drag_surface->buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003068 return;
3069
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003070 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
3071 list = &seat->sprite->layer_link;
3072 else
3073 list = &seat->compositor->cursor_layer.surface_list;
3074
3075 wl_list_insert(list, &seat->drag_surface->layer_link);
Daniel Stone37816df2012-05-16 18:45:18 +01003076 weston_surface_assign_output(seat->drag_surface);
3077 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003078}
3079
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003080static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003081weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01003082 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003083{
3084 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003085
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003086 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003087 return;
3088
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003089 if (seat->drag_surface && seat->seat.drag_surface &&
3090 (&seat->drag_surface->surface.resource !=
3091 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003092 /* between calls to this funcion we got a new drag_surface */
3093 surface_changed = 1;
3094
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003095 if (!seat->seat.drag_surface || surface_changed) {
3096 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003097 if (!surface_changed)
3098 return;
3099 }
3100
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003101 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003102 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003103 seat->seat.drag_surface;
3104 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003105 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003106 }
3107
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003108 /* the client may not have attached a buffer to the drag surface
3109 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003110 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003111
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02003112 /* the client may have attached a buffer with a different size to
3113 * the drag surface, causing the input region to be reset */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003114 if (region_is_undefined(&seat->drag_surface->input))
3115 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02003116
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003117 if (!dx && !dy)
3118 return;
3119
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003120 weston_surface_set_position(seat->drag_surface,
3121 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
3122 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003123}
3124
3125WL_EXPORT void
3126weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
3127{
Daniel Stone4dab5db2012-05-30 16:31:53 +01003128 struct weston_seat *seat;
3129
3130 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003131 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003132}
3133
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003134static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003135bind_output(struct wl_client *client,
3136 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003137{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003138 struct weston_output *output = data;
3139 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003140 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003141
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003142 resource = wl_client_add_object(client,
3143 &wl_output_interface, NULL, id, data);
3144
Casey Dahlin9074db52012-04-19 22:50:09 -04003145 wl_list_insert(&output->resource_list, &resource->link);
3146 resource->destroy = unbind_resource;
3147
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003148 wl_output_send_geometry(resource,
3149 output->x,
3150 output->y,
3151 output->mm_width,
3152 output->mm_height,
3153 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003154 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003155 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003156
3157 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003158 wl_output_send_mode(resource,
3159 mode->flags,
3160 mode->width,
3161 mode->height,
3162 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003163 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003164}
3165
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003166static const char vertex_shader[] =
3167 "uniform mat4 proj;\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05003168 "attribute vec2 position;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003169 "attribute vec2 texcoord;\n"
3170 "varying vec2 v_texcoord;\n"
3171 "void main()\n"
3172 "{\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05003173 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003174 " v_texcoord = texcoord;\n"
3175 "}\n";
3176
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02003177/* Declare common fragment shader uniforms */
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003178#define FRAGMENT_CONVERT_YUV \
Rob Clark0e5a2d02012-08-30 16:47:18 -05003179 " y *= alpha;\n" \
3180 " u *= alpha;\n" \
3181 " v *= alpha;\n" \
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003182 " gl_FragColor.r = y + 1.59602678 * v;\n" \
3183 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
3184 " gl_FragColor.b = y + 2.01723214 * u;\n" \
Rob Clark0e5a2d02012-08-30 16:47:18 -05003185 " gl_FragColor.a = alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003186
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02003187static const char texture_fragment_shader_rgba[] =
Kristian Høgsbergdfce71d2010-12-07 20:19:10 -05003188 "precision mediump float;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003189 "varying vec2 v_texcoord;\n"
3190 "uniform sampler2D tex;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003191 "uniform float alpha;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003192 "void main()\n"
3193 "{\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003194 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003195 "}\n";
3196
Pekka Paalanen6b5585b2012-08-30 16:47:19 -05003197static const char texture_fragment_shader_rgbx[] =
3198 "precision mediump float;\n"
3199 "varying vec2 v_texcoord;\n"
3200 "uniform sampler2D tex;\n"
3201 "uniform float alpha;\n"
3202 "void main()\n"
3203 "{\n"
3204 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
3205 " gl_FragColor.a = alpha;\n"
3206 "}\n";
3207
Rob Clarke3b95912012-08-31 16:42:18 -05003208static const char texture_fragment_shader_egl_external[] =
3209 "#extension GL_OES_EGL_image_external : require\n"
3210 "precision mediump float;\n"
3211 "varying vec2 v_texcoord;\n"
3212 "uniform samplerExternalOES tex;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003213 "uniform float alpha;\n"
Rob Clarke3b95912012-08-31 16:42:18 -05003214 "void main()\n"
3215 "{\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003216 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Rob Clarke3b95912012-08-31 16:42:18 -05003217 "}\n";
3218
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003219static const char texture_fragment_shader_y_uv[] =
3220 "precision mediump float;\n"
3221 "uniform sampler2D tex;\n"
3222 "uniform sampler2D tex1;\n"
3223 "varying vec2 v_texcoord;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003224 "uniform float alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003225 "void main() {\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003226 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
3227 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
3228 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
3229 FRAGMENT_CONVERT_YUV
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003230 "}\n";
3231
3232static const char texture_fragment_shader_y_u_v[] =
3233 "precision mediump float;\n"
3234 "uniform sampler2D tex;\n"
3235 "uniform sampler2D tex1;\n"
3236 "uniform sampler2D tex2;\n"
3237 "varying vec2 v_texcoord;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003238 "uniform float alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003239 "void main() {\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003240 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
3241 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
3242 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
3243 FRAGMENT_CONVERT_YUV
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003244 "}\n";
3245
3246static const char texture_fragment_shader_y_xuxv[] =
3247 "precision mediump float;\n"
3248 "uniform sampler2D tex;\n"
3249 "uniform sampler2D tex1;\n"
3250 "varying vec2 v_texcoord;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003251 "uniform float alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003252 "void main() {\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003253 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
3254 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
3255 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
3256 FRAGMENT_CONVERT_YUV
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003257 "}\n";
3258
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003259static const char solid_fragment_shader[] =
3260 "precision mediump float;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003261 "uniform vec4 color;\n"
Kristian Høgsbergdbae80e2012-03-29 11:34:39 -04003262 "uniform float alpha;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003263 "void main()\n"
3264 "{\n"
Kristian Høgsbergdbae80e2012-03-29 11:34:39 -04003265 " gl_FragColor = alpha * color\n;"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003266 "}\n";
3267
Kristian Høgsberga9468212010-06-14 21:03:11 -04003268static int
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003269compile_shader(GLenum type, const char *source)
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003270{
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003271 GLuint s;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003272 char msg[512];
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003273 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003274
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003275 s = glCreateShader(type);
3276 glShaderSource(s, 1, &source, NULL);
3277 glCompileShader(s);
3278 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003279 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003280 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
Martin Minarik6d118362012-06-07 18:01:59 +02003281 weston_log("shader info: %s\n", msg);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003282 return GL_NONE;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003283 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003284
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003285 return s;
3286}
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003287
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003288static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003289weston_shader_init(struct weston_shader *shader,
3290 const char *vertex_source, const char *fragment_source)
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003291{
3292 char msg[512];
3293 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003294
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003295 shader->vertex_shader =
3296 compile_shader(GL_VERTEX_SHADER, vertex_source);
3297 shader->fragment_shader =
3298 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
3299
3300 shader->program = glCreateProgram();
3301 glAttachShader(shader->program, shader->vertex_shader);
3302 glAttachShader(shader->program, shader->fragment_shader);
3303 glBindAttribLocation(shader->program, 0, "position");
3304 glBindAttribLocation(shader->program, 1, "texcoord");
3305
3306 glLinkProgram(shader->program);
3307 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003308 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003309 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
Martin Minarik6d118362012-06-07 18:01:59 +02003310 weston_log("link info: %s\n", msg);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003311 return -1;
3312 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003313
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003314 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02003315 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
3316 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
3317 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
Kristian Høgsberg541e5552011-12-14 09:24:11 -05003318 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
Pekka Paalanenf55f5442012-02-02 16:49:05 +02003319 shader->color_uniform = glGetUniformLocation(shader->program, "color");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003320
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003321 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003322}
3323
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003324WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003325weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003326{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003327 struct weston_compositor *c = output->compositor;
3328
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003329 pixman_region32_fini(&output->region);
Kristian Høgsberg8b72f602011-06-23 20:46:34 -04003330 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003331 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003332
3333 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003334}
3335
Scott Moreau1bad5db2012-08-18 01:04:05 -06003336static void
3337weston_output_compute_transform(struct weston_output *output)
3338{
3339 struct weston_matrix transform;
3340 int flip;
3341
3342 weston_matrix_init(&transform);
3343
3344 switch(output->transform) {
3345 case WL_OUTPUT_TRANSFORM_FLIPPED:
3346 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3347 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3348 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3349 flip = -1;
3350 break;
3351 default:
3352 flip = 1;
3353 break;
3354 }
3355
3356 switch(output->transform) {
3357 case WL_OUTPUT_TRANSFORM_NORMAL:
3358 case WL_OUTPUT_TRANSFORM_FLIPPED:
3359 transform.d[0] = flip;
3360 transform.d[1] = 0;
3361 transform.d[4] = 0;
3362 transform.d[5] = 1;
3363 break;
3364 case WL_OUTPUT_TRANSFORM_90:
3365 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3366 transform.d[0] = 0;
3367 transform.d[1] = -flip;
3368 transform.d[4] = 1;
3369 transform.d[5] = 0;
3370 break;
3371 case WL_OUTPUT_TRANSFORM_180:
3372 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3373 transform.d[0] = -flip;
3374 transform.d[1] = 0;
3375 transform.d[4] = 0;
3376 transform.d[5] = -1;
3377 break;
3378 case WL_OUTPUT_TRANSFORM_270:
3379 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3380 transform.d[0] = 0;
3381 transform.d[1] = flip;
3382 transform.d[4] = -1;
3383 transform.d[5] = 0;
3384 break;
3385 default:
3386 break;
3387 }
3388
3389 weston_matrix_multiply(&output->matrix, &transform);
3390}
3391
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003392WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003393weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003394{
Scott Moreau850ca422012-05-21 15:21:25 -06003395 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003396 struct weston_matrix camera;
3397 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003398
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003399 weston_matrix_init(&output->matrix);
3400 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003401 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
3402 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003403
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003404 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003405 2.0 / (output->width + output->border.left + output->border.right),
3406 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
3407
3408 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003409
Scott Moreauccbf29d2012-02-22 14:21:41 -07003410 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003411 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003412 weston_matrix_init(&camera);
3413 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06003414 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06003415 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003416 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003417 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003418 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003419 weston_matrix_multiply(&output->matrix, &modelview);
3420 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003421
Scott Moreauccbf29d2012-02-22 14:21:41 -07003422 output->dirty = 0;
3423}
3424
Scott Moreau1bad5db2012-08-18 01:04:05 -06003425static void
3426weston_output_transform_init(struct weston_output *output, uint32_t transform)
3427{
3428 output->transform = transform;
3429
3430 switch (transform) {
3431 case WL_OUTPUT_TRANSFORM_90:
3432 case WL_OUTPUT_TRANSFORM_270:
3433 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3434 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3435 /* Swap width and height */
3436 output->width = output->current->height;
3437 output->height = output->current->width;
3438 break;
3439 case WL_OUTPUT_TRANSFORM_NORMAL:
3440 case WL_OUTPUT_TRANSFORM_180:
3441 case WL_OUTPUT_TRANSFORM_FLIPPED:
3442 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3443 output->width = output->current->width;
3444 output->height = output->current->height;
3445 break;
3446 default:
3447 break;
3448 }
3449}
3450
Scott Moreauccbf29d2012-02-22 14:21:41 -07003451WL_EXPORT void
3452weston_output_move(struct weston_output *output, int x, int y)
3453{
3454 output->x = x;
3455 output->y = y;
3456
3457 pixman_region32_init(&output->previous_damage);
3458 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003459 output->width,
3460 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003461}
3462
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003463WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003464weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003465 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003466{
3467 output->compositor = c;
3468 output->x = x;
3469 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05003470 output->border.top = 0;
3471 output->border.bottom = 0;
3472 output->border.left = 0;
3473 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003474 output->mm_width = width;
3475 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003476 output->dirty = 1;
3477
Scott Moreau1bad5db2012-08-18 01:04:05 -06003478 if (transform != WL_OUTPUT_TRANSFORM_NORMAL)
3479 output->disable_planes++;
3480
3481 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06003482 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003483
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003484 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003485 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003486
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003487 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003488 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003489 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003490
Casey Dahlin58ba1372012-04-19 22:50:08 -04003491 output->id = ffs(~output->compositor->output_id_pool) - 1;
3492 output->compositor->output_id_pool |= 1 << output->id;
3493
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003494 output->global =
3495 wl_display_add_global(c->wl_display, &wl_output_interface,
3496 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003497}
3498
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003499static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003500compositor_bind(struct wl_client *client,
3501 void *data, uint32_t version, uint32_t id)
3502{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003503 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003504
3505 wl_client_add_object(client, &wl_compositor_interface,
3506 &compositor_interface, id, compositor);
3507}
3508
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003509static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003510log_uname(void)
3511{
3512 struct utsname usys;
3513
3514 uname(&usys);
3515
3516 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3517 usys.version, usys.machine);
3518}
3519
3520static void
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003521log_extensions(const char *name, const char *extensions)
3522{
3523 const char *p, *end;
3524 int l;
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003525 int len;
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003526
3527 l = weston_log("%s:", name);
3528 p = extensions;
3529 while (*p) {
3530 end = strchrnul(p, ' ');
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003531 len = end - p;
3532 if (l + len > 78)
Pekka Paalanen4e4167d2012-06-11 14:06:05 +03003533 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003534 len, p);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003535 else
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003536 l += weston_log_continue(" %.*s", len, p);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003537 for (p = end; isspace(*p); p++)
3538 ;
3539 }
3540 weston_log_continue("\n");
3541}
3542
Pekka Paalanen4e4167d2012-06-11 14:06:05 +03003543static void
3544log_egl_gl_info(EGLDisplay egldpy)
3545{
3546 const char *str;
3547
3548 str = eglQueryString(egldpy, EGL_VERSION);
3549 weston_log("EGL version: %s\n", str ? str : "(null)");
3550
3551 str = eglQueryString(egldpy, EGL_VENDOR);
3552 weston_log("EGL vendor: %s\n", str ? str : "(null)");
3553
3554 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
3555 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
3556
3557 str = eglQueryString(egldpy, EGL_EXTENSIONS);
3558 log_extensions("EGL extensions", str ? str : "(null)");
3559
3560 str = (char *)glGetString(GL_VERSION);
3561 weston_log("GL version: %s\n", str ? str : "(null)");
3562
3563 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
3564 weston_log("GLSL version: %s\n", str ? str : "(null)");
3565
3566 str = (char *)glGetString(GL_VENDOR);
3567 weston_log("GL vendor: %s\n", str ? str : "(null)");
3568
3569 str = (char *)glGetString(GL_RENDERER);
3570 weston_log("GL renderer: %s\n", str ? str : "(null)");
3571
3572 str = (char *)glGetString(GL_EXTENSIONS);
3573 log_extensions("GL extensions", str ? str : "(null)");
3574}
3575
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003576WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003577weston_compositor_init(struct weston_compositor *ec,
3578 struct wl_display *display,
3579 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003580 char *argv[],
3581 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003582{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003583 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003584 struct xkb_rule_names xkb_names;
3585 const struct config_key keyboard_config_keys[] = {
3586 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
3587 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
3588 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
3589 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
3590 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
3591 };
3592 const struct config_section cs[] = {
3593 { "keyboard",
3594 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
3595 };
3596
3597 memset(&xkb_names, 0, sizeof(xkb_names));
3598 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003599
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003600 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003601 wl_signal_init(&ec->destroy_signal);
3602 wl_signal_init(&ec->activate_signal);
3603 wl_signal_init(&ec->lock_signal);
3604 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003605 wl_signal_init(&ec->show_input_panel_signal);
3606 wl_signal_init(&ec->hide_input_panel_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01003607 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003608
Casey Dahlin58ba1372012-04-19 22:50:08 -04003609 ec->output_id_pool = 0;
3610
Kristian Høgsberga8873122011-11-23 10:39:34 -05003611 if (!wl_display_add_global(display, &wl_compositor_interface,
3612 ec, compositor_bind))
3613 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003614
Daniel Stone725c2c32012-06-22 14:04:36 +01003615 wl_list_init(&ec->surface_list);
3616 wl_list_init(&ec->layer_list);
3617 wl_list_init(&ec->seat_list);
3618 wl_list_init(&ec->output_list);
3619 wl_list_init(&ec->key_binding_list);
3620 wl_list_init(&ec->button_binding_list);
3621 wl_list_init(&ec->axis_binding_list);
3622 wl_list_init(&ec->fade.animation.link);
3623
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003624 weston_plane_init(&ec->primary_plane, 0, 0);
3625
Daniel Stone725c2c32012-06-22 14:04:36 +01003626 weston_compositor_xkb_init(ec, &xkb_names);
3627
3628 ec->ping_handler = NULL;
3629
3630 screenshooter_create(ec);
3631 text_cursor_position_notifier_create(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003632
3633 wl_data_device_manager_init(ec->wl_display);
3634
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003635 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003636
Daniel Stone725c2c32012-06-22 14:04:36 +01003637 loop = wl_display_get_event_loop(ec->wl_display);
3638 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3639 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3640
3641 ec->input_loop = wl_event_loop_create();
3642
3643 return 0;
3644}
3645
3646WL_EXPORT int
3647weston_compositor_init_gl(struct weston_compositor *ec)
3648{
3649 const char *extensions;
Rob Clarke3b95912012-08-31 16:42:18 -05003650 int has_egl_image_external = 0;
Daniel Stone725c2c32012-06-22 14:04:36 +01003651
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003652 log_egl_gl_info(ec->egl_display);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003653
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003654 ec->image_target_texture_2d =
3655 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3656 ec->image_target_renderbuffer_storage = (void *)
3657 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
3658 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3659 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3660 ec->bind_display =
3661 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
3662 ec->unbind_display =
3663 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02003664 ec->query_buffer =
3665 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003666
3667 extensions = (const char *) glGetString(GL_EXTENSIONS);
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003668 if (!extensions) {
Martin Minarik6d118362012-06-07 18:01:59 +02003669 weston_log("Retrieving GL extension string failed.\n");
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003670 return -1;
3671 }
3672
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003673 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
Martin Minarik6d118362012-06-07 18:01:59 +02003674 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003675 return -1;
3676 }
3677
Pekka Paalanena1d57db2012-04-17 15:02:08 +03003678 if (strstr(extensions, "GL_EXT_read_format_bgra"))
3679 ec->read_format = GL_BGRA_EXT;
3680 else
3681 ec->read_format = GL_RGBA;
Kristian Høgsbergf9247dd2012-03-27 15:25:46 -04003682
Pekka Paalanen52bfbaa2012-04-11 16:19:37 +03003683 if (strstr(extensions, "GL_EXT_unpack_subimage"))
3684 ec->has_unpack_subimage = 1;
Kristian Høgsbergfb6de222012-03-27 17:10:13 -04003685
Rob Clarke3b95912012-08-31 16:42:18 -05003686 if (strstr(extensions, "GL_OES_EGL_image_external"))
3687 has_egl_image_external = 1;
3688
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003689 extensions =
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003690 (const char *) eglQueryString(ec->egl_display, EGL_EXTENSIONS);
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003691 if (!extensions) {
Martin Minarik6d118362012-06-07 18:01:59 +02003692 weston_log("Retrieving EGL extension string failed.\n");
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003693 return -1;
3694 }
3695
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003696 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
3697 ec->has_bind_display = 1;
3698 if (ec->has_bind_display)
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003699 ec->bind_display(ec->egl_display, ec->wl_display);
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04003700
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003701 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003702 ec->fade.animation.frame = fade_frame;
Kristian Høgsberg3555d092011-04-11 13:58:13 -04003703
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003704 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3705 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3706
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003707 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003708
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02003709 if (weston_shader_init(&ec->texture_shader_rgba,
3710 vertex_shader, texture_fragment_shader_rgba) < 0)
Kristian Høgsberga9468212010-06-14 21:03:11 -04003711 return -1;
Pekka Paalanen6b5585b2012-08-30 16:47:19 -05003712 if (weston_shader_init(&ec->texture_shader_rgbx,
3713 vertex_shader, texture_fragment_shader_rgbx) < 0)
3714 return -1;
Rob Clarke3b95912012-08-31 16:42:18 -05003715 if (has_egl_image_external &&
3716 weston_shader_init(&ec->texture_shader_egl_external,
3717 vertex_shader, texture_fragment_shader_egl_external) < 0)
3718 return -1;
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003719 if (weston_shader_init(&ec->texture_shader_y_uv,
3720 vertex_shader, texture_fragment_shader_y_uv) < 0)
3721 return -1;
3722 if (weston_shader_init(&ec->texture_shader_y_u_v,
3723 vertex_shader, texture_fragment_shader_y_u_v) < 0)
3724 return -1;
3725 if (weston_shader_init(&ec->texture_shader_y_xuxv,
3726 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
3727 return -1;
Pekka Paalanenf55f5442012-02-02 16:49:05 +02003728 if (weston_shader_init(&ec->solid_shader,
3729 vertex_shader, solid_fragment_shader) < 0)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003730 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05003731
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003732 weston_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003733
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003734 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003735}
3736
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003737WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003738weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003739{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003740 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003741
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003742 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003743 if (ec->input_loop_source)
3744 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003745
Matt Roper361d2ad2011-08-29 13:52:23 -07003746 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003747 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003748 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003749
Daniel Stone325fc2d2012-05-30 16:31:58 +01003750 weston_binding_list_destroy_all(&ec->key_binding_list);
3751 weston_binding_list_destroy_all(&ec->button_binding_list);
3752 weston_binding_list_destroy_all(&ec->axis_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003753
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003754 weston_plane_release(&ec->primary_plane);
3755
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003756 wl_array_release(&ec->vertices);
3757 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05003758 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003759
3760 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07003761}
3762
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003763static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003764{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003765 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003766
Martin Minarik6d118362012-06-07 18:01:59 +02003767 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003768 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003769
3770 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003771}
3772
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003773static void
3774on_segv_signal(int s, siginfo_t *siginfo, void *context)
3775{
3776 void *buffer[32];
3777 int i, count;
3778 Dl_info info;
3779
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003780 /* This SIGSEGV handler will do a best-effort backtrace, and
3781 * then call the backend restore function, which will switch
3782 * back to the vt we launched from or ungrab X etc and then
3783 * raise SIGTRAP. If we run weston under gdb from X or a
3784 * different vt, and tell gdb "handle SIGSEGV nostop", this
3785 * will allow weston to switch back to gdb on crash and then
3786 * gdb will catch the crash with SIGTRAP. */
3787
Martin Minarik6d118362012-06-07 18:01:59 +02003788 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003789
3790 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3791 for (i = 0; i < count; i++) {
3792 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02003793 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003794 (long) buffer[i],
3795 info.dli_sname ? info.dli_sname : "--",
3796 info.dli_fname);
3797 }
3798
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003799 segv_compositor->restore(segv_compositor);
3800
3801 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003802}
3803
3804
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003805static void *
3806load_module(const char *name, const char *entrypoint, void **handle)
3807{
3808 char path[PATH_MAX];
3809 void *module, *init;
3810
3811 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003812 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003813 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003814 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003815
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003816 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003817 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003818 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003819 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003820 return NULL;
3821 }
3822
3823 init = dlsym(module, entrypoint);
3824 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003825 weston_log("Failed to lookup init function: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003826 return NULL;
3827 }
3828
3829 return init;
3830}
3831
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003832static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003833 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3834
3835static const char xdg_wrong_message[] =
3836 "fatal: environment variable XDG_RUNTIME_DIR\n"
3837 "is set to \"%s\", which is not a directory.\n";
3838
3839static const char xdg_wrong_mode_message[] =
3840 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3841 "correctly. Unix access mode must be 0700 but is %o,\n"
3842 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003843 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003844
3845static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003846 "Refer to your distribution on how to get it, or\n"
3847 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3848 "on how to implement it.\n";
3849
Martin Minarik37032f82012-06-18 20:15:18 +02003850static void
3851verify_xdg_runtime_dir(void)
3852{
3853 char *dir = getenv("XDG_RUNTIME_DIR");
3854 struct stat s;
3855
3856 if (!dir) {
3857 weston_log(xdg_error_message);
3858 weston_log_continue(xdg_detail_message);
3859 exit(EXIT_FAILURE);
3860 }
3861
3862 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3863 weston_log(xdg_wrong_message, dir);
3864 weston_log_continue(xdg_detail_message);
3865 exit(EXIT_FAILURE);
3866 }
3867
3868 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3869 weston_log(xdg_wrong_mode_message,
3870 dir, s.st_mode & 0777, s.st_uid);
3871 weston_log_continue(xdg_detail_message);
3872 }
3873}
3874
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003875static int
3876usage(int error_code)
3877{
3878 fprintf(stderr,
3879 "Usage: weston [OPTIONS]\n\n"
3880 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3881 "Weston supports multiple backends, and depending on which backend is in use\n"
3882 "different options will be accepted.\n\n"
3883
3884
3885 "Core options:\n\n"
3886 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3887 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3888 " -S, --socket=NAME\tName of socket to listen on\n"
3889 " -i, --idle-time=SECS\tIdle time in seconds\n"
3890 " --xserver\t\tEnable X server integration\n"
3891 " --module\t\tLoad the specified module\n"
3892 " --log==FILE\t\tLog to the given file\n"
3893 " -h, --help\t\tThis help message\n\n");
3894
3895 fprintf(stderr,
3896 "Options for drm-backend.so:\n\n"
3897 " --connector=ID\tBring up only this connector\n"
3898 " --seat=SEAT\t\tThe seat that weston should run on\n"
3899 " --tty=TTY\t\tThe tty to use\n"
3900 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3901
3902 fprintf(stderr,
3903 "Options for x11-backend.so:\n\n"
3904 " --width=WIDTH\t\tWidth of X window\n"
3905 " --height=HEIGHT\tHeight of X window\n"
3906 " --fullscreen\t\tRun in fullscreen mode\n"
3907 " --output-count=COUNT\tCreate multiple outputs\n"
3908 " --no-input\t\tDont create input devices\n\n");
3909
3910 fprintf(stderr,
3911 "Options for wayland-backend.so:\n\n"
3912 " --width=WIDTH\t\tWidth of Wayland surface\n"
3913 " --height=HEIGHT\tHeight of Wayland surface\n"
3914 " --display=DISPLAY\tWayland display to connect to\n\n");
3915
3916 exit(error_code);
3917}
3918
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003919int main(int argc, char *argv[])
3920{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003921 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003922 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003923 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003924 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003925 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003926 struct sigaction segv_action;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04003927 void *shell_module, *backend_module, *xserver_module;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003928 int (*module_init)(struct weston_compositor *ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003929 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003930 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003931 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003932 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003933 char *backend = NULL;
3934 char *shell = NULL;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003935 char *module = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003936 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003937 int32_t idle_time = 300;
Scott Moreaue17f6102012-04-25 10:03:06 -06003938 int32_t xserver = 0;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003939 int32_t help = 0;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003940 char *socket_name = NULL;
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003941 char *config_file;
3942
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003943 const struct config_key shell_config_keys[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003944 { "type", CONFIG_KEY_STRING, &shell },
3945 };
3946
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003947 const struct config_section cs[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003948 { "shell",
3949 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
3950 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003951
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003952 const struct weston_option core_options[] = {
3953 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3954 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3955 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003956 { WESTON_OPTION_BOOLEAN, "xserver", 0, &xserver },
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003957 { WESTON_OPTION_STRING, "module", 0, &module },
Martin Minarik19e6f262012-06-07 13:08:46 +02003958 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003959 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003960 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003961
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003962 argc = parse_options(core_options,
3963 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003964
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003965 if (help)
3966 usage(EXIT_SUCCESS);
3967
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003968 weston_log_file_open(log);
3969
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003970 weston_log("%s\n"
3971 STAMP_SPACE "%s\n"
3972 STAMP_SPACE "Bug reports to: %s\n"
3973 STAMP_SPACE "Build: %s\n",
3974 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003975 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003976 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003977
Martin Minarik37032f82012-06-18 20:15:18 +02003978 verify_xdg_runtime_dir();
3979
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003980 display = wl_display_create();
3981
Tiago Vignatti2116b892011-08-08 05:52:59 -07003982 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003983 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3984 display);
3985 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3986 display);
3987 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3988 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003989
3990 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003991 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3992 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003993
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003994 if (!backend) {
3995 if (getenv("WAYLAND_DISPLAY"))
3996 backend = "wayland-backend.so";
3997 else if (getenv("DISPLAY"))
3998 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003999 else
4000 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004001 }
4002
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004003 config_file = config_file_path("weston.ini");
4004 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004005
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004006 backend_init = load_module(backend, "backend_init", &backend_module);
4007 if (!backend_init)
4008 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004009
Daniel Stonec1be8e52012-06-01 11:14:02 -04004010 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004011 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004012 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004013 exit(EXIT_FAILURE);
4014 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004015
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004016 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4017 segv_action.sa_sigaction = on_segv_signal;
4018 sigemptyset(&segv_action.sa_mask);
4019 sigaction(SIGSEGV, &segv_action, NULL);
4020 segv_compositor = ec;
4021
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004022 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03004023 weston_log("fatal: unhandled option: %s\n", argv[i]);
4024 if (argv[1]) {
4025 ret = EXIT_FAILURE;
4026 goto out;
4027 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004028
Daniel Stonec1be8e52012-06-01 11:14:02 -04004029 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01004030
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004031 ec->option_idle_time = idle_time;
4032 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004033
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004034 module_init = NULL;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04004035 if (xserver)
Tiago Vignatti629ce232012-05-23 23:04:14 +03004036 module_init = load_module("xwayland.so",
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004037 "weston_xserver_init",
4038 &xserver_module);
Pekka Paalanen33616392012-07-30 16:56:57 +03004039 if (module_init && module_init(ec) < 0) {
4040 ret = EXIT_FAILURE;
4041 goto out;
4042 }
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04004043
Kristian Høgsberg33d75092012-08-13 13:56:03 -04004044 if (socket_name)
4045 setenv("WAYLAND_DISPLAY", socket_name, 1);
4046
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004047 if (!shell)
4048 shell = "desktop-shell.so";
4049 module_init = load_module(shell, "shell_init", &shell_module);
Pekka Paalanen33616392012-07-30 16:56:57 +03004050 if (!module_init || module_init(ec) < 0) {
4051 ret = EXIT_FAILURE;
4052 goto out;
4053 }
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004054
4055
4056 module_init = NULL;
4057 if (module)
4058 module_init = load_module(module, "module_init", NULL);
Pekka Paalanen33616392012-07-30 16:56:57 +03004059 if (module_init && module_init(ec) < 0) {
4060 ret = EXIT_FAILURE;
4061 goto out;
4062 }
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004063
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004064 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004065 weston_log("fatal: failed to add socket: %m\n");
4066 ret = EXIT_FAILURE;
4067 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004068 }
4069
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004070 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004071 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004072
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004073 wl_display_run(display);
4074
4075 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004076 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05004077 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004078
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004079 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004080
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004081 if (ec->has_bind_display)
Kristian Høgsberg362b6722012-06-18 15:13:51 -04004082 ec->unbind_display(ec->egl_display, display);
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004083
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004084 for (i = ARRAY_LENGTH(signals); i;)
4085 wl_event_source_remove(signals[--i]);
4086
Daniel Stone855028f2012-05-01 20:37:10 +01004087 weston_compositor_xkb_destroy(ec);
4088
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004089 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004090 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004091
Martin Minarik19e6f262012-06-07 13:08:46 +02004092 weston_log_file_close();
4093
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004094 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004095}