blob: e48a44f58d63990813d67364aa481ccbc1d0ddd6 [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,
1283 pixman_region32_t *damage)
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;
Rob Clark0e5a2d02012-08-30 16:47:18 -05001288 /* regions of surface to draw opaque/blended in surface coordinates: */
1289 pixman_region32_t surface_opaque, 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_opaque);
1295 pixman_region32_init(&surface_blend);
1296
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001297 pixman_region32_intersect(&repaint,
1298 &es->transform.boundingbox, damage);
1299 pixman_region32_subtract(&repaint, &repaint, &es->clip);
Pekka Paalanenc7b45f62012-02-06 12:16:07 +02001300
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001301 if (!pixman_region32_not_empty(&repaint))
Pekka Paalanena9f8a212012-01-26 11:28:08 +02001302 goto out;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001303
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001304 pixman_region32_subtract(&ec->primary_plane.damage,
1305 &ec->primary_plane.damage, &repaint);
1306
Kristian Høgsberg101cb652012-02-17 10:45:16 -05001307 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
Pekka Paalanen6b5585b2012-08-30 16:47:19 -05001308 if (1 || es->alpha < 1.0) {
Rob Clark0e5a2d02012-08-30 16:47:18 -05001309 /* blended region is whole surface minus opaque region: */
1310 pixman_region32_init_rect(&surface_blend, 0, 0,
1311 es->geometry.width, es->geometry.height);
1312 pixman_region32_init(&surface_opaque);
1313 pixman_region32_copy(&surface_opaque, &es->opaque);
1314 pixman_region32_subtract(&surface_blend, &surface_blend,
1315 &surface_opaque);
1316 } else {
1317 /* whole surface is opaque: */
1318 pixman_region32_init_rect(&surface_opaque, 0, 0,
1319 es->geometry.width, es->geometry.height);
1320 pixman_region32_init(&surface_blend);
1321 }
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001322
Kristian Høgsberg07632622012-01-25 23:02:06 -05001323 if (ec->current_shader != es->shader) {
1324 glUseProgram(es->shader->program);
Kristian Høgsberg07632622012-01-25 23:02:06 -05001325 ec->current_shader = es->shader;
1326 }
1327
Kristian Høgsberg0452abc2012-01-31 15:38:36 -05001328 glUniformMatrix4fv(es->shader->proj_uniform,
1329 1, GL_FALSE, output->matrix.d);
Pekka Paalanenf55f5442012-02-02 16:49:05 +02001330 glUniform4fv(es->shader->color_uniform, 1, es->color);
Kristian Høgsberga416fa12012-05-21 14:06:52 -04001331 glUniform1f(es->shader->alpha_uniform, es->alpha);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001332
Scott Moreauccbf29d2012-02-22 14:21:41 -07001333 if (es->transform.enabled || output->zoom.active)
Kristian Høgsberg40caded2011-06-20 19:48:16 -04001334 filter = GL_LINEAR;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001335 else
Pekka Paalanenc61eca62012-01-06 14:10:06 +02001336 filter = GL_NEAREST;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001337
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02001338 for (i = 0; i < es->num_textures; i++) {
1339 glUniform1i(es->shader->tex_uniforms[i], i);
1340 glActiveTexture(GL_TEXTURE0 + i);
Rob Clarke3b95912012-08-31 16:42:18 -05001341 glBindTexture(es->target, es->textures[i]);
1342 glTexParameteri(es->target, GL_TEXTURE_MIN_FILTER, filter);
1343 glTexParameteri(es->target, GL_TEXTURE_MAG_FILTER, filter);
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02001344 }
Kristian Høgsberg40caded2011-06-20 19:48:16 -04001345
Rob Clark0e5a2d02012-08-30 16:47:18 -05001346 if (pixman_region32_not_empty(&surface_opaque)) {
1347 glDisable(GL_BLEND);
1348 repaint_region(es, &repaint, &surface_opaque);
1349 }
Pekka Paalanen3df327f2012-01-24 15:53:35 +02001350
Rob Clark0e5a2d02012-08-30 16:47:18 -05001351 if (pixman_region32_not_empty(&surface_blend)) {
1352 glEnable(GL_BLEND);
1353 repaint_region(es, &repaint, &surface_blend);
1354 }
Pekka Paalanena9f8a212012-01-26 11:28:08 +02001355
1356out:
Kristian Høgsberg525e4c02011-02-14 10:39:54 -05001357 pixman_region32_fini(&repaint);
Rob Clark0e5a2d02012-08-30 16:47:18 -05001358 pixman_region32_fini(&surface_opaque);
1359 pixman_region32_fini(&surface_blend);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001360}
1361
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001362WL_EXPORT void
1363weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001364{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001365 wl_list_remove(&surface->layer_link);
1366 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001367 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001368 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001369}
1370
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001371WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001372weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001373{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001374 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001375
1376 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001377 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001378}
1379
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001380WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001381weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +02001382{
Kristian Høgsberg24596942011-10-24 17:51:02 -04001383 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +02001384 return;
Benjamin Franzke06286262011-05-06 19:12:33 +02001385
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001386 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -05001387 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +02001388}
1389
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001390WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001391weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001392{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001393 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001394
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001395 pixman_region32_union(&compositor->primary_plane.damage,
1396 &compositor->primary_plane.damage,
1397 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001398 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001399}
1400
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001401static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001402fade_frame(struct weston_animation *animation,
1403 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001404{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001405 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001406 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001407 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001408 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001409
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001410 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -06001411 compositor->fade.spring.timestamp = msecs;
1412
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001413 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001414 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001415 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
1416 compositor->fade.spring.current);
1417 weston_surface_damage(surface);
1418
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001419 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001420 compositor->fade.spring.current =
1421 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001422 wl_list_remove(&animation->link);
1423 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001424
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001425 if (compositor->fade.spring.current < 0.001) {
1426 destroy_surface(&surface->surface.resource);
1427 compositor->fade.surface = NULL;
1428 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001429 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001430 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001431 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001432 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001433}
1434
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001435static void
1436update_shm_texture(struct weston_surface *surface)
1437{
1438#ifdef GL_UNPACK_ROW_LENGTH
1439 pixman_box32_t *rectangles;
1440 void *data;
1441 int i, n;
1442#endif
1443
Gwenole Beauchesne023f8552012-04-20 11:07:06 +02001444 glBindTexture(GL_TEXTURE_2D, surface->textures[0]);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001445
1446 if (!surface->compositor->has_unpack_subimage) {
1447 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1448 surface->pitch, surface->buffer->height, 0,
1449 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1450 wl_shm_buffer_get_data(surface->buffer));
1451
1452 return;
1453 }
1454
1455#ifdef GL_UNPACK_ROW_LENGTH
1456 /* Mesa does not define GL_EXT_unpack_subimage */
1457 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch);
1458 data = wl_shm_buffer_get_data(surface->buffer);
1459 rectangles = pixman_region32_rectangles(&surface->damage, &n);
1460 for (i = 0; i < n; i++) {
1461 glPixelStorei(GL_UNPACK_SKIP_PIXELS, rectangles[i].x1);
1462 glPixelStorei(GL_UNPACK_SKIP_ROWS, rectangles[i].y1);
1463 glTexSubImage2D(GL_TEXTURE_2D, 0,
1464 rectangles[i].x1, rectangles[i].y1,
Rob Bradford8f241562012-07-02 17:33:40 +01001465 rectangles[i].x2 - rectangles[i].x1,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001466 rectangles[i].y2 - rectangles[i].y1,
1467 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
1468 }
1469#endif
1470}
1471
1472static void
1473surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001474 pixman_region32_t *opaque)
1475{
1476 if (surface->buffer && wl_buffer_is_shm(surface->buffer))
1477 update_shm_texture(surface);
1478
1479 if (surface->transform.enabled) {
1480 pixman_box32_t *extents;
1481
1482 extents = pixman_region32_extents(&surface->damage);
1483 surface_compute_bbox(surface, extents->x1, extents->y1,
1484 extents->x2 - extents->x1,
1485 extents->y2 - extents->y1,
1486 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001487 pixman_region32_translate(&surface->damage,
1488 -surface->plane->x,
1489 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001490 } else {
1491 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001492 surface->geometry.x - surface->plane->x,
1493 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001494 }
1495
1496 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001497 pixman_region32_union(&surface->plane->damage,
1498 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001499 empty_region(&surface->damage);
1500 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001501 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001502}
1503
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001504static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001505weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001506{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001507 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001508 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001509 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001510 struct weston_animation *animation, *next;
1511 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001512 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001513 pixman_region32_t opaque, output_damage, new_damage;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001514 int32_t width, height;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001515
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001516 weston_compositor_update_drag_surfaces(ec);
1517
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001518 width = output->current->width +
1519 output->border.left + output->border.right;
1520 height = output->current->height +
1521 output->border.top + output->border.bottom;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001522
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001523 glViewport(0, 0, width, height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -05001524
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001525 /* Rebuild the surface list and update surface transforms up front. */
1526 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +02001527 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001528 wl_list_for_each(layer, &ec->layer_list, link) {
1529 wl_list_for_each(es, &layer->surface_list, layer_link) {
1530 weston_surface_update_transform(es);
1531 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +02001532 if (es->output == output) {
1533 wl_list_insert_list(&frame_callback_list,
1534 &es->frame_callback_list);
1535 wl_list_init(&es->frame_callback_list);
1536 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001537 }
1538 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001539
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001540 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001541 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001542 else
1543 wl_list_for_each(es, &ec->surface_list, link)
1544 weston_surface_move_to_plane(es, &ec->primary_plane);
1545
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001546 pixman_region32_init(&opaque);
1547
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001548 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001549 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001550
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -04001551 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001552
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001553 pixman_region32_init(&output_damage);
1554
1555 pixman_region32_init(&new_damage);
1556 pixman_region32_copy(&new_damage, &ec->primary_plane.damage);
1557
1558 pixman_region32_union(&ec->primary_plane.damage,
1559 &ec->primary_plane.damage,
1560 &output->previous_damage);
1561
1562 pixman_region32_intersect(&output->previous_damage,
1563 &new_damage, &output->region);
1564
1565 pixman_region32_intersect(&output_damage,
1566 &ec->primary_plane.damage, &output->region);
1567
1568 pixman_region32_fini(&new_damage);
1569
Scott Moreauccbf29d2012-02-22 14:21:41 -07001570 if (output->dirty)
1571 weston_output_update_matrix(output);
1572
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001573 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001574
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001575 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001576
Kristian Høgsbergef044142011-06-21 15:02:12 -04001577 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001578
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001579 weston_compositor_repick(ec);
1580 wl_event_loop_dispatch(ec->input_loop, 0);
1581
Jonas Ådahldb773762012-06-13 00:01:21 +02001582 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001583 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001584 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001585 }
Jonas Ådahldb773762012-06-13 00:01:21 +02001586 wl_list_init(&frame_callback_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001587
Scott Moreaud64cf212012-06-08 19:40:54 -06001588 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001589 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001590 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001591 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001592}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001593
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001594static int
1595weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001596{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001597 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001598
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001599 wl_event_loop_dispatch(compositor->input_loop, 0);
1600
1601 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001602}
1603
1604WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001605weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001606{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001607 struct weston_compositor *compositor = output->compositor;
1608 struct wl_event_loop *loop =
1609 wl_display_get_event_loop(compositor->wl_display);
1610 int fd;
1611
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001612 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001613 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001614 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001615 return;
1616 }
1617
1618 output->repaint_scheduled = 0;
1619 if (compositor->input_loop_source)
1620 return;
1621
1622 fd = wl_event_loop_get_fd(compositor->input_loop);
1623 compositor->input_loop_source =
1624 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1625 weston_compositor_read_input, compositor);
1626}
1627
1628static void
1629idle_repaint(void *data)
1630{
1631 struct weston_output *output = data;
1632
1633 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001634}
1635
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001636WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001637weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1638{
1639 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001640 if (below != NULL)
1641 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001642}
1643
1644WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001645weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001646{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001647 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001648 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001649
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001650 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001651 return;
1652
Kristian Høgsbergef044142011-06-21 15:02:12 -04001653 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001654 output->repaint_needed = 1;
1655 if (output->repaint_scheduled)
1656 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001657
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001658 wl_event_loop_add_idle(loop, idle_repaint, output);
1659 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001660
1661 if (compositor->input_loop_source) {
1662 wl_event_source_remove(compositor->input_loop_source);
1663 compositor->input_loop_source = NULL;
1664 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001665}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001666
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001667WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001668weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1669{
1670 struct weston_output *output;
1671
1672 wl_list_for_each(output, &compositor->output_list, link)
1673 weston_output_schedule_repaint(output);
1674}
1675
1676WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001677weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001678{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001679 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001680 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001681
Scott Moreau9d1b1122012-06-08 19:40:53 -06001682 output = container_of(compositor->output_list.next,
1683 struct weston_output, link);
1684
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001685 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001686 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001687 return;
1688
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001689 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001690 surface = weston_surface_create(compositor);
1691 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001692 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001693 wl_list_insert(&compositor->fade_layer.surface_list,
1694 &surface->layer_link);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001695 weston_surface_assign_output(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001696 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001697 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001698 }
1699
1700 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001701 if (wl_list_empty(&compositor->fade.animation.link)) {
1702 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001703 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001704 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001705 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001706}
1707
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001708static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001709surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001710{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001711 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001712}
1713
Casey Dahlin96d8a752012-04-19 22:50:07 -04001714static struct wl_resource *
1715find_resource_for_client(struct wl_list *list, struct wl_client *client)
1716{
1717 struct wl_resource *r;
1718
1719 wl_list_for_each(r, list, link) {
1720 if (r->client == client)
1721 return r;
1722 }
1723
1724 return NULL;
1725}
1726
Casey Dahlin9074db52012-04-19 22:50:09 -04001727static void
1728weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1729{
1730 uint32_t different = es->output_mask ^ mask;
1731 uint32_t entered = mask & different;
1732 uint32_t left = es->output_mask & different;
1733 struct weston_output *output;
Rob Bradford8667e772012-05-18 14:13:03 +01001734 struct wl_resource *resource = NULL;
Casey Dahlin9074db52012-04-19 22:50:09 -04001735 struct wl_client *client = es->surface.resource.client;
1736
Scott Moreaua5021522012-08-03 17:11:51 -06001737 es->output_mask = mask;
Casey Dahlin9074db52012-04-19 22:50:09 -04001738 if (es->surface.resource.client == NULL)
1739 return;
1740 if (different == 0)
1741 return;
1742
Casey Dahlin9074db52012-04-19 22:50:09 -04001743 wl_list_for_each(output, &es->compositor->output_list, link) {
1744 if (1 << output->id & different)
1745 resource =
1746 find_resource_for_client(&output->resource_list,
1747 client);
Kristian Høgsbergc7814d22012-07-12 12:34:43 -04001748 if (resource == NULL)
1749 continue;
Casey Dahlin9074db52012-04-19 22:50:09 -04001750 if (1 << output->id & entered)
1751 wl_surface_send_enter(&es->surface.resource, resource);
1752 if (1 << output->id & left)
1753 wl_surface_send_leave(&es->surface.resource, resource);
1754 }
1755}
1756
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001757WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001758weston_surface_assign_output(struct weston_surface *es)
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001759{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001760 struct weston_compositor *ec = es->compositor;
1761 struct weston_output *output, *new_output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001762 pixman_region32_t region;
Casey Dahlin9074db52012-04-19 22:50:09 -04001763 uint32_t max, area, mask;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001764 pixman_box32_t *e;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001765
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001766 new_output = NULL;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001767 max = 0;
Casey Dahlin9074db52012-04-19 22:50:09 -04001768 mask = 0;
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001769 pixman_region32_init(&region);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001770 wl_list_for_each(output, &ec->output_list, link) {
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001771 pixman_region32_intersect(&region, &es->transform.boundingbox,
1772 &output->region);
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001773
1774 e = pixman_region32_extents(&region);
1775 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1776
Casey Dahlin9074db52012-04-19 22:50:09 -04001777 if (area > 0)
1778 mask |= 1 << output->id;
1779
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001780 if (area >= max) {
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001781 new_output = output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001782 max = area;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001783 }
1784 }
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001785 pixman_region32_fini(&region);
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001786
1787 es->output = new_output;
Casey Dahlin9074db52012-04-19 22:50:09 -04001788 weston_surface_update_output_mask(es, mask);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001789}
1790
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001791static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001792surface_attach(struct wl_client *client,
1793 struct wl_resource *resource,
1794 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1795{
1796 struct weston_surface *es = resource->data;
1797 struct wl_buffer *buffer = NULL;
1798
1799 if (buffer_resource)
1800 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001801
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -04001802 weston_surface_attach(&es->surface, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001803
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001804 if (buffer && es->configure)
1805 es->configure(es, sx, sy);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001806}
1807
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001808static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001809surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001810 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001811 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001812{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001813 struct weston_surface *es = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001814
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001815 pixman_region32_union_rect(&es->damage, &es->damage,
1816 x, y, width, height);
Kristian Høgsberg98238702012-08-03 16:29:12 -04001817 weston_surface_schedule_repaint(es);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001818}
1819
Kristian Høgsberg33418202011-08-16 23:01:28 -04001820static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001821destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001822{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001823 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001824
1825 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001826 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001827}
1828
1829static void
1830surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001831 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001832{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001833 struct weston_frame_callback *cb;
1834 struct weston_surface *es = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001835
1836 cb = malloc(sizeof *cb);
1837 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001838 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001839 return;
1840 }
1841
1842 cb->resource.object.interface = &wl_callback_interface;
1843 cb->resource.object.id = callback;
1844 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001845 cb->resource.client = client;
1846 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001847
1848 wl_client_add_resource(client, &cb->resource);
Jonas Ådahldb773762012-06-13 00:01:21 +02001849 wl_list_insert(es->frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001850}
1851
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001852static void
1853surface_set_opaque_region(struct wl_client *client,
1854 struct wl_resource *resource,
1855 struct wl_resource *region_resource)
1856{
1857 struct weston_surface *surface = resource->data;
1858 struct weston_region *region;
1859
1860 pixman_region32_fini(&surface->opaque);
1861
1862 if (region_resource) {
1863 region = region_resource->data;
1864 pixman_region32_init_rect(&surface->opaque, 0, 0,
1865 surface->geometry.width,
1866 surface->geometry.height);
1867 pixman_region32_intersect(&surface->opaque,
1868 &surface->opaque, &region->region);
1869 } else {
1870 pixman_region32_init(&surface->opaque);
1871 }
1872
1873 surface->geometry.dirty = 1;
1874}
1875
1876static void
1877surface_set_input_region(struct wl_client *client,
1878 struct wl_resource *resource,
1879 struct wl_resource *region_resource)
1880{
1881 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001882 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001883
1884 if (region_resource) {
1885 region = region_resource->data;
1886 pixman_region32_init_rect(&surface->input, 0, 0,
1887 surface->geometry.width,
1888 surface->geometry.height);
1889 pixman_region32_intersect(&surface->input,
1890 &surface->input, &region->region);
1891 } else {
1892 pixman_region32_init_rect(&surface->input, 0, 0,
1893 surface->geometry.width,
1894 surface->geometry.height);
1895 }
1896
Kristian Høgsberg98238702012-08-03 16:29:12 -04001897 weston_surface_schedule_repaint(surface);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001898}
1899
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001900static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001901 surface_destroy,
1902 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001903 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001904 surface_frame,
1905 surface_set_opaque_region,
1906 surface_set_input_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001907};
1908
1909static void
1910compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001911 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001912{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001913 struct weston_compositor *ec = resource->data;
1914 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001915
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001916 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001917 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001918 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001919 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001920 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001921
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001922 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001923
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001924 surface->surface.resource.object.id = id;
1925 surface->surface.resource.object.interface = &wl_surface_interface;
1926 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001927 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001928 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001929
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001930 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001931}
1932
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001933static void
1934destroy_region(struct wl_resource *resource)
1935{
1936 struct weston_region *region =
1937 container_of(resource, struct weston_region, resource);
1938
1939 pixman_region32_fini(&region->region);
1940 free(region);
1941}
1942
1943static void
1944region_destroy(struct wl_client *client, struct wl_resource *resource)
1945{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001946 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001947}
1948
1949static void
1950region_add(struct wl_client *client, struct wl_resource *resource,
1951 int32_t x, int32_t y, int32_t width, int32_t height)
1952{
1953 struct weston_region *region = resource->data;
1954
1955 pixman_region32_union_rect(&region->region, &region->region,
1956 x, y, width, height);
1957}
1958
1959static void
1960region_subtract(struct wl_client *client, struct wl_resource *resource,
1961 int32_t x, int32_t y, int32_t width, int32_t height)
1962{
1963 struct weston_region *region = resource->data;
1964 pixman_region32_t rect;
1965
1966 pixman_region32_init_rect(&rect, x, y, width, height);
1967 pixman_region32_subtract(&region->region, &region->region, &rect);
1968 pixman_region32_fini(&rect);
1969}
1970
1971static const struct wl_region_interface region_interface = {
1972 region_destroy,
1973 region_add,
1974 region_subtract
1975};
1976
1977static void
1978compositor_create_region(struct wl_client *client,
1979 struct wl_resource *resource, uint32_t id)
1980{
1981 struct weston_region *region;
1982
1983 region = malloc(sizeof *region);
1984 if (region == NULL) {
1985 wl_resource_post_no_memory(resource);
1986 return;
1987 }
1988
1989 region->resource.destroy = destroy_region;
1990
1991 region->resource.object.id = id;
1992 region->resource.object.interface = &wl_region_interface;
1993 region->resource.object.implementation =
1994 (void (**)(void)) &region_interface;
1995 region->resource.data = region;
1996
1997 pixman_region32_init(&region->region);
1998
1999 wl_client_add_resource(client, &region->resource);
2000}
2001
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002002static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002003 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002004 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002005};
2006
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002007WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002008weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002009{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002010 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2011 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002012
2013 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02002014 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002015}
2016
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002017static void
2018weston_compositor_dpms_on(struct weston_compositor *compositor)
2019{
2020 struct weston_output *output;
2021
2022 wl_list_for_each(output, &compositor->output_list, link)
2023 if (output->set_dpms)
2024 output->set_dpms(output, WESTON_DPMS_ON);
2025}
2026
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002027WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002028weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002029{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002030 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
2031 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002032 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002033 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002034 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002035 }
2036}
2037
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002038static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002039weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002040{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002041 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002042 compositor->idle_inhibit++;
2043}
2044
2045static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002046weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002047{
2048 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002049 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002050}
2051
2052static int
2053idle_handler(void *data)
2054{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002055 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002056
2057 if (compositor->idle_inhibit)
2058 return 1;
2059
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002060 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002061
2062 return 1;
2063}
2064
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002065WL_EXPORT void
2066weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2067{
2068 pixman_region32_init(&plane->damage);
2069 plane->x = x;
2070 plane->y = y;
2071}
2072
2073WL_EXPORT void
2074weston_plane_release(struct weston_plane *plane)
2075{
2076 pixman_region32_fini(&plane->damage);
2077}
2078
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002079static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002080weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002081
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002082static void
Daniel Stone37816df2012-05-16 18:45:18 +01002083clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002084{
Daniel Stone37816df2012-05-16 18:45:18 +01002085 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002086 struct weston_output *output, *prev = NULL;
2087 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002088
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002089 x = wl_fixed_to_int(*fx);
2090 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01002091 old_x = wl_fixed_to_int(seat->seat.pointer->x);
2092 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002093
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002094 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002095 if (pixman_region32_contains_point(&output->region,
2096 x, y, NULL))
2097 valid = 1;
2098 if (pixman_region32_contains_point(&output->region,
2099 old_x, old_y, NULL))
2100 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002101 }
Daniel Stone37816df2012-05-16 18:45:18 +01002102
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002103 if (!valid) {
2104 if (x < prev->x)
2105 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06002106 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002107 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06002108 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002109 if (y < prev->y)
2110 *fy = wl_fixed_from_int(prev->y);
2111 else if (y >= prev->y + prev->current->height)
2112 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06002113 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002114 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002115}
2116
2117WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002118notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002119{
2120 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002121 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002122 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002123 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002124 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002125
2126 weston_compositor_activity(ec);
2127
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002128 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002129
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002130 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002131
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002132 pointer->x = x;
2133 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002134
2135 ix = wl_fixed_to_int(x);
2136 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002137
Scott Moreauccbf29d2012-02-22 14:21:41 -07002138 wl_list_for_each(output, &ec->output_list, link)
2139 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002140 pixman_region32_contains_point(&output->region,
2141 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06002142 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002143
Daniel Stone37816df2012-05-16 18:45:18 +01002144 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002145 interface = pointer->grab->interface;
2146 interface->motion(pointer->grab, time,
2147 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002148
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002149 if (seat->sprite) {
2150 weston_surface_set_position(seat->sprite,
2151 ix - seat->hotspot_x,
2152 iy - seat->hotspot_y);
2153 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002154 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002155}
2156
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002157WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002158weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002159 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05002160{
Daniel Stone37816df2012-05-16 18:45:18 +01002161 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002162
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03002163 if (seat->seat.keyboard) {
2164 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
2165 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04002166 }
2167
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002168 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05002169}
2170
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002171WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002172notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002173 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05002174{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002175 struct weston_compositor *compositor = seat->compositor;
2176 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01002177 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002178 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002179 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05002180
Daniel Stone4dbadb12012-05-30 16:31:51 +01002181 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002182 if (compositor->ping_handler && focus)
2183 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002184 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002185 if (pointer->button_count == 0) {
2186 pointer->grab_button = button;
2187 pointer->grab_time = time;
2188 pointer->grab_x = pointer->x;
2189 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002190 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002191 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002192 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002193 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002194 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002195 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002196
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002197 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002198 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05002199
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002200 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05002201
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002202 if (pointer->button_count == 1)
2203 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002204 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05002205}
2206
Scott Moreau210d0792012-03-22 10:47:01 -06002207WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002208notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01002209 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002210{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002211 struct weston_compositor *compositor = seat->compositor;
2212 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01002213 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002214 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002215 uint32_t serial = wl_display_next_serial(compositor->wl_display);
2216
2217 if (compositor->ping_handler && focus)
2218 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06002219
2220 weston_compositor_activity(compositor);
2221
Scott Moreau6a3633d2012-03-20 08:47:59 -06002222 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002223 weston_compositor_run_axis_binding(compositor, seat,
2224 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06002225 else
2226 return;
2227
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002228 if (pointer->focus_resource)
2229 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01002230 value);
Scott Moreau210d0792012-03-22 10:47:01 -06002231}
2232
Daniel Stone05d58682012-06-22 13:21:38 +01002233WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002234notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002235{
Daniel Stone05d58682012-06-22 13:21:38 +01002236 struct wl_keyboard *keyboard = &seat->keyboard;
2237 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01002238 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002239 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01002240 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01002241 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01002242
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002243 /* Serialize and update our internal state, checking to see if it's
2244 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01002245 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
2246 XKB_STATE_DEPRESSED);
2247 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
2248 XKB_STATE_LATCHED);
2249 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
2250 XKB_STATE_LOCKED);
2251 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01002252 XKB_STATE_EFFECTIVE);
2253
Daniel Stone71c38772012-06-22 13:21:30 +01002254 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
2255 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
2256 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
2257 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01002258 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01002259
Daniel Stone71c38772012-06-22 13:21:30 +01002260 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
2261 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
2262 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
2263 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05002264
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002265 /* And update the modifier_state for bindings. */
2266 mods_lookup = mods_depressed | mods_latched;
2267 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01002268 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002269 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01002270 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002271 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01002272 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002273 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04002274 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
2275 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01002276
Daniel Stone8c8164f2012-05-30 16:31:45 +01002277 /* Finally, notify the compositor that LEDs have changed. */
2278 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01002279 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01002280 leds |= LED_NUM_LOCK;
2281 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01002282 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01002283 leds |= LED_CAPS_LOCK;
2284 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01002285 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01002286 leds |= LED_SCROLL_LOCK;
2287 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01002288 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01002289 seat->xkb_state.leds = leds;
2290
Daniel Stone05d58682012-06-22 13:21:38 +01002291 if (changed) {
2292 grab->interface->modifiers(grab,
2293 serial,
2294 keyboard->modifiers.mods_depressed,
2295 keyboard->modifiers.mods_latched,
2296 keyboard->modifiers.mods_locked,
2297 keyboard->modifiers.group);
2298 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002299}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04002300
Daniel Stone05d58682012-06-22 13:21:38 +01002301static void
2302update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002303 enum wl_keyboard_key_state state)
2304{
2305 enum xkb_key_direction direction;
2306
2307 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2308 direction = XKB_KEY_DOWN;
2309 else
2310 direction = XKB_KEY_UP;
2311
2312 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
2313 * broken keycode system, which starts at 8. */
2314 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
2315
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002316 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002317}
2318
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002319WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002320notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01002321 enum wl_keyboard_key_state state,
2322 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002323{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002324 struct weston_compositor *compositor = seat->compositor;
2325 struct wl_keyboard *keyboard = seat->seat.keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01002326 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002327 (struct weston_surface *) keyboard->focus;
2328 struct wl_keyboard_grab *grab = keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002329 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002330 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002331
Daniel Stonec9785ea2012-05-30 16:31:52 +01002332 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002333 if (compositor->ping_handler && focus)
2334 compositor->ping_handler(focus, serial);
2335
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002336 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002337 keyboard->grab_key = key;
2338 keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07002339 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002340 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07002341 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002342
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002343 end = keyboard->keys.data + keyboard->keys.size;
2344 for (k = keyboard->keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01002345 if (*k == key) {
2346 /* Ignore server-generated repeats. */
2347 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2348 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002349 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01002350 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002351 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002352 keyboard->keys.size = (void *) end - keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002353 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002354 k = wl_array_add(&keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002355 *k = key;
2356 }
Ray Strodee96dcb82008-12-20 02:00:49 -05002357
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002358 if (grab == &keyboard->default_grab) {
2359 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002360 state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002361 grab = keyboard->grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06002362 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05002363
Daniel Stone7ace3902012-05-30 16:31:40 +01002364 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01002365
2366 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002367 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01002368 wl_display_get_serial(compositor->wl_display),
2369 key,
2370 state);
2371 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002372}
2373
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002374WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002375notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01002376 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002377{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002378 struct weston_compositor *compositor = seat->compositor;
2379 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002380
2381 if (output) {
Daniel Stone37816df2012-05-16 18:45:18 +01002382 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002383 x - pointer->x,
2384 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002385
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002386 pointer->x = x;
2387 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002388 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002389 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002390 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002391 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03002392 /* FIXME: We should call wl_pointer_set_focus(seat,
2393 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002394 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002395}
2396
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002397static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002398destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002399{
Daniel Stone37816df2012-05-16 18:45:18 +01002400 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002401
Daniel Stone37816df2012-05-16 18:45:18 +01002402 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002403 saved_kbd_focus_listener);
2404
Daniel Stone37816df2012-05-16 18:45:18 +01002405 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002406}
2407
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002408WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002409notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002410 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002411{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002412 struct weston_compositor *compositor = seat->compositor;
2413 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04002414 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002415 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05002416
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002417 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002418 wl_array_copy(&keyboard->keys, keys);
2419 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002420 weston_compositor_idle_inhibit(compositor);
2421 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002422 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002423 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002424 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002425
Daniel Stoneef172672012-06-22 13:21:32 +01002426 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002427 wl_array_for_each(k, &keyboard->keys) {
2428 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01002429 WL_KEYBOARD_KEY_STATE_PRESSED);
2430 }
2431
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002432 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002433
2434 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002435 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2436 wl_keyboard_set_focus(keyboard, surface);
2437 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002438 }
2439}
2440
2441WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002442notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01002443{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002444 struct weston_compositor *compositor = seat->compositor;
2445 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002446 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002447
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002448 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002449 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002450 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002451 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002452 WL_KEYBOARD_KEY_STATE_RELEASED);
2453 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002454
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002455 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002456
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002457 if (keyboard->focus) {
2458 seat->saved_kbd_focus = keyboard->focus;
2459 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01002460 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002461 wl_signal_add(&keyboard->focus->resource.destroy_signal,
2462 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002463 }
2464
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002465 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002466 /* FIXME: We really need keyboard grab cancel here to
2467 * let the grab shut down properly. As it is we leak
2468 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002469 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002470}
2471
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002472static void
Daniel Stone37816df2012-05-16 18:45:18 +01002473touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002474{
Daniel Stone37816df2012-05-16 18:45:18 +01002475 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002476 struct wl_resource *resource;
2477
Pekka Paalanen56464252012-07-31 13:21:08 +03002478 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002479 return;
2480
Pekka Paalanen56464252012-07-31 13:21:08 +03002481 if (seat->touch->focus_resource)
2482 wl_list_remove(&seat->touch->focus_listener.link);
2483 seat->touch->focus = NULL;
2484 seat->touch->focus_resource = NULL;
2485
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002486 if (surface) {
2487 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01002488 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04002489 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002490 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02002491 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002492 return;
2493 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002494
Daniel Stone37816df2012-05-16 18:45:18 +01002495 seat->touch->focus = surface;
2496 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03002497 wl_signal_add(&resource->destroy_signal,
2498 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002499 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002500}
2501
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002502/**
2503 * notify_touch - emulates button touches and notifies surfaces accordingly.
2504 *
2505 * It assumes always the correct cycle sequence until it gets here: touch_down
2506 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2507 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002508 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002509 */
2510WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002511notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002512 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002513{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002514 struct weston_compositor *ec = seat->compositor;
2515 struct wl_touch *touch = seat->seat.touch;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002516 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002517 wl_fixed_t sx, sy;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002518 uint32_t serial = 0;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002519
2520 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01002521 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002522 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002523
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002524 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002525
2526 /* the first finger down picks the surface, and all further go
2527 * to that surface for the remainder of the touch session i.e.
2528 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002529 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002530 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002531 touch_set_focus(seat, &es->surface);
2532 } else if (touch->focus) {
2533 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002534 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002535 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002536
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002537 if (touch->focus_resource && touch->focus)
2538 wl_touch_send_down(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002539 serial, time,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002540 &touch->focus->resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002541 touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002542 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002543 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002544 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002545 if (!es)
2546 break;
2547
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002548 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002549 if (touch->focus_resource)
2550 wl_touch_send_motion(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002551 time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002552 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002553 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002554 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002555 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002556
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002557 if (touch->focus_resource)
2558 wl_touch_send_up(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002559 serial, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002560 if (seat->num_tp == 0)
2561 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002562 break;
2563 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002564}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002565
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04002566static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002567pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
2568{
2569 struct weston_seat *seat = container_of(listener, struct weston_seat,
2570 sprite_destroy_listener);
2571
2572 seat->sprite = NULL;
2573}
2574
2575static void
2576pointer_cursor_surface_configure(struct weston_surface *es,
2577 int32_t dx, int32_t dy)
2578{
2579 struct weston_seat *seat = es->private;
2580 int x, y;
2581
2582 assert(es == seat->sprite);
2583
2584 seat->hotspot_x -= dx;
2585 seat->hotspot_y -= dy;
2586
2587 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
2588 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
2589
2590 weston_surface_configure(seat->sprite, x, y,
2591 es->buffer->width, es->buffer->height);
2592
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03002593 empty_region(&es->input);
2594
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002595 if (!weston_surface_is_mapped(es)) {
2596 wl_list_insert(&es->compositor->cursor_layer.surface_list,
2597 &es->layer_link);
2598 weston_surface_assign_output(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002599 }
2600}
2601
2602static void
2603pointer_unmap_sprite(struct weston_seat *seat)
2604{
2605 if (weston_surface_is_mapped(seat->sprite))
2606 weston_surface_unmap(seat->sprite);
2607
2608 wl_list_remove(&seat->sprite_destroy_listener.link);
2609 seat->sprite->configure = NULL;
2610 seat->sprite->private = NULL;
2611 seat->sprite = NULL;
2612}
2613
2614static void
2615pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2616 uint32_t serial, struct wl_resource *surface_resource,
2617 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002618{
Daniel Stone37816df2012-05-16 18:45:18 +01002619 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002620 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002621
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002622 if (surface_resource)
2623 surface = container_of(surface_resource->data,
2624 struct weston_surface, surface);
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002625
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04002626 if (seat->seat.pointer->focus == NULL)
2627 return;
2628 if (seat->seat.pointer->focus->resource.client != client)
2629 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002630 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002631 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002632
2633 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002634 if (surface->configure) {
2635 wl_resource_post_error(&surface->surface.resource,
2636 WL_DISPLAY_ERROR_INVALID_OBJECT,
2637 "surface->configure already "
2638 "set");
2639 return;
2640 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002641 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002642
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002643 if (seat->sprite)
2644 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002645
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002646 if (!surface)
2647 return;
2648
2649 wl_signal_add(&surface->surface.resource.destroy_signal,
2650 &seat->sprite_destroy_listener);
2651
2652 surface->configure = pointer_cursor_surface_configure;
2653 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002654 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002655 seat->hotspot_x = x;
2656 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002657
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04002658 if (surface->buffer)
2659 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002660}
2661
Daniel Stone37816df2012-05-16 18:45:18 +01002662static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002663 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002664};
2665
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002666static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002667handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002668{
Daniel Stone37816df2012-05-16 18:45:18 +01002669 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002670
Daniel Stone37816df2012-05-16 18:45:18 +01002671 seat = container_of(listener, struct weston_seat,
2672 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002673
Daniel Stone37816df2012-05-16 18:45:18 +01002674 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002675}
2676
Casey Dahlin9074db52012-04-19 22:50:09 -04002677static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002678{
2679 wl_list_remove(&resource->link);
2680 free(resource);
2681}
2682
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002683static void
Daniel Stone37816df2012-05-16 18:45:18 +01002684seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2685 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002686{
Daniel Stone37816df2012-05-16 18:45:18 +01002687 struct weston_seat *seat = resource->data;
2688 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002689
Daniel Stone37816df2012-05-16 18:45:18 +01002690 if (!seat->seat.pointer)
2691 return;
2692
2693 cr = wl_client_add_object(client, &wl_pointer_interface,
2694 &pointer_interface, id, seat);
2695 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002696 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002697
2698 if (seat->seat.pointer->focus &&
2699 seat->seat.pointer->focus->resource.client == client) {
2700 struct weston_surface *surface;
2701 wl_fixed_t sx, sy;
2702
2703 surface = (struct weston_surface *) seat->seat.pointer->focus;
2704 weston_surface_from_global_fixed(surface,
2705 seat->seat.pointer->x,
2706 seat->seat.pointer->y,
2707 &sx,
2708 &sy);
2709 wl_pointer_set_focus(seat->seat.pointer,
2710 seat->seat.pointer->focus,
2711 sx,
2712 sy);
2713 }
Daniel Stone37816df2012-05-16 18:45:18 +01002714}
2715
2716static void
2717seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2718 uint32_t id)
2719{
2720 struct weston_seat *seat = resource->data;
2721 struct wl_resource *cr;
2722
2723 if (!seat->seat.keyboard)
2724 return;
2725
2726 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2727 seat);
2728 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002729 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002730
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002731 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2732 seat->xkb_info.keymap_fd,
2733 seat->xkb_info.keymap_size);
2734
Daniel Stone17b13bd2012-05-30 16:31:39 +01002735 if (seat->seat.keyboard->focus &&
2736 seat->seat.keyboard->focus->resource.client == client) {
2737 wl_keyboard_set_focus(seat->seat.keyboard,
2738 seat->seat.keyboard->focus);
2739 }
Daniel Stone37816df2012-05-16 18:45:18 +01002740}
2741
2742static void
2743seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2744 uint32_t id)
2745{
2746 struct weston_seat *seat = resource->data;
2747 struct wl_resource *cr;
2748
2749 if (!seat->seat.touch)
2750 return;
2751
2752 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2753 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002754 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002755}
2756
2757static const struct wl_seat_interface seat_interface = {
2758 seat_get_pointer,
2759 seat_get_keyboard,
2760 seat_get_touch,
2761};
2762
2763static void
2764bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2765{
2766 struct wl_seat *seat = data;
2767 struct wl_resource *resource;
2768 enum wl_seat_capability caps = 0;
2769
2770 resource = wl_client_add_object(client, &wl_seat_interface,
2771 &seat_interface, id, data);
2772 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002773 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002774
2775 if (seat->pointer)
2776 caps |= WL_SEAT_CAPABILITY_POINTER;
2777 if (seat->keyboard)
2778 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2779 if (seat->touch)
2780 caps |= WL_SEAT_CAPABILITY_TOUCH;
2781
2782 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002783}
2784
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002785static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002786device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002787{
Daniel Stone37816df2012-05-16 18:45:18 +01002788 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002789
Daniel Stone37816df2012-05-16 18:45:18 +01002790 seat = container_of(listener, struct weston_seat,
2791 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002792
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002793 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002794}
2795
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002796static void weston_compositor_xkb_init(struct weston_compositor *ec,
2797 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002798{
Daniel Stonee379da92012-05-30 16:32:04 +01002799 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002800 ec->xkb_context = xkb_context_new(0);
2801 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002802 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002803 exit(1);
2804 }
Daniel Stone994679a2012-05-30 16:31:43 +01002805 }
2806
2807 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002808 ec->xkb_names = *names;
2809 if (!ec->xkb_names.rules)
2810 ec->xkb_names.rules = strdup("evdev");
2811 if (!ec->xkb_names.model)
2812 ec->xkb_names.model = strdup("pc105");
2813 if (!ec->xkb_names.layout)
2814 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002815}
2816
Daniel Stonee379da92012-05-30 16:32:04 +01002817static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2818{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002819 if (xkb_info->keymap)
2820 xkb_map_unref(xkb_info->keymap);
2821
2822 if (xkb_info->keymap_area)
2823 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2824 if (xkb_info->keymap_fd >= 0)
2825 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002826}
2827
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002828static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2829{
Daniel Stonee379da92012-05-30 16:32:04 +01002830 free((char *) ec->xkb_names.rules);
2831 free((char *) ec->xkb_names.model);
2832 free((char *) ec->xkb_names.layout);
2833 free((char *) ec->xkb_names.variant);
2834 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002835
Daniel Stonee379da92012-05-30 16:32:04 +01002836 xkb_info_destroy(&ec->xkb_info);
2837 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002838}
2839
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002840static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002841weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002842{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002843 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002844
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002845 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2846 XKB_MOD_NAME_SHIFT);
2847 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2848 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002849 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2850 XKB_MOD_NAME_CTRL);
2851 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2852 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002853 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2854 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002855 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2856 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002857 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002858
2859 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2860 XKB_LED_NAME_NUM);
2861 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2862 XKB_LED_NAME_CAPS);
2863 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2864 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002865
2866 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2867 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002868 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002869 exit(EXIT_FAILURE);
2870 }
2871 xkb_info->keymap_size = strlen(keymap_str) + 1;
2872
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002873 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002874 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002875 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002876 (unsigned long) xkb_info->keymap_size);
2877 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002878 }
2879
2880 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2881 PROT_READ | PROT_WRITE,
2882 MAP_SHARED, xkb_info->keymap_fd, 0);
2883 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002884 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002885 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002886 goto err_dev_zero;
2887 }
2888 strcpy(xkb_info->keymap_area, keymap_str);
2889 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002890
2891 return;
2892
2893err_dev_zero:
2894 close(xkb_info->keymap_fd);
2895 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002896err_keymap_str:
2897 free(keymap_str);
2898 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002899}
2900
2901static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002902weston_compositor_build_global_keymap(struct weston_compositor *ec)
2903{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002904 if (ec->xkb_info.keymap != NULL)
2905 return;
2906
Daniel Stonee379da92012-05-30 16:32:04 +01002907 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002908 &ec->xkb_names,
2909 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002910 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002911 weston_log("failed to compile global XKB keymap\n");
2912 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002913 "options %s",
2914 ec->xkb_names.rules, ec->xkb_names.model,
2915 ec->xkb_names.layout, ec->xkb_names.variant,
2916 ec->xkb_names.options);
2917 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002918 }
2919
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002920 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002921}
2922
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002923WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002924weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002925{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002926 if (seat->has_keyboard)
2927 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002928
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002929 if (keymap != NULL) {
2930 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002931 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002932 }
2933 else {
2934 weston_compositor_build_global_keymap(seat->compositor);
2935 seat->xkb_info = seat->compositor->xkb_info;
2936 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2937 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002938
2939 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2940 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002941 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01002942 exit(1);
2943 }
2944
Daniel Stone71c38772012-06-22 13:21:30 +01002945 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002946
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002947 wl_keyboard_init(&seat->keyboard);
2948 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2949
2950 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01002951}
2952
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002953WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002954weston_seat_init_pointer(struct weston_seat *seat)
2955{
2956 if (seat->has_pointer)
2957 return;
2958
2959 wl_pointer_init(&seat->pointer);
2960 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2961
2962 seat->has_pointer = 1;
2963}
2964
2965WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002966weston_seat_init_touch(struct weston_seat *seat)
2967{
2968 if (seat->has_touch)
2969 return;
2970
2971 wl_touch_init(&seat->touch);
2972 wl_seat_set_touch(&seat->seat, &seat->touch);
2973
2974 seat->has_touch = 1;
2975}
2976
2977WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002978weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002979{
Daniel Stone37816df2012-05-16 18:45:18 +01002980 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002981 seat->has_pointer = 0;
2982 seat->has_keyboard = 0;
2983 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002984
Daniel Stone37816df2012-05-16 18:45:18 +01002985 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2986 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002987
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002988 seat->sprite = NULL;
2989 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002990
Daniel Stone37816df2012-05-16 18:45:18 +01002991 seat->compositor = ec;
2992 seat->hotspot_x = 16;
2993 seat->hotspot_y = 16;
2994 seat->modifier_state = 0;
2995 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002996
Daniel Stone37816df2012-05-16 18:45:18 +01002997 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002998 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002999
Daniel Stone37816df2012-05-16 18:45:18 +01003000 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03003001
Daniel Stone37816df2012-05-16 18:45:18 +01003002 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
3003 wl_signal_add(&seat->seat.drag_icon_signal,
3004 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04003005
3006 clipboard_create(seat);
Jan Arne Petersene829adc2012-08-10 16:47:22 +02003007 input_method_create(ec, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05003008}
3009
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003010WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01003011weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003012{
Daniel Stone37816df2012-05-16 18:45:18 +01003013 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003014 /* The global object is destroyed at wl_display_destroy() time. */
3015
Daniel Stone37816df2012-05-16 18:45:18 +01003016 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003017 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003018
Daniel Stone74419a22012-05-30 16:32:02 +01003019 if (seat->xkb_state.state != NULL)
3020 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01003021 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01003022
Daniel Stone37816df2012-05-16 18:45:18 +01003023 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003024}
3025
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003026static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003027drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
3028{
3029 weston_surface_configure(es,
3030 es->geometry.x + sx, es->geometry.y + sy,
3031 es->buffer->width, es->buffer->height);
3032}
3033
3034static int
Daniel Stone37816df2012-05-16 18:45:18 +01003035device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003036 struct weston_surface *surface)
3037{
Daniel Stone37816df2012-05-16 18:45:18 +01003038 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003039
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003040 if (surface->configure) {
3041 wl_resource_post_error(&surface->surface.resource,
3042 WL_DISPLAY_ERROR_INVALID_OBJECT,
3043 "surface->configure already set");
3044 return 0;
3045 }
3046
Daniel Stone37816df2012-05-16 18:45:18 +01003047 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003048
Daniel Stone37816df2012-05-16 18:45:18 +01003049 weston_surface_set_position(ws->drag_surface,
3050 wl_fixed_to_double(seat->pointer->x),
3051 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003052
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003053 surface->configure = drag_surface_configure;
3054
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003055 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01003056 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003057
3058 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003059}
3060
3061static void
Daniel Stone37816df2012-05-16 18:45:18 +01003062device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003063{
Daniel Stone37816df2012-05-16 18:45:18 +01003064 seat->drag_surface->configure = NULL;
3065 undef_region(&seat->drag_surface->input);
3066 wl_list_remove(&seat->drag_surface_destroy_listener.link);
3067 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003068}
3069
3070static void
Daniel Stone37816df2012-05-16 18:45:18 +01003071device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003072{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003073 struct wl_list *list;
3074
Daniel Stone37816df2012-05-16 18:45:18 +01003075 if (weston_surface_is_mapped(seat->drag_surface) ||
3076 !seat->drag_surface->buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003077 return;
3078
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003079 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
3080 list = &seat->sprite->layer_link;
3081 else
3082 list = &seat->compositor->cursor_layer.surface_list;
3083
3084 wl_list_insert(list, &seat->drag_surface->layer_link);
Daniel Stone37816df2012-05-16 18:45:18 +01003085 weston_surface_assign_output(seat->drag_surface);
3086 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003087}
3088
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003089static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003090weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01003091 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003092{
3093 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003094
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003095 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003096 return;
3097
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003098 if (seat->drag_surface && seat->seat.drag_surface &&
3099 (&seat->drag_surface->surface.resource !=
3100 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003101 /* between calls to this funcion we got a new drag_surface */
3102 surface_changed = 1;
3103
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003104 if (!seat->seat.drag_surface || surface_changed) {
3105 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003106 if (!surface_changed)
3107 return;
3108 }
3109
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003110 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003111 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003112 seat->seat.drag_surface;
3113 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003114 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003115 }
3116
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003117 /* the client may not have attached a buffer to the drag surface
3118 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003119 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003120
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02003121 /* the client may have attached a buffer with a different size to
3122 * the drag surface, causing the input region to be reset */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003123 if (region_is_undefined(&seat->drag_surface->input))
3124 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02003125
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003126 if (!dx && !dy)
3127 return;
3128
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003129 weston_surface_set_position(seat->drag_surface,
3130 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
3131 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003132}
3133
3134WL_EXPORT void
3135weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
3136{
Daniel Stone4dab5db2012-05-30 16:31:53 +01003137 struct weston_seat *seat;
3138
3139 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003140 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003141}
3142
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003143static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003144bind_output(struct wl_client *client,
3145 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003146{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003147 struct weston_output *output = data;
3148 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003149 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003150
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003151 resource = wl_client_add_object(client,
3152 &wl_output_interface, NULL, id, data);
3153
Casey Dahlin9074db52012-04-19 22:50:09 -04003154 wl_list_insert(&output->resource_list, &resource->link);
3155 resource->destroy = unbind_resource;
3156
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003157 wl_output_send_geometry(resource,
3158 output->x,
3159 output->y,
3160 output->mm_width,
3161 output->mm_height,
3162 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003163 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003164 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003165
3166 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003167 wl_output_send_mode(resource,
3168 mode->flags,
3169 mode->width,
3170 mode->height,
3171 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003172 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003173}
3174
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003175static const char vertex_shader[] =
3176 "uniform mat4 proj;\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05003177 "attribute vec2 position;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003178 "attribute vec2 texcoord;\n"
3179 "varying vec2 v_texcoord;\n"
3180 "void main()\n"
3181 "{\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05003182 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003183 " v_texcoord = texcoord;\n"
3184 "}\n";
3185
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02003186/* Declare common fragment shader uniforms */
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003187#define FRAGMENT_CONVERT_YUV \
Rob Clark0e5a2d02012-08-30 16:47:18 -05003188 " y *= alpha;\n" \
3189 " u *= alpha;\n" \
3190 " v *= alpha;\n" \
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003191 " gl_FragColor.r = y + 1.59602678 * v;\n" \
3192 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
3193 " gl_FragColor.b = y + 2.01723214 * u;\n" \
Rob Clark0e5a2d02012-08-30 16:47:18 -05003194 " gl_FragColor.a = alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003195
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02003196static const char texture_fragment_shader_rgba[] =
Kristian Høgsbergdfce71d2010-12-07 20:19:10 -05003197 "precision mediump float;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003198 "varying vec2 v_texcoord;\n"
3199 "uniform sampler2D tex;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003200 "uniform float alpha;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003201 "void main()\n"
3202 "{\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003203 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003204 "}\n";
3205
Pekka Paalanen6b5585b2012-08-30 16:47:19 -05003206static const char texture_fragment_shader_rgbx[] =
3207 "precision mediump float;\n"
3208 "varying vec2 v_texcoord;\n"
3209 "uniform sampler2D tex;\n"
3210 "uniform float alpha;\n"
3211 "void main()\n"
3212 "{\n"
3213 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
3214 " gl_FragColor.a = alpha;\n"
3215 "}\n";
3216
Rob Clarke3b95912012-08-31 16:42:18 -05003217static const char texture_fragment_shader_egl_external[] =
3218 "#extension GL_OES_EGL_image_external : require\n"
3219 "precision mediump float;\n"
3220 "varying vec2 v_texcoord;\n"
3221 "uniform samplerExternalOES tex;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003222 "uniform float alpha;\n"
Rob Clarke3b95912012-08-31 16:42:18 -05003223 "void main()\n"
3224 "{\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003225 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Rob Clarke3b95912012-08-31 16:42:18 -05003226 "}\n";
3227
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003228static const char texture_fragment_shader_y_uv[] =
3229 "precision mediump float;\n"
3230 "uniform sampler2D tex;\n"
3231 "uniform sampler2D tex1;\n"
3232 "varying vec2 v_texcoord;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003233 "uniform float alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003234 "void main() {\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003235 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
3236 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
3237 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
3238 FRAGMENT_CONVERT_YUV
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003239 "}\n";
3240
3241static const char texture_fragment_shader_y_u_v[] =
3242 "precision mediump float;\n"
3243 "uniform sampler2D tex;\n"
3244 "uniform sampler2D tex1;\n"
3245 "uniform sampler2D tex2;\n"
3246 "varying vec2 v_texcoord;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003247 "uniform float alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003248 "void main() {\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003249 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
3250 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
3251 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
3252 FRAGMENT_CONVERT_YUV
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003253 "}\n";
3254
3255static const char texture_fragment_shader_y_xuxv[] =
3256 "precision mediump float;\n"
3257 "uniform sampler2D tex;\n"
3258 "uniform sampler2D tex1;\n"
3259 "varying vec2 v_texcoord;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003260 "uniform float alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003261 "void main() {\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003262 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
3263 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
3264 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
3265 FRAGMENT_CONVERT_YUV
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003266 "}\n";
3267
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003268static const char solid_fragment_shader[] =
3269 "precision mediump float;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003270 "uniform vec4 color;\n"
Kristian Høgsbergdbae80e2012-03-29 11:34:39 -04003271 "uniform float alpha;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003272 "void main()\n"
3273 "{\n"
Kristian Høgsbergdbae80e2012-03-29 11:34:39 -04003274 " gl_FragColor = alpha * color\n;"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003275 "}\n";
3276
Kristian Høgsberga9468212010-06-14 21:03:11 -04003277static int
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003278compile_shader(GLenum type, const char *source)
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003279{
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003280 GLuint s;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003281 char msg[512];
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003282 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003283
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003284 s = glCreateShader(type);
3285 glShaderSource(s, 1, &source, NULL);
3286 glCompileShader(s);
3287 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003288 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003289 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
Martin Minarik6d118362012-06-07 18:01:59 +02003290 weston_log("shader info: %s\n", msg);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003291 return GL_NONE;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003292 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003293
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003294 return s;
3295}
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003296
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003297static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003298weston_shader_init(struct weston_shader *shader,
3299 const char *vertex_source, const char *fragment_source)
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003300{
3301 char msg[512];
3302 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003303
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003304 shader->vertex_shader =
3305 compile_shader(GL_VERTEX_SHADER, vertex_source);
3306 shader->fragment_shader =
3307 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
3308
3309 shader->program = glCreateProgram();
3310 glAttachShader(shader->program, shader->vertex_shader);
3311 glAttachShader(shader->program, shader->fragment_shader);
3312 glBindAttribLocation(shader->program, 0, "position");
3313 glBindAttribLocation(shader->program, 1, "texcoord");
3314
3315 glLinkProgram(shader->program);
3316 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003317 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003318 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
Martin Minarik6d118362012-06-07 18:01:59 +02003319 weston_log("link info: %s\n", msg);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003320 return -1;
3321 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003322
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003323 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02003324 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
3325 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
3326 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
Kristian Høgsberg541e5552011-12-14 09:24:11 -05003327 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
Pekka Paalanenf55f5442012-02-02 16:49:05 +02003328 shader->color_uniform = glGetUniformLocation(shader->program, "color");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003329
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003330 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003331}
3332
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003333WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003334weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003335{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003336 struct weston_compositor *c = output->compositor;
3337
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003338 pixman_region32_fini(&output->region);
Kristian Høgsberg8b72f602011-06-23 20:46:34 -04003339 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003340 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003341
3342 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003343}
3344
Scott Moreau1bad5db2012-08-18 01:04:05 -06003345static void
3346weston_output_compute_transform(struct weston_output *output)
3347{
3348 struct weston_matrix transform;
3349 int flip;
3350
3351 weston_matrix_init(&transform);
3352
3353 switch(output->transform) {
3354 case WL_OUTPUT_TRANSFORM_FLIPPED:
3355 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3356 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3357 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3358 flip = -1;
3359 break;
3360 default:
3361 flip = 1;
3362 break;
3363 }
3364
3365 switch(output->transform) {
3366 case WL_OUTPUT_TRANSFORM_NORMAL:
3367 case WL_OUTPUT_TRANSFORM_FLIPPED:
3368 transform.d[0] = flip;
3369 transform.d[1] = 0;
3370 transform.d[4] = 0;
3371 transform.d[5] = 1;
3372 break;
3373 case WL_OUTPUT_TRANSFORM_90:
3374 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3375 transform.d[0] = 0;
3376 transform.d[1] = -flip;
3377 transform.d[4] = 1;
3378 transform.d[5] = 0;
3379 break;
3380 case WL_OUTPUT_TRANSFORM_180:
3381 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3382 transform.d[0] = -flip;
3383 transform.d[1] = 0;
3384 transform.d[4] = 0;
3385 transform.d[5] = -1;
3386 break;
3387 case WL_OUTPUT_TRANSFORM_270:
3388 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3389 transform.d[0] = 0;
3390 transform.d[1] = flip;
3391 transform.d[4] = -1;
3392 transform.d[5] = 0;
3393 break;
3394 default:
3395 break;
3396 }
3397
3398 weston_matrix_multiply(&output->matrix, &transform);
3399}
3400
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003401WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003402weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003403{
Scott Moreau850ca422012-05-21 15:21:25 -06003404 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003405 struct weston_matrix camera;
3406 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003407
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003408 weston_matrix_init(&output->matrix);
3409 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003410 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
3411 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003412
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003413 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003414 2.0 / (output->width + output->border.left + output->border.right),
3415 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
3416
3417 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003418
Scott Moreauccbf29d2012-02-22 14:21:41 -07003419 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003420 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003421 weston_matrix_init(&camera);
3422 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06003423 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06003424 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003425 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003426 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003427 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003428 weston_matrix_multiply(&output->matrix, &modelview);
3429 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003430
Scott Moreauccbf29d2012-02-22 14:21:41 -07003431 output->dirty = 0;
3432}
3433
Scott Moreau1bad5db2012-08-18 01:04:05 -06003434static void
3435weston_output_transform_init(struct weston_output *output, uint32_t transform)
3436{
3437 output->transform = transform;
3438
3439 switch (transform) {
3440 case WL_OUTPUT_TRANSFORM_90:
3441 case WL_OUTPUT_TRANSFORM_270:
3442 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3443 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3444 /* Swap width and height */
3445 output->width = output->current->height;
3446 output->height = output->current->width;
3447 break;
3448 case WL_OUTPUT_TRANSFORM_NORMAL:
3449 case WL_OUTPUT_TRANSFORM_180:
3450 case WL_OUTPUT_TRANSFORM_FLIPPED:
3451 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3452 output->width = output->current->width;
3453 output->height = output->current->height;
3454 break;
3455 default:
3456 break;
3457 }
3458}
3459
Scott Moreauccbf29d2012-02-22 14:21:41 -07003460WL_EXPORT void
3461weston_output_move(struct weston_output *output, int x, int y)
3462{
3463 output->x = x;
3464 output->y = y;
3465
3466 pixman_region32_init(&output->previous_damage);
3467 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003468 output->width,
3469 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003470}
3471
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003472WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003473weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003474 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003475{
3476 output->compositor = c;
3477 output->x = x;
3478 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05003479 output->border.top = 0;
3480 output->border.bottom = 0;
3481 output->border.left = 0;
3482 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003483 output->mm_width = width;
3484 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003485 output->dirty = 1;
3486
Scott Moreau1bad5db2012-08-18 01:04:05 -06003487 if (transform != WL_OUTPUT_TRANSFORM_NORMAL)
3488 output->disable_planes++;
3489
3490 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06003491 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003492
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003493 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003494 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003495
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003496 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003497 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003498 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003499
Casey Dahlin58ba1372012-04-19 22:50:08 -04003500 output->id = ffs(~output->compositor->output_id_pool) - 1;
3501 output->compositor->output_id_pool |= 1 << output->id;
3502
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003503 output->global =
3504 wl_display_add_global(c->wl_display, &wl_output_interface,
3505 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003506}
3507
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003508static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003509compositor_bind(struct wl_client *client,
3510 void *data, uint32_t version, uint32_t id)
3511{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003512 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003513
3514 wl_client_add_object(client, &wl_compositor_interface,
3515 &compositor_interface, id, compositor);
3516}
3517
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003518static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003519log_uname(void)
3520{
3521 struct utsname usys;
3522
3523 uname(&usys);
3524
3525 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3526 usys.version, usys.machine);
3527}
3528
3529static void
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003530log_extensions(const char *name, const char *extensions)
3531{
3532 const char *p, *end;
3533 int l;
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003534 int len;
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003535
3536 l = weston_log("%s:", name);
3537 p = extensions;
3538 while (*p) {
3539 end = strchrnul(p, ' ');
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003540 len = end - p;
3541 if (l + len > 78)
Pekka Paalanen4e4167d2012-06-11 14:06:05 +03003542 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003543 len, p);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003544 else
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003545 l += weston_log_continue(" %.*s", len, p);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003546 for (p = end; isspace(*p); p++)
3547 ;
3548 }
3549 weston_log_continue("\n");
3550}
3551
Pekka Paalanen4e4167d2012-06-11 14:06:05 +03003552static void
3553log_egl_gl_info(EGLDisplay egldpy)
3554{
3555 const char *str;
3556
3557 str = eglQueryString(egldpy, EGL_VERSION);
3558 weston_log("EGL version: %s\n", str ? str : "(null)");
3559
3560 str = eglQueryString(egldpy, EGL_VENDOR);
3561 weston_log("EGL vendor: %s\n", str ? str : "(null)");
3562
3563 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
3564 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
3565
3566 str = eglQueryString(egldpy, EGL_EXTENSIONS);
3567 log_extensions("EGL extensions", str ? str : "(null)");
3568
3569 str = (char *)glGetString(GL_VERSION);
3570 weston_log("GL version: %s\n", str ? str : "(null)");
3571
3572 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
3573 weston_log("GLSL version: %s\n", str ? str : "(null)");
3574
3575 str = (char *)glGetString(GL_VENDOR);
3576 weston_log("GL vendor: %s\n", str ? str : "(null)");
3577
3578 str = (char *)glGetString(GL_RENDERER);
3579 weston_log("GL renderer: %s\n", str ? str : "(null)");
3580
3581 str = (char *)glGetString(GL_EXTENSIONS);
3582 log_extensions("GL extensions", str ? str : "(null)");
3583}
3584
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003585WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003586weston_compositor_init(struct weston_compositor *ec,
3587 struct wl_display *display,
3588 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003589 char *argv[],
3590 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003591{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003592 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003593 struct xkb_rule_names xkb_names;
3594 const struct config_key keyboard_config_keys[] = {
3595 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
3596 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
3597 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
3598 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
3599 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
3600 };
3601 const struct config_section cs[] = {
3602 { "keyboard",
3603 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
3604 };
3605
3606 memset(&xkb_names, 0, sizeof(xkb_names));
3607 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003608
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003609 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003610 wl_signal_init(&ec->destroy_signal);
3611 wl_signal_init(&ec->activate_signal);
3612 wl_signal_init(&ec->lock_signal);
3613 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003614 wl_signal_init(&ec->show_input_panel_signal);
3615 wl_signal_init(&ec->hide_input_panel_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01003616 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003617
Casey Dahlin58ba1372012-04-19 22:50:08 -04003618 ec->output_id_pool = 0;
3619
Kristian Høgsberga8873122011-11-23 10:39:34 -05003620 if (!wl_display_add_global(display, &wl_compositor_interface,
3621 ec, compositor_bind))
3622 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003623
Daniel Stone725c2c32012-06-22 14:04:36 +01003624 wl_list_init(&ec->surface_list);
3625 wl_list_init(&ec->layer_list);
3626 wl_list_init(&ec->seat_list);
3627 wl_list_init(&ec->output_list);
3628 wl_list_init(&ec->key_binding_list);
3629 wl_list_init(&ec->button_binding_list);
3630 wl_list_init(&ec->axis_binding_list);
3631 wl_list_init(&ec->fade.animation.link);
3632
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003633 weston_plane_init(&ec->primary_plane, 0, 0);
3634
Daniel Stone725c2c32012-06-22 14:04:36 +01003635 weston_compositor_xkb_init(ec, &xkb_names);
3636
3637 ec->ping_handler = NULL;
3638
3639 screenshooter_create(ec);
3640 text_cursor_position_notifier_create(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003641
3642 wl_data_device_manager_init(ec->wl_display);
3643
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003644 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003645
Daniel Stone725c2c32012-06-22 14:04:36 +01003646 loop = wl_display_get_event_loop(ec->wl_display);
3647 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3648 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3649
3650 ec->input_loop = wl_event_loop_create();
3651
3652 return 0;
3653}
3654
3655WL_EXPORT int
3656weston_compositor_init_gl(struct weston_compositor *ec)
3657{
3658 const char *extensions;
Rob Clarke3b95912012-08-31 16:42:18 -05003659 int has_egl_image_external = 0;
Daniel Stone725c2c32012-06-22 14:04:36 +01003660
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003661 log_egl_gl_info(ec->egl_display);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003662
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003663 ec->image_target_texture_2d =
3664 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3665 ec->image_target_renderbuffer_storage = (void *)
3666 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
3667 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3668 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3669 ec->bind_display =
3670 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
3671 ec->unbind_display =
3672 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02003673 ec->query_buffer =
3674 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003675
3676 extensions = (const char *) glGetString(GL_EXTENSIONS);
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003677 if (!extensions) {
Martin Minarik6d118362012-06-07 18:01:59 +02003678 weston_log("Retrieving GL extension string failed.\n");
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003679 return -1;
3680 }
3681
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003682 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
Martin Minarik6d118362012-06-07 18:01:59 +02003683 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003684 return -1;
3685 }
3686
Pekka Paalanena1d57db2012-04-17 15:02:08 +03003687 if (strstr(extensions, "GL_EXT_read_format_bgra"))
3688 ec->read_format = GL_BGRA_EXT;
3689 else
3690 ec->read_format = GL_RGBA;
Kristian Høgsbergf9247dd2012-03-27 15:25:46 -04003691
Pekka Paalanen52bfbaa2012-04-11 16:19:37 +03003692 if (strstr(extensions, "GL_EXT_unpack_subimage"))
3693 ec->has_unpack_subimage = 1;
Kristian Høgsbergfb6de222012-03-27 17:10:13 -04003694
Rob Clarke3b95912012-08-31 16:42:18 -05003695 if (strstr(extensions, "GL_OES_EGL_image_external"))
3696 has_egl_image_external = 1;
3697
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003698 extensions =
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003699 (const char *) eglQueryString(ec->egl_display, EGL_EXTENSIONS);
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003700 if (!extensions) {
Martin Minarik6d118362012-06-07 18:01:59 +02003701 weston_log("Retrieving EGL extension string failed.\n");
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003702 return -1;
3703 }
3704
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003705 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
3706 ec->has_bind_display = 1;
3707 if (ec->has_bind_display)
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003708 ec->bind_display(ec->egl_display, ec->wl_display);
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04003709
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003710 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003711 ec->fade.animation.frame = fade_frame;
Kristian Høgsberg3555d092011-04-11 13:58:13 -04003712
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003713 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3714 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3715
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003716 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003717
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02003718 if (weston_shader_init(&ec->texture_shader_rgba,
3719 vertex_shader, texture_fragment_shader_rgba) < 0)
Kristian Høgsberga9468212010-06-14 21:03:11 -04003720 return -1;
Pekka Paalanen6b5585b2012-08-30 16:47:19 -05003721 if (weston_shader_init(&ec->texture_shader_rgbx,
3722 vertex_shader, texture_fragment_shader_rgbx) < 0)
3723 return -1;
Rob Clarke3b95912012-08-31 16:42:18 -05003724 if (has_egl_image_external &&
3725 weston_shader_init(&ec->texture_shader_egl_external,
3726 vertex_shader, texture_fragment_shader_egl_external) < 0)
3727 return -1;
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003728 if (weston_shader_init(&ec->texture_shader_y_uv,
3729 vertex_shader, texture_fragment_shader_y_uv) < 0)
3730 return -1;
3731 if (weston_shader_init(&ec->texture_shader_y_u_v,
3732 vertex_shader, texture_fragment_shader_y_u_v) < 0)
3733 return -1;
3734 if (weston_shader_init(&ec->texture_shader_y_xuxv,
3735 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
3736 return -1;
Pekka Paalanenf55f5442012-02-02 16:49:05 +02003737 if (weston_shader_init(&ec->solid_shader,
3738 vertex_shader, solid_fragment_shader) < 0)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003739 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05003740
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003741 weston_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003742
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003743 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003744}
3745
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003746WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003747weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003748{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003749 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003750
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003751 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003752 if (ec->input_loop_source)
3753 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003754
Matt Roper361d2ad2011-08-29 13:52:23 -07003755 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003756 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003757 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003758
Daniel Stone325fc2d2012-05-30 16:31:58 +01003759 weston_binding_list_destroy_all(&ec->key_binding_list);
3760 weston_binding_list_destroy_all(&ec->button_binding_list);
3761 weston_binding_list_destroy_all(&ec->axis_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003762
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003763 weston_plane_release(&ec->primary_plane);
3764
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003765 wl_array_release(&ec->vertices);
3766 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05003767 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003768
3769 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07003770}
3771
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003772static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003773{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003774 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003775
Martin Minarik6d118362012-06-07 18:01:59 +02003776 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003777 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003778
3779 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003780}
3781
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003782static void
3783on_segv_signal(int s, siginfo_t *siginfo, void *context)
3784{
3785 void *buffer[32];
3786 int i, count;
3787 Dl_info info;
3788
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003789 /* This SIGSEGV handler will do a best-effort backtrace, and
3790 * then call the backend restore function, which will switch
3791 * back to the vt we launched from or ungrab X etc and then
3792 * raise SIGTRAP. If we run weston under gdb from X or a
3793 * different vt, and tell gdb "handle SIGSEGV nostop", this
3794 * will allow weston to switch back to gdb on crash and then
3795 * gdb will catch the crash with SIGTRAP. */
3796
Martin Minarik6d118362012-06-07 18:01:59 +02003797 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003798
3799 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3800 for (i = 0; i < count; i++) {
3801 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02003802 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003803 (long) buffer[i],
3804 info.dli_sname ? info.dli_sname : "--",
3805 info.dli_fname);
3806 }
3807
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003808 segv_compositor->restore(segv_compositor);
3809
3810 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003811}
3812
3813
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003814static void *
3815load_module(const char *name, const char *entrypoint, void **handle)
3816{
3817 char path[PATH_MAX];
3818 void *module, *init;
3819
3820 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003821 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003822 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003823 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003824
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003825 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003826 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003827 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003828 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003829 return NULL;
3830 }
3831
3832 init = dlsym(module, entrypoint);
3833 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003834 weston_log("Failed to lookup init function: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003835 return NULL;
3836 }
3837
3838 return init;
3839}
3840
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003841static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003842 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3843
3844static const char xdg_wrong_message[] =
3845 "fatal: environment variable XDG_RUNTIME_DIR\n"
3846 "is set to \"%s\", which is not a directory.\n";
3847
3848static const char xdg_wrong_mode_message[] =
3849 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3850 "correctly. Unix access mode must be 0700 but is %o,\n"
3851 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003852 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003853
3854static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003855 "Refer to your distribution on how to get it, or\n"
3856 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3857 "on how to implement it.\n";
3858
Martin Minarik37032f82012-06-18 20:15:18 +02003859static void
3860verify_xdg_runtime_dir(void)
3861{
3862 char *dir = getenv("XDG_RUNTIME_DIR");
3863 struct stat s;
3864
3865 if (!dir) {
3866 weston_log(xdg_error_message);
3867 weston_log_continue(xdg_detail_message);
3868 exit(EXIT_FAILURE);
3869 }
3870
3871 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3872 weston_log(xdg_wrong_message, dir);
3873 weston_log_continue(xdg_detail_message);
3874 exit(EXIT_FAILURE);
3875 }
3876
3877 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3878 weston_log(xdg_wrong_mode_message,
3879 dir, s.st_mode & 0777, s.st_uid);
3880 weston_log_continue(xdg_detail_message);
3881 }
3882}
3883
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003884static int
3885usage(int error_code)
3886{
3887 fprintf(stderr,
3888 "Usage: weston [OPTIONS]\n\n"
3889 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3890 "Weston supports multiple backends, and depending on which backend is in use\n"
3891 "different options will be accepted.\n\n"
3892
3893
3894 "Core options:\n\n"
3895 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3896 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3897 " -S, --socket=NAME\tName of socket to listen on\n"
3898 " -i, --idle-time=SECS\tIdle time in seconds\n"
3899 " --xserver\t\tEnable X server integration\n"
3900 " --module\t\tLoad the specified module\n"
3901 " --log==FILE\t\tLog to the given file\n"
3902 " -h, --help\t\tThis help message\n\n");
3903
3904 fprintf(stderr,
3905 "Options for drm-backend.so:\n\n"
3906 " --connector=ID\tBring up only this connector\n"
3907 " --seat=SEAT\t\tThe seat that weston should run on\n"
3908 " --tty=TTY\t\tThe tty to use\n"
3909 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3910
3911 fprintf(stderr,
3912 "Options for x11-backend.so:\n\n"
3913 " --width=WIDTH\t\tWidth of X window\n"
3914 " --height=HEIGHT\tHeight of X window\n"
3915 " --fullscreen\t\tRun in fullscreen mode\n"
3916 " --output-count=COUNT\tCreate multiple outputs\n"
3917 " --no-input\t\tDont create input devices\n\n");
3918
3919 fprintf(stderr,
3920 "Options for wayland-backend.so:\n\n"
3921 " --width=WIDTH\t\tWidth of Wayland surface\n"
3922 " --height=HEIGHT\tHeight of Wayland surface\n"
3923 " --display=DISPLAY\tWayland display to connect to\n\n");
3924
3925 exit(error_code);
3926}
3927
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003928int main(int argc, char *argv[])
3929{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003930 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003931 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003932 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003933 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003934 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003935 struct sigaction segv_action;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04003936 void *shell_module, *backend_module, *xserver_module;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003937 int (*module_init)(struct weston_compositor *ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003938 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003939 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003940 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003941 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003942 char *backend = NULL;
3943 char *shell = NULL;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003944 char *module = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003945 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003946 int32_t idle_time = 300;
Scott Moreaue17f6102012-04-25 10:03:06 -06003947 int32_t xserver = 0;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003948 int32_t help = 0;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003949 char *socket_name = NULL;
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003950 char *config_file;
3951
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003952 const struct config_key shell_config_keys[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003953 { "type", CONFIG_KEY_STRING, &shell },
3954 };
3955
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003956 const struct config_section cs[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003957 { "shell",
3958 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
3959 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003960
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003961 const struct weston_option core_options[] = {
3962 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3963 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3964 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003965 { WESTON_OPTION_BOOLEAN, "xserver", 0, &xserver },
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003966 { WESTON_OPTION_STRING, "module", 0, &module },
Martin Minarik19e6f262012-06-07 13:08:46 +02003967 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003968 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003969 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003970
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003971 argc = parse_options(core_options,
3972 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003973
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003974 if (help)
3975 usage(EXIT_SUCCESS);
3976
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003977 weston_log_file_open(log);
3978
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003979 weston_log("%s\n"
3980 STAMP_SPACE "%s\n"
3981 STAMP_SPACE "Bug reports to: %s\n"
3982 STAMP_SPACE "Build: %s\n",
3983 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003984 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003985 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003986
Martin Minarik37032f82012-06-18 20:15:18 +02003987 verify_xdg_runtime_dir();
3988
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003989 display = wl_display_create();
3990
Tiago Vignatti2116b892011-08-08 05:52:59 -07003991 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003992 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3993 display);
3994 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3995 display);
3996 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3997 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003998
3999 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004000 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4001 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004002
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004003 if (!backend) {
4004 if (getenv("WAYLAND_DISPLAY"))
4005 backend = "wayland-backend.so";
4006 else if (getenv("DISPLAY"))
4007 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004008 else
4009 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004010 }
4011
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004012 config_file = config_file_path("weston.ini");
4013 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004014
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004015 backend_init = load_module(backend, "backend_init", &backend_module);
4016 if (!backend_init)
4017 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004018
Daniel Stonec1be8e52012-06-01 11:14:02 -04004019 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004020 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004021 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004022 exit(EXIT_FAILURE);
4023 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004024
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004025 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4026 segv_action.sa_sigaction = on_segv_signal;
4027 sigemptyset(&segv_action.sa_mask);
4028 sigaction(SIGSEGV, &segv_action, NULL);
4029 segv_compositor = ec;
4030
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004031 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03004032 weston_log("fatal: unhandled option: %s\n", argv[i]);
4033 if (argv[1]) {
4034 ret = EXIT_FAILURE;
4035 goto out;
4036 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004037
Daniel Stonec1be8e52012-06-01 11:14:02 -04004038 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01004039
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004040 ec->option_idle_time = idle_time;
4041 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004042
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004043 module_init = NULL;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04004044 if (xserver)
Tiago Vignatti629ce232012-05-23 23:04:14 +03004045 module_init = load_module("xwayland.so",
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004046 "weston_xserver_init",
4047 &xserver_module);
Pekka Paalanen33616392012-07-30 16:56:57 +03004048 if (module_init && module_init(ec) < 0) {
4049 ret = EXIT_FAILURE;
4050 goto out;
4051 }
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04004052
Kristian Høgsberg33d75092012-08-13 13:56:03 -04004053 if (socket_name)
4054 setenv("WAYLAND_DISPLAY", socket_name, 1);
4055
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004056 if (!shell)
4057 shell = "desktop-shell.so";
4058 module_init = load_module(shell, "shell_init", &shell_module);
Pekka Paalanen33616392012-07-30 16:56:57 +03004059 if (!module_init || module_init(ec) < 0) {
4060 ret = EXIT_FAILURE;
4061 goto out;
4062 }
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004063
4064
4065 module_init = NULL;
4066 if (module)
4067 module_init = load_module(module, "module_init", NULL);
Pekka Paalanen33616392012-07-30 16:56:57 +03004068 if (module_init && module_init(ec) < 0) {
4069 ret = EXIT_FAILURE;
4070 goto out;
4071 }
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004072
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004073 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004074 weston_log("fatal: failed to add socket: %m\n");
4075 ret = EXIT_FAILURE;
4076 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004077 }
4078
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004079 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004080 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004081
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004082 wl_display_run(display);
4083
4084 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004085 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05004086 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004087
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004088 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004089
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004090 if (ec->has_bind_display)
Kristian Høgsberg362b6722012-06-18 15:13:51 -04004091 ec->unbind_display(ec->egl_display, display);
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004092
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004093 for (i = ARRAY_LENGTH(signals); i;)
4094 wl_event_source_remove(signals[--i]);
4095
Daniel Stone855028f2012-05-01 20:37:10 +01004096 weston_compositor_xkb_destroy(ec);
4097
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004098 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004099 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004100
Martin Minarik19e6f262012-06-07 13:08:46 +02004101 weston_log_file_close();
4102
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004103 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004104}