blob: 622db5812fd7542a0e0f2d35aea66cb577942cad [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
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040029#include <stdio.h>
30#include <string.h>
31#include <stdlib.h>
32#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010033#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050034#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020035#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040036#include <sys/ioctl.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040037#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020038#include <sys/types.h>
39#include <sys/socket.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040040#include <fcntl.h>
41#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050042#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040043#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040044#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040045#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050046#include <setjmp.h>
47#include <execinfo.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050048
Pekka Paalanen50719bc2011-11-22 14:18:50 +020049#include <wayland-server.h>
Kristian Høgsberg82863022010-06-04 21:52:02 -040050#include "compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050051
Kristian Høgsberg27da5382011-06-21 17:32:25 -040052static struct wl_list child_process_list;
Kristian Høgsberg0690da62012-01-16 11:53:54 -050053static jmp_buf segv_jmp_buf;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040054
55static int
56sigchld_handler(int signal_number, void *data)
57{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050058 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040059 int status;
60 pid_t pid;
61
Bill Spitzak027b9622012-03-17 15:22:03 -070062 pid = waitpid(-1, &status, WNOHANG);
63 if (!pid)
64 return 1;
65
Kristian Høgsberg27da5382011-06-21 17:32:25 -040066 wl_list_for_each(p, &child_process_list, link) {
67 if (p->pid == pid)
68 break;
69 }
70
71 if (&p->link == &child_process_list) {
72 fprintf(stderr, "unknown child process exited\n");
73 return 1;
74 }
75
76 wl_list_remove(&p->link);
77 p->cleanup(p, status);
78
79 return 1;
80}
81
Alex Wu2dda6042012-04-17 17:20:47 +080082WL_EXPORT int
83weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode)
84{
85 if (!output->switch_mode)
86 return -1;
87
88 return output->switch_mode(output, mode);
89}
90
Kristian Høgsberg27da5382011-06-21 17:32:25 -040091WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050092weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -040093{
94 wl_list_insert(&child_process_list, &process->link);
95}
96
Benjamin Franzke06286262011-05-06 19:12:33 +020097static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020098child_client_exec(int sockfd, const char *path)
99{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500100 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200101 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200102 sigset_t allsigs;
103
104 /* do not give our signal mask to the new process */
105 sigfillset(&allsigs);
106 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200107
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500108 /* Launch clients as the user. */
109 seteuid(getuid());
110
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500111 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
112 * non-CLOEXEC fd to pass through exec. */
113 clientfd = dup(sockfd);
114 if (clientfd == -1) {
115 fprintf(stderr, "compositor: dup failed: %m\n");
116 return;
117 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200118
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500119 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200120 setenv("WAYLAND_SOCKET", s, 1);
121
122 if (execl(path, path, NULL) < 0)
123 fprintf(stderr, "compositor: executing '%s' failed: %m\n",
124 path);
125}
126
127WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500128weston_client_launch(struct weston_compositor *compositor,
129 struct weston_process *proc,
130 const char *path,
131 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200132{
133 int sv[2];
134 pid_t pid;
135 struct wl_client *client;
136
137 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500138 fprintf(stderr, "weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200139 "socketpair failed while launching '%s': %m\n",
140 path);
141 return NULL;
142 }
143
144 pid = fork();
145 if (pid == -1) {
146 close(sv[0]);
147 close(sv[1]);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500148 fprintf(stderr, "weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200149 "fork failed while launching '%s': %m\n", path);
150 return NULL;
151 }
152
153 if (pid == 0) {
154 child_client_exec(sv[1], path);
155 exit(-1);
156 }
157
158 close(sv[1]);
159
160 client = wl_client_create(compositor->wl_display, sv[0]);
161 if (!client) {
162 close(sv[0]);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500163 fprintf(stderr, "weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200164 "wl_client_create failed while launching '%s'.\n",
165 path);
166 return NULL;
167 }
168
169 proc->pid = pid;
170 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500171 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200172
173 return client;
174}
175
176static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400177surface_handle_buffer_destroy(struct wl_listener *listener, void *data)
Benjamin Franzke06286262011-05-06 19:12:33 +0200178{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500179 struct weston_surface *es =
180 container_of(listener, struct weston_surface,
181 buffer_destroy_listener);
Benjamin Franzke06286262011-05-06 19:12:33 +0200182
Kristian Høgsberg24596942011-10-24 17:51:02 -0400183 es->buffer = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200184}
185
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500186static const pixman_region32_data_t undef_region_data;
187
188static void
189undef_region(pixman_region32_t *region)
190{
191 pixman_region32_fini(region);
192 region->data = (pixman_region32_data_t *) &undef_region_data;
193}
194
195static int
196region_is_undefined(pixman_region32_t *region)
197{
198 return region->data == &undef_region_data;
199}
200
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200201static void
202empty_region(pixman_region32_t *region)
203{
204 if (!region_is_undefined(region))
205 pixman_region32_fini(region);
206
207 pixman_region32_init(region);
208}
209
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500210WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500211weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500212{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500213 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400214
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200215 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400216 if (surface == NULL)
217 return NULL;
218
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400219 wl_signal_init(&surface->surface.resource.destroy_signal);
Kristian Høgsberg48891542012-02-23 17:38:33 -0500220
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500221 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500222 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500223
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400224 surface->surface.resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400225
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500226 surface->compositor = compositor;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200227 surface->image = EGL_NO_IMAGE_KHR;
Kristian Høgsberg541e5552011-12-14 09:24:11 -0500228 surface->alpha = 255;
Scott Moreau69367082012-04-17 19:06:19 -0600229 surface->brightness = 255;
230 surface->saturation = 255;
Juan Zhao46436612012-03-20 01:48:46 +0800231 surface->pitch = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400232
Benjamin Franzke06286262011-05-06 19:12:33 +0200233 surface->buffer = NULL;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400234 surface->output = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200235
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400236 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500237 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500238 pixman_region32_init(&surface->clip);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500239 undef_region(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200240 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500241 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400242
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400243 surface->buffer_destroy_listener.notify =
244 surface_handle_buffer_destroy;
Benjamin Franzke06286262011-05-06 19:12:33 +0200245
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200246 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200247 wl_list_insert(&surface->geometry.transformation_list,
248 &surface->transform.position.link);
249 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200250 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200251 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500252
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400253 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500254}
255
Alex Wu8811bf92012-02-28 18:07:54 +0800256WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500257weston_surface_set_color(struct weston_surface *surface,
258 GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
259{
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500260 surface->color[0] = red;
261 surface->color[1] = green;
262 surface->color[2] = blue;
263 surface->color[3] = alpha;
264 surface->shader = &surface->compositor->solid_shader;
265}
266
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200267static void
Pekka Paalanenece8a012012-02-08 15:23:15 +0200268surface_to_global_float(struct weston_surface *surface,
Daniel Stonebd3489b2012-05-08 17:17:53 +0100269 GLfloat sx, GLfloat sy, GLfloat *x, GLfloat *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200270{
271 if (surface->transform.enabled) {
272 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
273
274 weston_matrix_transform(&surface->transform.matrix, &v);
275
276 if (fabsf(v.f[3]) < 1e-6) {
277 fprintf(stderr, "warning: numerical instability in "
278 "weston_surface_to_global(), divisor = %g\n",
279 v.f[3]);
280 *x = 0;
281 *y = 0;
282 return;
283 }
284
285 *x = v.f[0] / v.f[3];
286 *y = v.f[1] / v.f[3];
287 } else {
288 *x = sx + surface->geometry.x;
289 *y = sy + surface->geometry.y;
290 }
291}
292
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500293WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500294weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200295{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500296 struct weston_compositor *compositor = surface->compositor;
297 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200298
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500299 pixman_region32_init(&damage);
300 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
301 &surface->clip);
302 pixman_region32_union(&compositor->damage,
303 &compositor->damage, &damage);
304 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200305}
306
307static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200308surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
309 int32_t width, int32_t height,
310 pixman_region32_t *bbox)
311{
Pekka Paalanen219b9822012-02-08 15:38:37 +0200312 GLfloat min_x = HUGE_VALF, min_y = HUGE_VALF;
313 GLfloat max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200314 int32_t s[4][2] = {
315 { sx, sy },
316 { sx, sy + height },
317 { sx + width, sy },
318 { sx + width, sy + height }
319 };
Pekka Paalanen219b9822012-02-08 15:38:37 +0200320 GLfloat int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200321 int i;
322
323 for (i = 0; i < 4; ++i) {
Pekka Paalanen219b9822012-02-08 15:38:37 +0200324 GLfloat x, y;
325 surface_to_global_float(surface, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200326 if (x < min_x)
327 min_x = x;
328 if (x > max_x)
329 max_x = x;
330 if (y < min_y)
331 min_y = y;
332 if (y > max_y)
333 max_y = y;
334 }
335
Pekka Paalanen219b9822012-02-08 15:38:37 +0200336 int_x = floorf(min_x);
337 int_y = floorf(min_y);
338 pixman_region32_init_rect(bbox, int_x, int_y,
339 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200340}
341
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200342static void
343weston_surface_update_transform_disable(struct weston_surface *surface)
344{
345 surface->transform.enabled = 0;
346
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200347 /* round off fractions when not transformed */
348 surface->geometry.x = roundf(surface->geometry.x);
349 surface->geometry.y = roundf(surface->geometry.y);
350
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200351 pixman_region32_init_rect(&surface->transform.boundingbox,
352 surface->geometry.x,
353 surface->geometry.y,
354 surface->geometry.width,
355 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500356
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500357 if (surface->alpha == 255) {
358 pixman_region32_copy(&surface->transform.opaque,
359 &surface->opaque);
360 pixman_region32_translate(&surface->transform.opaque,
361 surface->geometry.x,
362 surface->geometry.y);
363 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200364}
365
366static int
367weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200368{
369 struct weston_matrix *matrix = &surface->transform.matrix;
370 struct weston_matrix *inverse = &surface->transform.inverse;
371 struct weston_transform *tform;
372
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200373 surface->transform.enabled = 1;
374
375 /* Otherwise identity matrix, but with x and y translation. */
376 surface->transform.position.matrix.d[12] = surface->geometry.x;
377 surface->transform.position.matrix.d[13] = surface->geometry.y;
378
379 weston_matrix_init(matrix);
380 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
381 weston_matrix_multiply(matrix, &tform->matrix);
382
383 if (weston_matrix_invert(inverse, matrix) < 0) {
384 /* Oops, bad total transformation, not invertible */
385 fprintf(stderr, "error: weston_surface %p"
386 " transformation not invertible.\n", surface);
387 return -1;
388 }
389
390 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
391 surface->geometry.height,
392 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500393
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200394 return 0;
395}
396
397WL_EXPORT void
398weston_surface_update_transform(struct weston_surface *surface)
399{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200400 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200401 return;
402
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200403 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200404
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500405 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200406
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200407 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500408 pixman_region32_fini(&surface->transform.opaque);
409 pixman_region32_init(&surface->transform.opaque);
410
411 if (region_is_undefined(&surface->input))
412 pixman_region32_init_rect(&surface->input, 0, 0,
413 surface->geometry.width,
414 surface->geometry.height);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200415
Pekka Paalanencd403622012-01-25 13:37:39 +0200416 /* transform.position is always in transformation_list */
417 if (surface->geometry.transformation_list.next ==
418 &surface->transform.position.link &&
419 surface->geometry.transformation_list.prev ==
420 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200421 weston_surface_update_transform_disable(surface);
422 } else {
423 if (weston_surface_update_transform_enable(surface) < 0)
424 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200425 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200426
427 /* weston_surface_damage() without update */
428 pixman_region32_union(&surface->damage, &surface->damage,
429 &surface->transform.boundingbox);
430
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300431 if (weston_surface_is_mapped(surface))
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +0200432 weston_surface_assign_output(surface);
433
Pekka Paalanen96516782012-02-09 15:32:15 +0200434 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200435}
436
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200437WL_EXPORT void
Ander Conselvan de Oliveirafbf28942012-03-27 17:36:37 +0300438weston_surface_to_global_float(struct weston_surface *surface,
Daniel Stonebd3489b2012-05-08 17:17:53 +0100439 GLfloat sx, GLfloat sy, GLfloat *x, GLfloat *y)
Ander Conselvan de Oliveirafbf28942012-03-27 17:36:37 +0300440{
Ander Conselvan de Oliveirafbf28942012-03-27 17:36:37 +0300441 surface_to_global_float(surface, sx, sy, x, y);
442}
443
444WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100445weston_surface_to_global_fixed(struct weston_surface *surface,
446 wl_fixed_t sx, wl_fixed_t sy,
447 wl_fixed_t *x, wl_fixed_t *y)
448{
449 GLfloat xf, yf;
450
451 weston_surface_to_global_float(surface,
452 wl_fixed_to_double(sx),
453 wl_fixed_to_double(sy),
454 &xf, &yf);
455 *x = wl_fixed_from_double(xf);
456 *y = wl_fixed_from_double(yf);
457}
458
459WL_EXPORT void
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200460weston_surface_to_global(struct weston_surface *surface,
461 int32_t sx, int32_t sy, int32_t *x, int32_t *y)
462{
463 GLfloat xf, yf;
464
Ander Conselvan de Oliveirafbf28942012-03-27 17:36:37 +0300465 weston_surface_to_global_float(surface, sx, sy, &xf, &yf);
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200466 *x = floorf(xf);
467 *y = floorf(yf);
468}
469
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200470static void
471surface_from_global_float(struct weston_surface *surface,
Daniel Stonebd3489b2012-05-08 17:17:53 +0100472 GLfloat x, GLfloat y, GLfloat *sx, GLfloat *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200473{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200474 if (surface->transform.enabled) {
475 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
476
477 weston_matrix_transform(&surface->transform.inverse, &v);
478
479 if (fabsf(v.f[3]) < 1e-6) {
480 fprintf(stderr, "warning: numerical instability in "
481 "weston_surface_from_global(), divisor = %g\n",
482 v.f[3]);
483 *sx = 0;
484 *sy = 0;
485 return;
486 }
487
Pekka Paalanencd403622012-01-25 13:37:39 +0200488 *sx = v.f[0] / v.f[3];
489 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200490 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200491 *sx = x - surface->geometry.x;
492 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200493 }
494}
495
496WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100497weston_surface_from_global_fixed(struct weston_surface *surface,
498 wl_fixed_t x, wl_fixed_t y,
499 wl_fixed_t *sx, wl_fixed_t *sy)
500{
501 GLfloat sxf, syf;
502
Daniel Stonebd3489b2012-05-08 17:17:53 +0100503 surface_from_global_float(surface,
504 wl_fixed_to_double(x),
505 wl_fixed_to_double(y),
506 &sxf, &syf);
507 *sx = wl_fixed_from_double(sxf);
508 *sy = wl_fixed_from_double(syf);
509}
510
511WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200512weston_surface_from_global(struct weston_surface *surface,
513 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
514{
515 GLfloat sxf, syf;
516
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200517 surface_from_global_float(surface, x, y, &sxf, &syf);
518 *sx = floorf(sxf);
519 *sy = floorf(syf);
520}
521
Tiago Vignatti9d393522012-02-10 16:26:19 +0200522static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500523weston_surface_damage_rectangle(struct weston_surface *surface,
Pekka Paalanen2267d452012-01-26 13:12:45 +0200524 int32_t sx, int32_t sy,
525 int32_t width, int32_t height)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500526{
Pekka Paalanen2267d452012-01-26 13:12:45 +0200527 if (surface->transform.enabled) {
528 pixman_region32_t box;
529 surface_compute_bbox(surface, sx, sy, width, height, &box);
530 pixman_region32_union(&surface->damage, &surface->damage,
531 &box);
532 pixman_region32_fini(&box);
533 } else {
Pekka Paalanen2267d452012-01-26 13:12:45 +0200534 pixman_region32_union_rect(&surface->damage, &surface->damage,
Pekka Paalanen1d5035c2012-02-09 15:27:45 +0200535 surface->geometry.x + sx,
536 surface->geometry.y + sy,
537 width, height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200538 }
539
540 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500541}
542
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400543WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500544weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500545{
Pekka Paalanen2267d452012-01-26 13:12:45 +0200546 pixman_region32_union(&surface->damage, &surface->damage,
547 &surface->transform.boundingbox);
548
549 weston_compositor_schedule_repaint(surface->compositor);
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
Daniel Stone37816df2012-05-16 18:45:18 +0100611weston_device_repick(struct wl_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500612{
Daniel Stone37816df2012-05-16 18:45:18 +0100613 struct weston_seat *ws = (struct weston_seat *) seat;
Scott Moreau447013d2012-02-18 05:05:29 -0700614 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500615 struct weston_surface *surface, *focus;
616
Daniel Stone37816df2012-05-16 18:45:18 +0100617 surface = weston_compositor_pick_surface(ws->compositor,
618 seat->pointer->x,
619 seat->pointer->y,
620 &seat->pointer->current_x,
621 &seat->pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500622
Daniel Stone37816df2012-05-16 18:45:18 +0100623 if (&surface->surface != seat->pointer->current) {
624 interface = seat->pointer->grab->interface;
625 interface->focus(seat->pointer->grab, &surface->surface,
626 seat->pointer->current_x,
627 seat->pointer->current_y);
628 seat->pointer->current = &surface->surface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500629 }
630
Daniel Stone37816df2012-05-16 18:45:18 +0100631 focus = (struct weston_surface *) seat->pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500632 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100633 weston_surface_from_global_fixed(focus,
634 seat->pointer->x,
635 seat->pointer->y,
636 &seat->pointer->grab->x,
637 &seat->pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500638}
639
640WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500641weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400642{
Daniel Stone37816df2012-05-16 18:45:18 +0100643 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400644
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500645 if (!compositor->focus)
646 return;
647
Daniel Stone37816df2012-05-16 18:45:18 +0100648 wl_list_for_each(seat, &compositor->seat_list, link)
649 weston_device_repick(&seat->seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400650}
651
652static void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500653weston_surface_unmap(struct weston_surface *surface)
654{
Daniel Stone37816df2012-05-16 18:45:18 +0100655 struct wl_seat *seat = &surface->compositor->seat->seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500656
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500657 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500658 surface->output = NULL;
659 wl_list_remove(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500660 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500661
Daniel Stone37816df2012-05-16 18:45:18 +0100662 if (seat->keyboard->focus == &surface->surface)
663 wl_keyboard_set_focus(seat->keyboard, NULL);
664 if (seat->pointer->focus == &surface->surface)
665 wl_pointer_set_focus(seat->pointer,
666 NULL,
667 wl_fixed_from_int(0),
668 wl_fixed_from_int(0));
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500669
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500670 weston_compositor_schedule_repaint(surface->compositor);
671}
672
673static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400674destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500675{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500676 struct weston_surface *surface =
677 container_of(resource,
678 struct weston_surface, surface.resource);
679 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400680
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300681 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500682 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400683
Kristian Høgsberg12bbf812012-02-17 12:15:27 -0500684 if (surface->texture)
685 glDeleteTextures(1, &surface->texture);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200686
Benjamin Franzke06286262011-05-06 19:12:33 +0200687 if (surface->buffer)
688 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400689
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200690 if (surface->image != EGL_NO_IMAGE_KHR)
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400691 compositor->destroy_image(compositor->display,
692 surface->image);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200693
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200694 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200695 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500696 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500697 pixman_region32_fini(&surface->clip);
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500698 if (!region_is_undefined(&surface->input))
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500699 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200700
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400701 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500702}
703
Alex Wu8811bf92012-02-28 18:07:54 +0800704WL_EXPORT void
705weston_surface_destroy(struct weston_surface *surface)
706{
707 /* Not a valid way to destroy a client surface */
708 assert(surface->surface.resource.client == NULL);
709
710 destroy_surface(&surface->surface.resource);
711}
712
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100713static void
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -0400714weston_surface_attach(struct wl_surface *surface, struct wl_buffer *buffer)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100715{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500716 struct weston_surface *es = (struct weston_surface *) surface;
717 struct weston_compositor *ec = es->compositor;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100718
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300719 if (es->buffer) {
720 weston_buffer_post_release(es->buffer);
721 wl_list_remove(&es->buffer_destroy_listener.link);
722 }
723
724 es->buffer = buffer;
725
726 if (!buffer) {
727 if (weston_surface_is_mapped(es))
728 weston_surface_unmap(es);
729 return;
730 }
731
732 buffer->busy_count++;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400733 wl_signal_add(&es->buffer->resource.destroy_signal,
734 &es->buffer_destroy_listener);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300735
736 if (es->geometry.width != buffer->width ||
737 es->geometry.height != buffer->height) {
738 undef_region(&es->input);
739 pixman_region32_fini(&es->opaque);
740 pixman_region32_init(&es->opaque);
741 }
742
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500743 if (!es->texture) {
744 glGenTextures(1, &es->texture);
745 glBindTexture(GL_TEXTURE_2D, es->texture);
746 glTexParameteri(GL_TEXTURE_2D,
747 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
748 glTexParameteri(GL_TEXTURE_2D,
749 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
750 es->shader = &ec->texture_shader;
751 } else {
752 glBindTexture(GL_TEXTURE_2D, es->texture);
753 }
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100754
755 if (wl_buffer_is_shm(buffer)) {
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200756 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
Kristian Høgsbergfb6de222012-03-27 17:10:13 -0400757 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
758 es->pitch, es->buffer->height, 0,
759 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100760 } else {
Benjamin Franzkefb4b5a22011-06-21 10:44:37 +0200761 if (es->image != EGL_NO_IMAGE_KHR)
762 ec->destroy_image(ec->display, es->image);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400763 es->image = ec->create_image(ec->display, NULL,
764 EGL_WAYLAND_BUFFER_WL,
765 buffer, NULL);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300766
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400767 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400768
Ander Conselvan de Oliveira9390ae32012-03-27 17:36:38 +0300769 es->pitch = buffer->width;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100770 }
771}
772
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400773static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500774texture_region(struct weston_surface *es, pixman_region32_t *region)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500775{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500776 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500777 GLfloat *v, inv_width, inv_height;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200778 GLfloat sx, sy;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500779 pixman_box32_t *rectangles;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400780 unsigned int *p;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500781 int i, n;
782
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400783 rectangles = pixman_region32_rectangles(region, &n);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500784 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
785 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200786 inv_width = 1.0 / es->pitch;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200787 inv_height = 1.0 / es->geometry.height;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400788
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500789 for (i = 0; i < n; i++, v += 16, p += 6) {
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200790 surface_from_global_float(es, rectangles[i].x1,
791 rectangles[i].y1, &sx, &sy);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500792 v[ 0] = rectangles[i].x1;
793 v[ 1] = rectangles[i].y1;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200794 v[ 2] = sx * inv_width;
795 v[ 3] = sy * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500796
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200797 surface_from_global_float(es, rectangles[i].x1,
798 rectangles[i].y2, &sx, &sy);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500799 v[ 4] = rectangles[i].x1;
800 v[ 5] = rectangles[i].y2;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200801 v[ 6] = sx * inv_width;
802 v[ 7] = sy * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500803
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200804 surface_from_global_float(es, rectangles[i].x2,
805 rectangles[i].y1, &sx, &sy);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500806 v[ 8] = rectangles[i].x2;
807 v[ 9] = rectangles[i].y1;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200808 v[10] = sx * inv_width;
809 v[11] = sy * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500810
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200811 surface_from_global_float(es, rectangles[i].x2,
812 rectangles[i].y2, &sx, &sy);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500813 v[12] = rectangles[i].x2;
814 v[13] = rectangles[i].y2;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200815 v[14] = sx * inv_width;
816 v[15] = sy * inv_height;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500817
818 p[0] = i * 4 + 0;
819 p[1] = i * 4 + 1;
820 p[2] = i * 4 + 2;
821 p[3] = i * 4 + 2;
822 p[4] = i * 4 + 1;
823 p[5] = i * 4 + 3;
824 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500825
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400826 return n;
827}
828
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500829WL_EXPORT void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500830weston_surface_draw(struct weston_surface *es, struct weston_output *output,
831 pixman_region32_t *damage)
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400832{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500833 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400834 GLfloat *v;
835 pixman_region32_t repaint;
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400836 GLint filter;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400837 int n;
838
Pekka Paalanena9f8a212012-01-26 11:28:08 +0200839 pixman_region32_init(&repaint);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500840 pixman_region32_intersect(&repaint,
841 &es->transform.boundingbox, damage);
842 pixman_region32_subtract(&repaint, &repaint, &es->clip);
Pekka Paalanenc7b45f62012-02-06 12:16:07 +0200843
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400844 if (!pixman_region32_not_empty(&repaint))
Pekka Paalanena9f8a212012-01-26 11:28:08 +0200845 goto out;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400846
Kristian Høgsberg101cb652012-02-17 10:45:16 -0500847 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
848 glEnable(GL_BLEND);
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400849
Kristian Høgsberg07632622012-01-25 23:02:06 -0500850 if (ec->current_shader != es->shader) {
851 glUseProgram(es->shader->program);
Kristian Høgsberg07632622012-01-25 23:02:06 -0500852 ec->current_shader = es->shader;
853 }
854
Kristian Høgsberg0452abc2012-01-31 15:38:36 -0500855 glUniformMatrix4fv(es->shader->proj_uniform,
856 1, GL_FALSE, output->matrix.d);
Pekka Paalanenf55f5442012-02-02 16:49:05 +0200857 glUniform1i(es->shader->tex_uniform, 0);
858 glUniform4fv(es->shader->color_uniform, 1, es->color);
859 glUniform1f(es->shader->alpha_uniform, es->alpha / 255.0);
Scott Moreau69367082012-04-17 19:06:19 -0600860 glUniform1f(es->shader->brightness_uniform, es->brightness / 255.0);
861 glUniform1f(es->shader->saturation_uniform, es->saturation / 255.0);
Pekka Paalanenf55f5442012-02-02 16:49:05 +0200862 glUniform1f(es->shader->texwidth_uniform,
863 (GLfloat)es->geometry.width / es->pitch);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200864
Scott Moreauccbf29d2012-02-22 14:21:41 -0700865 if (es->transform.enabled || output->zoom.active)
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400866 filter = GL_LINEAR;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200867 else
Pekka Paalanenc61eca62012-01-06 14:10:06 +0200868 filter = GL_NEAREST;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200869
870 n = texture_region(es, &repaint);
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400871
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500872 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400873 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
874 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
875
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500876 v = ec->vertices.data;
877 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
878 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400879 glEnableVertexAttribArray(0);
880 glEnableVertexAttribArray(1);
Pekka Paalanen3df327f2012-01-24 15:53:35 +0200881
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500882 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
883
Pekka Paalanen3df327f2012-01-24 15:53:35 +0200884 glDisableVertexAttribArray(1);
885 glDisableVertexAttribArray(0);
886
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500887 ec->vertices.size = 0;
888 ec->indices.size = 0;
Pekka Paalanena9f8a212012-01-26 11:28:08 +0200889
890out:
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500891 pixman_region32_fini(&repaint);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500892}
893
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500894WL_EXPORT void
895weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400896{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500897 wl_list_remove(&surface->layer_link);
898 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500899 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500900 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400901}
902
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400903WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500904weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400905{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500906 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400907
908 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500909 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400910}
911
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500912WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500913weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +0200914{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400915 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200916 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200917
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400918 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -0500919 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200920}
921
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400922WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500923weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400924{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500925 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400926
Kristian Høgsberg944236a2012-02-28 23:07:47 -0500927 pixman_region32_union(&compositor->damage,
928 &compositor->damage, &output->region);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500929 weston_compositor_schedule_repaint(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400930}
931
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400932static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500933fade_frame(struct weston_animation *animation,
934 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400935{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500936 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400937 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500938 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500939 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400940
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500941 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500942 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500943 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
944 compositor->fade.spring.current);
945 weston_surface_damage(surface);
946
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500947 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400948 compositor->fade.spring.current =
949 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400950 wl_list_remove(&animation->link);
951 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200952
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500953 if (compositor->fade.spring.current < 0.001) {
954 destroy_surface(&surface->surface.resource);
955 compositor->fade.surface = NULL;
956 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500957 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400958 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200959 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400960 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400961}
962
Kristian Høgsberg30c018b2012-01-26 08:40:37 -0500963struct weston_frame_callback {
964 struct wl_resource resource;
965 struct wl_list link;
966};
967
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400968static void
Kristian Høgsberg30c018b2012-01-26 08:40:37 -0500969weston_output_repaint(struct weston_output *output, int msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400970{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500971 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500972 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500973 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -0500974 struct weston_animation *animation, *next;
975 struct weston_frame_callback *cb, *cnext;
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500976 pixman_region32_t opaque, new_damage, output_damage;
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500977 int32_t width, height;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500978
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +0200979 weston_compositor_update_drag_surfaces(ec);
980
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500981 width = output->current->width +
982 output->border.left + output->border.right;
983 height = output->current->height +
984 output->border.top + output->border.bottom;
985 glViewport(0, 0, width, height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500986
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500987 /* Rebuild the surface list and update surface transforms up front. */
988 wl_list_init(&ec->surface_list);
989 wl_list_for_each(layer, &ec->layer_list, link) {
990 wl_list_for_each(es, &layer->surface_list, layer_link) {
991 weston_surface_update_transform(es);
992 wl_list_insert(ec->surface_list.prev, &es->link);
993 }
994 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +0200995
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800996 if (output->assign_planes)
997 /*
998 * This will queue flips for the fbs and sprites where
999 * applicable and clear the damage for those surfaces.
1000 * The repaint loop below will repaint everything
1001 * else.
1002 */
1003 output->assign_planes(output);
1004
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001005 pixman_region32_init(&new_damage);
1006 pixman_region32_init(&opaque);
1007
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001008 wl_list_for_each(es, &ec->surface_list, link) {
1009 pixman_region32_subtract(&es->damage, &es->damage, &opaque);
1010 pixman_region32_union(&new_damage, &new_damage, &es->damage);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001011 empty_region(&es->damage);
1012 pixman_region32_copy(&es->clip, &opaque);
Pekka Paalanen730d87e2012-02-09 16:39:38 +02001013 pixman_region32_union(&opaque, &opaque, &es->transform.opaque);
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001014 }
1015
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001016 pixman_region32_union(&ec->damage, &ec->damage, &new_damage);
1017
1018 pixman_region32_init(&output_damage);
1019 pixman_region32_union(&output_damage,
1020 &ec->damage, &output->previous_damage);
1021 pixman_region32_copy(&output->previous_damage, &ec->damage);
1022 pixman_region32_intersect(&output_damage,
1023 &output_damage, &output->region);
1024 pixman_region32_subtract(&ec->damage, &ec->damage, &output->region);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001025
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -04001026 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001027 pixman_region32_fini(&new_damage);
1028
Scott Moreauccbf29d2012-02-22 14:21:41 -07001029 if (output->dirty)
1030 weston_output_update_matrix(output);
1031
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001032 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001033
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001034 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001035
Kristian Høgsbergef044142011-06-21 15:02:12 -04001036 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001037
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001038 weston_compositor_repick(ec);
1039 wl_event_loop_dispatch(ec->input_loop, 0);
1040
Kristian Høgsberg33418202011-08-16 23:01:28 -04001041 wl_list_for_each_safe(cb, cnext, &output->frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001042 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001043 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001044 }
1045
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001046 wl_list_for_each_safe(animation, next, &ec->animation_list, link)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001047 animation->frame(animation, output, msecs);
1048}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001049
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001050static int
1051weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001052{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001053 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001054
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001055 wl_event_loop_dispatch(compositor->input_loop, 0);
1056
1057 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001058}
1059
1060WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001061weston_output_finish_frame(struct weston_output *output, int msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001062{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001063 struct weston_compositor *compositor = output->compositor;
1064 struct wl_event_loop *loop =
1065 wl_display_get_event_loop(compositor->wl_display);
1066 int fd;
1067
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001068 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001069 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001070 return;
1071 }
1072
1073 output->repaint_scheduled = 0;
1074 if (compositor->input_loop_source)
1075 return;
1076
1077 fd = wl_event_loop_get_fd(compositor->input_loop);
1078 compositor->input_loop_source =
1079 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1080 weston_compositor_read_input, compositor);
1081}
1082
1083static void
1084idle_repaint(void *data)
1085{
1086 struct weston_output *output = data;
1087
1088 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001089}
1090
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001091WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001092weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1093{
1094 wl_list_init(&layer->surface_list);
1095 wl_list_insert(below, &layer->link);
1096}
1097
1098WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001099weston_compositor_schedule_repaint(struct weston_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001100{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001101 struct weston_output *output;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001102 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001103
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001104 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001105 return;
1106
Kristian Høgsbergef044142011-06-21 15:02:12 -04001107 loop = wl_display_get_event_loop(compositor->wl_display);
1108 wl_list_for_each(output, &compositor->output_list, link) {
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001109 output->repaint_needed = 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001110 if (output->repaint_scheduled)
1111 continue;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001112
Kristian Høgsbergef044142011-06-21 15:02:12 -04001113 wl_event_loop_add_idle(loop, idle_repaint, output);
1114 output->repaint_scheduled = 1;
1115 }
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001116
1117 if (compositor->input_loop_source) {
1118 wl_event_source_remove(compositor->input_loop_source);
1119 compositor->input_loop_source = NULL;
1120 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001121}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001122
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001123WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001124weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001125{
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001126 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001127 int done;
1128
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001129 done = weston_spring_done(&compositor->fade.spring);
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001130 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001131 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001132 return;
1133
1134 if (done)
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001135 compositor->fade.spring.timestamp =
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001136 weston_compositor_get_time();
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001137
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001138 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001139 surface = weston_surface_create(compositor);
1140 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001141 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001142 wl_list_insert(&compositor->fade_layer.surface_list,
1143 &surface->layer_link);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001144 weston_surface_assign_output(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001145 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001146 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001147 }
1148
1149 weston_surface_damage(compositor->fade.surface);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001150 if (wl_list_empty(&compositor->fade.animation.link))
1151 wl_list_insert(compositor->animation_list.prev,
1152 &compositor->fade.animation.link);
1153}
1154
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001155static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001156surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001157{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001158 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001159}
1160
Casey Dahlin96d8a752012-04-19 22:50:07 -04001161static struct wl_resource *
1162find_resource_for_client(struct wl_list *list, struct wl_client *client)
1163{
1164 struct wl_resource *r;
1165
1166 wl_list_for_each(r, list, link) {
1167 if (r->client == client)
1168 return r;
1169 }
1170
1171 return NULL;
1172}
1173
Casey Dahlin9074db52012-04-19 22:50:09 -04001174static void
1175weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1176{
1177 uint32_t different = es->output_mask ^ mask;
1178 uint32_t entered = mask & different;
1179 uint32_t left = es->output_mask & different;
1180 struct weston_output *output;
1181 struct wl_resource *resource;
1182 struct wl_client *client = es->surface.resource.client;
1183
1184 if (es->surface.resource.client == NULL)
1185 return;
1186 if (different == 0)
1187 return;
1188
1189 es->output_mask = mask;
1190 wl_list_for_each(output, &es->compositor->output_list, link) {
1191 if (1 << output->id & different)
1192 resource =
1193 find_resource_for_client(&output->resource_list,
1194 client);
1195 if (1 << output->id & entered)
1196 wl_surface_send_enter(&es->surface.resource, resource);
1197 if (1 << output->id & left)
1198 wl_surface_send_leave(&es->surface.resource, resource);
1199 }
1200}
1201
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001202WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001203weston_surface_assign_output(struct weston_surface *es)
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001204{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001205 struct weston_compositor *ec = es->compositor;
1206 struct weston_output *output, *new_output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001207 pixman_region32_t region;
Casey Dahlin9074db52012-04-19 22:50:09 -04001208 uint32_t max, area, mask;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001209 pixman_box32_t *e;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001210
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001211 new_output = NULL;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001212 max = 0;
Casey Dahlin9074db52012-04-19 22:50:09 -04001213 mask = 0;
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001214 pixman_region32_init(&region);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001215 wl_list_for_each(output, &ec->output_list, link) {
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001216 pixman_region32_intersect(&region, &es->transform.boundingbox,
1217 &output->region);
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001218
1219 e = pixman_region32_extents(&region);
1220 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1221
Casey Dahlin9074db52012-04-19 22:50:09 -04001222 if (area > 0)
1223 mask |= 1 << output->id;
1224
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001225 if (area >= max) {
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001226 new_output = output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001227 max = area;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001228 }
1229 }
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001230 pixman_region32_fini(&region);
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001231
1232 es->output = new_output;
Casey Dahlin9074db52012-04-19 22:50:09 -04001233 weston_surface_update_output_mask(es, mask);
1234
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001235 if (!wl_list_empty(&es->frame_callback_list)) {
1236 wl_list_insert_list(new_output->frame_callback_list.prev,
1237 &es->frame_callback_list);
1238 wl_list_init(&es->frame_callback_list);
1239 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001240}
1241
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001242static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001243surface_attach(struct wl_client *client,
1244 struct wl_resource *resource,
1245 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1246{
1247 struct weston_surface *es = resource->data;
1248 struct wl_buffer *buffer = NULL;
1249
1250 if (buffer_resource)
1251 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001252
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -04001253 weston_surface_attach(&es->surface, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001254
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001255 if (buffer && es->configure)
1256 es->configure(es, sx, sy);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001257}
1258
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001259static void
Pekka Paalanen52bfbaa2012-04-11 16:19:37 +03001260texture_set_subimage(struct weston_surface *surface,
1261 int32_t x, int32_t y, int32_t width, int32_t height)
1262{
1263 glBindTexture(GL_TEXTURE_2D, surface->texture);
1264
1265#ifdef GL_UNPACK_ROW_LENGTH
1266 /* Mesa does not define GL_EXT_unpack_subimage */
1267
1268 if (surface->compositor->has_unpack_subimage) {
1269 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch);
1270 glPixelStorei(GL_UNPACK_SKIP_PIXELS, x);
1271 glPixelStorei(GL_UNPACK_SKIP_ROWS, y);
1272
1273 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height,
1274 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1275 wl_shm_buffer_get_data(surface->buffer));
1276 return;
1277 }
1278#endif
1279
1280 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1281 surface->pitch, surface->buffer->height, 0,
1282 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1283 wl_shm_buffer_get_data(surface->buffer));
1284}
1285
1286static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001287surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001288 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001289 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001290{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001291 struct weston_surface *es = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001292
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001293 weston_surface_damage_rectangle(es, x, y, width, height);
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04001294
Pekka Paalanen52bfbaa2012-04-11 16:19:37 +03001295 if (es->buffer && wl_buffer_is_shm(es->buffer))
1296 texture_set_subimage(es, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001297}
1298
Kristian Høgsberg33418202011-08-16 23:01:28 -04001299static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001300destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001301{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001302 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001303
1304 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001305 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001306}
1307
1308static void
1309surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001310 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001311{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001312 struct weston_frame_callback *cb;
1313 struct weston_surface *es = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001314
1315 cb = malloc(sizeof *cb);
1316 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001317 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001318 return;
1319 }
1320
1321 cb->resource.object.interface = &wl_callback_interface;
1322 cb->resource.object.id = callback;
1323 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001324 cb->resource.client = client;
1325 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001326
1327 wl_client_add_resource(client, &cb->resource);
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -04001328
1329 if (es->output) {
1330 wl_list_insert(es->output->frame_callback_list.prev,
1331 &cb->link);
1332 } else {
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001333 wl_list_insert(es->frame_callback_list.prev, &cb->link);
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -04001334 }
Kristian Høgsberg33418202011-08-16 23:01:28 -04001335}
1336
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001337static void
1338surface_set_opaque_region(struct wl_client *client,
1339 struct wl_resource *resource,
1340 struct wl_resource *region_resource)
1341{
1342 struct weston_surface *surface = resource->data;
1343 struct weston_region *region;
1344
1345 pixman_region32_fini(&surface->opaque);
1346
1347 if (region_resource) {
1348 region = region_resource->data;
1349 pixman_region32_init_rect(&surface->opaque, 0, 0,
1350 surface->geometry.width,
1351 surface->geometry.height);
1352 pixman_region32_intersect(&surface->opaque,
1353 &surface->opaque, &region->region);
1354 } else {
1355 pixman_region32_init(&surface->opaque);
1356 }
1357
1358 surface->geometry.dirty = 1;
1359}
1360
1361static void
1362surface_set_input_region(struct wl_client *client,
1363 struct wl_resource *resource,
1364 struct wl_resource *region_resource)
1365{
1366 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001367 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001368
1369 if (region_resource) {
1370 region = region_resource->data;
1371 pixman_region32_init_rect(&surface->input, 0, 0,
1372 surface->geometry.width,
1373 surface->geometry.height);
1374 pixman_region32_intersect(&surface->input,
1375 &surface->input, &region->region);
1376 } else {
1377 pixman_region32_init_rect(&surface->input, 0, 0,
1378 surface->geometry.width,
1379 surface->geometry.height);
1380 }
1381
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001382 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001383}
1384
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001385static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001386 surface_destroy,
1387 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001388 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001389 surface_frame,
1390 surface_set_opaque_region,
1391 surface_set_input_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001392};
1393
1394static void
1395compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001396 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001397{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001398 struct weston_compositor *ec = resource->data;
1399 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001400
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001401 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001402 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001403 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001404 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001405 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001406
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001407 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001408
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001409 surface->surface.resource.object.id = id;
1410 surface->surface.resource.object.interface = &wl_surface_interface;
1411 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001412 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001413 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001414
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001415 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001416}
1417
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001418static void
1419destroy_region(struct wl_resource *resource)
1420{
1421 struct weston_region *region =
1422 container_of(resource, struct weston_region, resource);
1423
1424 pixman_region32_fini(&region->region);
1425 free(region);
1426}
1427
1428static void
1429region_destroy(struct wl_client *client, struct wl_resource *resource)
1430{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001431 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001432}
1433
1434static void
1435region_add(struct wl_client *client, struct wl_resource *resource,
1436 int32_t x, int32_t y, int32_t width, int32_t height)
1437{
1438 struct weston_region *region = resource->data;
1439
1440 pixman_region32_union_rect(&region->region, &region->region,
1441 x, y, width, height);
1442}
1443
1444static void
1445region_subtract(struct wl_client *client, struct wl_resource *resource,
1446 int32_t x, int32_t y, int32_t width, int32_t height)
1447{
1448 struct weston_region *region = resource->data;
1449 pixman_region32_t rect;
1450
1451 pixman_region32_init_rect(&rect, x, y, width, height);
1452 pixman_region32_subtract(&region->region, &region->region, &rect);
1453 pixman_region32_fini(&rect);
1454}
1455
1456static const struct wl_region_interface region_interface = {
1457 region_destroy,
1458 region_add,
1459 region_subtract
1460};
1461
1462static void
1463compositor_create_region(struct wl_client *client,
1464 struct wl_resource *resource, uint32_t id)
1465{
1466 struct weston_region *region;
1467
1468 region = malloc(sizeof *region);
1469 if (region == NULL) {
1470 wl_resource_post_no_memory(resource);
1471 return;
1472 }
1473
1474 region->resource.destroy = destroy_region;
1475
1476 region->resource.object.id = id;
1477 region->resource.object.interface = &wl_region_interface;
1478 region->resource.object.implementation =
1479 (void (**)(void)) &region_interface;
1480 region->resource.data = region;
1481
1482 pixman_region32_init(&region->region);
1483
1484 wl_client_add_resource(client, &region->resource);
1485}
1486
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001487static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001488 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001489 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001490};
1491
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001492WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001493weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001494{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001495 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1496 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001497
1498 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001499 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001500}
1501
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001502static void
1503weston_compositor_dpms_on(struct weston_compositor *compositor)
1504{
1505 struct weston_output *output;
1506
1507 wl_list_for_each(output, &compositor->output_list, link)
1508 if (output->set_dpms)
1509 output->set_dpms(output, WESTON_DPMS_ON);
1510}
1511
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001512WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001513weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001514{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001515 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1516 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001517 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001518 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001519 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001520 }
1521}
1522
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001523static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001524weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001525{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001526 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001527 compositor->idle_inhibit++;
1528}
1529
1530static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001531weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001532{
1533 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001534 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001535}
1536
1537static int
1538idle_handler(void *data)
1539{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001540 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001541
1542 if (compositor->idle_inhibit)
1543 return 1;
1544
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001545 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001546
1547 return 1;
1548}
1549
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001550static void
Daniel Stone37816df2012-05-16 18:45:18 +01001551weston_seat_update_drag_surface(struct wl_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001552
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001553static void
Daniel Stone37816df2012-05-16 18:45:18 +01001554clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001555{
Daniel Stone37816df2012-05-16 18:45:18 +01001556 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001557 struct weston_output *output, *prev = NULL;
1558 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001559
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001560 x = wl_fixed_to_int(*fx);
1561 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001562 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1563 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001564
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001565 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001566 if (pixman_region32_contains_point(&output->region,
1567 x, y, NULL))
1568 valid = 1;
1569 if (pixman_region32_contains_point(&output->region,
1570 old_x, old_y, NULL))
1571 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001572 }
Daniel Stone37816df2012-05-16 18:45:18 +01001573
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001574 if (!valid) {
1575 if (x < prev->x)
1576 *fx = wl_fixed_from_int(prev->x);
1577 else if (x >= prev->x + prev->current->width)
1578 *fx = wl_fixed_from_int(prev->x +
1579 prev->current->width - 1);
1580 if (y < prev->y)
1581 *fy = wl_fixed_from_int(prev->y);
1582 else if (y >= prev->y + prev->current->height)
1583 *fy = wl_fixed_from_int(prev->y +
1584 prev->current->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001585 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001586}
1587
1588WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01001589notify_motion(struct wl_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001590{
1591 const struct wl_pointer_grab_interface *interface;
Daniel Stone37816df2012-05-16 18:45:18 +01001592 struct weston_seat *ws = (struct weston_seat *) seat;
1593 struct weston_compositor *ec = ws->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001594 struct weston_output *output;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001595 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001596
1597 weston_compositor_activity(ec);
1598
Daniel Stone37816df2012-05-16 18:45:18 +01001599 clip_pointer_motion(ws, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001600
Daniel Stone37816df2012-05-16 18:45:18 +01001601 weston_seat_update_drag_surface(seat,
1602 x - seat->pointer->x,
1603 y - seat->pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001604
Daniel Stone37816df2012-05-16 18:45:18 +01001605 seat->pointer->x = x;
1606 seat->pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001607
1608 ix = wl_fixed_to_int(x);
1609 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001610
Scott Moreauccbf29d2012-02-22 14:21:41 -07001611 wl_list_for_each(output, &ec->output_list, link)
1612 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001613 pixman_region32_contains_point(&output->region,
1614 ix, iy, NULL))
Scott Moreauccbf29d2012-02-22 14:21:41 -07001615 weston_output_update_zoom(output, x, y);
1616
Daniel Stone37816df2012-05-16 18:45:18 +01001617 weston_device_repick(seat);
1618 interface = seat->pointer->grab->interface;
1619 interface->motion(seat->pointer->grab, time,
1620 seat->pointer->grab->x, seat->pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001621
Daniel Stone37816df2012-05-16 18:45:18 +01001622 if (ws->sprite) {
1623 weston_surface_set_position(ws->sprite,
1624 ix - ws->hotspot_x,
1625 iy - ws->hotspot_y);
Pekka Paalanenf8c6aae2012-02-13 11:01:59 +02001626 weston_compositor_schedule_repaint(ec);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001627 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001628}
1629
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001630WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001631weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001632 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001633{
Daniel Stone37816df2012-05-16 18:45:18 +01001634 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001635
Daniel Stone37816df2012-05-16 18:45:18 +01001636 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1637 wl_data_device_set_keyboard_focus(&seat->seat);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001638
1639 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001640}
1641
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001642WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01001643notify_button(struct wl_seat *seat, uint32_t time, int32_t button,
1644 uint32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001645{
Daniel Stone37816df2012-05-16 18:45:18 +01001646 struct weston_seat *ws = (struct weston_seat *) seat;
1647 struct weston_compositor *compositor = ws->compositor;
1648 struct weston_surface *focus =
1649 (struct weston_surface *) seat->pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001650 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001651
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001652 if (state) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001653 if (compositor->ping_handler && focus)
1654 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001655 weston_compositor_idle_inhibit(compositor);
Daniel Stone37816df2012-05-16 18:45:18 +01001656 if (seat->pointer->button_count == 0) {
1657 seat->pointer->grab_button = button;
1658 seat->pointer->grab_time = time;
1659 seat->pointer->grab_x = seat->pointer->x;
1660 seat->pointer->grab_y = seat->pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001661 }
Daniel Stone37816df2012-05-16 18:45:18 +01001662 seat->pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001663 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001664 weston_compositor_idle_release(compositor);
Daniel Stone37816df2012-05-16 18:45:18 +01001665 seat->pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001666 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001667
Daniel Stone37816df2012-05-16 18:45:18 +01001668 weston_compositor_run_binding(compositor, ws, time, 0, button, 0,
1669 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001670
Daniel Stone37816df2012-05-16 18:45:18 +01001671 seat->pointer->grab->interface->button(seat->pointer->grab, time,
1672 button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001673
Daniel Stone37816df2012-05-16 18:45:18 +01001674 if (seat->pointer->button_count == 1)
1675 seat->pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001676 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001677}
1678
Scott Moreau210d0792012-03-22 10:47:01 -06001679WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01001680notify_axis(struct wl_seat *seat, uint32_t time, uint32_t axis, int32_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001681{
Daniel Stone37816df2012-05-16 18:45:18 +01001682 struct weston_seat *ws = (struct weston_seat *) seat;
1683 struct weston_compositor *compositor = ws->compositor;
1684 struct weston_surface *focus =
1685 (struct weston_surface *) seat->pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001686 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1687
1688 if (compositor->ping_handler && focus)
1689 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001690
1691 weston_compositor_activity(compositor);
1692
Scott Moreau6a3633d2012-03-20 08:47:59 -06001693 if (value)
Daniel Stone37816df2012-05-16 18:45:18 +01001694 weston_compositor_run_binding(compositor, ws,
1695 time, 0, 0, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06001696 else
1697 return;
1698
Daniel Stone37816df2012-05-16 18:45:18 +01001699 if (seat->pointer->focus_resource)
1700 wl_resource_post_event(seat->pointer->focus_resource,
1701 WL_POINTER_AXIS, time, axis, value);
Scott Moreau210d0792012-03-22 10:47:01 -06001702}
1703
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001704static void
Daniel Stone37816df2012-05-16 18:45:18 +01001705update_modifier_state(struct weston_seat *seat, uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001706{
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001707 uint32_t modifier;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001708
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001709 switch (key) {
1710 case KEY_LEFTCTRL:
1711 case KEY_RIGHTCTRL:
1712 modifier = MODIFIER_CTRL;
1713 break;
1714
1715 case KEY_LEFTALT:
1716 case KEY_RIGHTALT:
1717 modifier = MODIFIER_ALT;
1718 break;
1719
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -04001720 case KEY_LEFTMETA:
1721 case KEY_RIGHTMETA:
1722 modifier = MODIFIER_SUPER;
1723 break;
1724
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001725 default:
1726 modifier = 0;
1727 break;
1728 }
1729
1730 if (state)
Daniel Stone37816df2012-05-16 18:45:18 +01001731 seat->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001732 else
Daniel Stone37816df2012-05-16 18:45:18 +01001733 seat->modifier_state &= ~modifier;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001734}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001735
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001736WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01001737notify_key(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001738{
Daniel Stone37816df2012-05-16 18:45:18 +01001739 struct weston_seat *ws = (struct weston_seat *) seat;
1740 struct weston_compositor *compositor = ws->compositor;
1741 struct weston_surface *focus =
1742 (struct weston_surface *) seat->pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001743 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001744 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001745
Scott Moreauec286eb2012-02-18 05:05:30 -07001746 if (state) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001747 if (compositor->ping_handler && focus)
1748 compositor->ping_handler(focus, serial);
1749
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001750 weston_compositor_idle_inhibit(compositor);
Daniel Stone37816df2012-05-16 18:45:18 +01001751 seat->keyboard->grab_key = key;
1752 seat->keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07001753 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001754 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001755 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001756
Daniel Stone37816df2012-05-16 18:45:18 +01001757 update_modifier_state(ws, key, state);
1758 end = seat->keyboard->keys.data + seat->keyboard->keys.size;
1759 for (k = seat->keyboard->keys.data; k < end; k++) {
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001760 if (*k == key)
1761 *k = *--end;
1762 }
Daniel Stone37816df2012-05-16 18:45:18 +01001763 seat->keyboard->keys.size = (void *) end - seat->keyboard->keys.data;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001764 if (state) {
Daniel Stone37816df2012-05-16 18:45:18 +01001765 k = wl_array_add(&seat->keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001766 *k = key;
1767 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001768
Daniel Stone37816df2012-05-16 18:45:18 +01001769 if (seat->keyboard->grab == &seat->keyboard->default_grab)
1770 weston_compositor_run_binding(compositor, ws,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001771 time, key, 0, 0, state);
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001772
Daniel Stone37816df2012-05-16 18:45:18 +01001773 seat->keyboard->grab->interface->key(seat->keyboard->grab,
1774 time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001775}
1776
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001777WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01001778notify_pointer_focus(struct wl_seat *seat, struct weston_output *output,
1779 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001780{
Daniel Stone37816df2012-05-16 18:45:18 +01001781 struct weston_seat *ws = (struct weston_seat *) seat;
1782 struct weston_compositor *compositor = ws->compositor;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001783
1784 if (output) {
Daniel Stone37816df2012-05-16 18:45:18 +01001785 weston_seat_update_drag_surface(seat,
1786 x - seat->pointer->x,
1787 y - seat->pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001788
Daniel Stone37816df2012-05-16 18:45:18 +01001789 seat->pointer->x = x;
1790 seat->pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001791 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001792 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001793 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001794 compositor->focus = 0;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001795 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001796 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001797}
1798
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001799static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001800destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001801{
Daniel Stone37816df2012-05-16 18:45:18 +01001802 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001803
Daniel Stone37816df2012-05-16 18:45:18 +01001804 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001805 saved_kbd_focus_listener);
1806
Daniel Stone37816df2012-05-16 18:45:18 +01001807 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001808}
1809
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001810WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01001811notify_keyboard_focus(struct wl_seat *seat, struct wl_array *keys)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001812{
Daniel Stone37816df2012-05-16 18:45:18 +01001813 struct weston_seat *ws = (struct weston_seat *) seat;
1814 struct weston_compositor *compositor = ws->compositor;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001815 struct wl_surface *surface;
Kristian Høgsbergd3c02752012-03-04 22:35:47 -05001816 uint32_t *k;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001817
Kristian Høgsberg31dd6b82012-04-09 22:10:00 -04001818 if (keys) {
Daniel Stone37816df2012-05-16 18:45:18 +01001819 wl_array_copy(&seat->keyboard->keys, keys);
1820 ws->modifier_state = 0;
1821 wl_array_for_each(k, &seat->keyboard->keys) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001822 weston_compositor_idle_inhibit(compositor);
Daniel Stone37816df2012-05-16 18:45:18 +01001823 update_modifier_state(ws, *k, 1);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001824 }
1825
Daniel Stone37816df2012-05-16 18:45:18 +01001826 surface = ws->saved_kbd_focus;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001827
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001828 if (surface) {
Daniel Stone37816df2012-05-16 18:45:18 +01001829 wl_list_remove(&ws->saved_kbd_focus_listener.link);
1830 wl_keyboard_set_focus(ws->seat.keyboard, surface);
1831 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001832 }
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001833 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001834 wl_array_for_each(k, &seat->keyboard->keys)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001835 weston_compositor_idle_release(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001836
Daniel Stone37816df2012-05-16 18:45:18 +01001837 ws->modifier_state = 0;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001838
Daniel Stone37816df2012-05-16 18:45:18 +01001839 surface = ws->seat.keyboard->focus;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001840
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001841 if (surface) {
Daniel Stone37816df2012-05-16 18:45:18 +01001842 ws->saved_kbd_focus = surface;
1843 ws->saved_kbd_focus_listener.notify =
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001844 destroy_device_saved_kbd_focus;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001845 wl_signal_add(&surface->resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01001846 &ws->saved_kbd_focus_listener);
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001847 }
1848
Daniel Stone37816df2012-05-16 18:45:18 +01001849 wl_keyboard_set_focus(ws->seat.keyboard, NULL);
Kristian Høgsberg035dd9c2012-04-10 00:33:40 -04001850 /* FIXME: We really need keyboard grab cancel here to
1851 * let the grab shut down properly. As it is we leak
1852 * the grab data. */
Daniel Stone37816df2012-05-16 18:45:18 +01001853 wl_keyboard_end_grab(ws->seat.keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001854 }
1855}
1856
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001857static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001858lose_touch_focus_resource(struct wl_listener *listener, void *data)
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001859{
Daniel Stone37816df2012-05-16 18:45:18 +01001860 struct weston_seat *seat = container_of(listener, struct weston_seat,
1861 touch_focus_resource_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001862
Daniel Stone37816df2012-05-16 18:45:18 +01001863 seat->touch_focus_resource = NULL;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001864}
1865
1866static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001867lose_touch_focus(struct wl_listener *listener, void *data)
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001868{
Daniel Stone37816df2012-05-16 18:45:18 +01001869 struct weston_seat *seat = container_of(listener, struct weston_seat,
1870 touch_focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001871
Daniel Stone37816df2012-05-16 18:45:18 +01001872 seat->touch_focus = NULL;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001873}
1874
1875static void
Daniel Stone37816df2012-05-16 18:45:18 +01001876touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001877{
Daniel Stone37816df2012-05-16 18:45:18 +01001878 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001879 struct wl_resource *resource;
1880
Daniel Stone37816df2012-05-16 18:45:18 +01001881 if (ws->touch_focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001882 return;
1883
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001884 if (surface) {
1885 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01001886 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04001887 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001888 if (!resource) {
1889 fprintf(stderr, "couldn't find resource\n");
1890 return;
1891 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001892
Daniel Stone37816df2012-05-16 18:45:18 +01001893 ws->touch_focus_resource_listener.notify =
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001894 lose_touch_focus_resource;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001895 wl_signal_add(&resource->destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01001896 &ws->touch_focus_resource_listener);
1897 ws->touch_focus_listener.notify = lose_touch_focus;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001898 wl_signal_add(&surface->resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01001899 &ws->touch_focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001900
Daniel Stone37816df2012-05-16 18:45:18 +01001901 seat->touch->focus = surface;
1902 seat->touch->focus_resource = resource;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001903 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001904 if (seat->touch->focus)
1905 wl_list_remove(&ws->touch_focus_listener.link);
1906 if (seat->touch->focus_resource)
1907 wl_list_remove(&ws->touch_focus_resource_listener.link);
1908 seat->touch->focus = NULL;
1909 seat->touch->focus_resource = NULL;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001910 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001911}
1912
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001913/**
1914 * notify_touch - emulates button touches and notifies surfaces accordingly.
1915 *
1916 * It assumes always the correct cycle sequence until it gets here: touch_down
1917 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1918 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001919 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001920 */
1921WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01001922notify_touch(struct wl_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001923 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001924{
Daniel Stone37816df2012-05-16 18:45:18 +01001925 struct weston_seat *ws = (struct weston_seat *) seat;
1926 struct weston_compositor *ec = ws->compositor;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001927 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001928 wl_fixed_t sx, sy;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001929 uint32_t serial = 0;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001930
1931 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01001932 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001933 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001934
Daniel Stone37816df2012-05-16 18:45:18 +01001935 ws->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001936
1937 /* the first finger down picks the surface, and all further go
1938 * to that surface for the remainder of the touch session i.e.
1939 * until all touch points are up again. */
Daniel Stone37816df2012-05-16 18:45:18 +01001940 if (ws->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001941 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Daniel Stone37816df2012-05-16 18:45:18 +01001942 touch_set_focus(ws, &es->surface);
1943 } else if (ws->touch_focus) {
1944 es = (struct weston_surface *) ws->touch_focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001945 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001946 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001947
Daniel Stone37816df2012-05-16 18:45:18 +01001948 if (ws->touch_focus_resource && ws->touch_focus)
1949 wl_touch_send_down(ws->touch_focus_resource,
1950 serial, time,
1951 &ws->touch_focus->resource,
1952 touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001953 break;
Daniel Stone37816df2012-05-16 18:45:18 +01001954 case WL_TOUCH_MOTION:
1955 es = (struct weston_surface *) ws->touch_focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001956 if (!es)
1957 break;
1958
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001959 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Daniel Stone37816df2012-05-16 18:45:18 +01001960 if (ws->touch_focus_resource)
1961 wl_touch_send_motion(ws->touch_focus_resource,
1962 time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001963 break;
Daniel Stone37816df2012-05-16 18:45:18 +01001964 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001965 weston_compositor_idle_release(ec);
Daniel Stone37816df2012-05-16 18:45:18 +01001966 ws->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001967
Daniel Stone37816df2012-05-16 18:45:18 +01001968 if (ws->touch_focus_resource)
1969 wl_touch_send_up(ws->touch_focus_resource,
1970 serial, time, touch_id);
1971 if (ws->num_tp == 0)
1972 touch_set_focus(ws, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001973 break;
1974 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001975}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001976
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001977static void
Daniel Stone37816df2012-05-16 18:45:18 +01001978pointer_attach(struct wl_client *client, struct wl_resource *resource,
1979 uint32_t serial, struct wl_resource *buffer_resource,
1980 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001981{
Daniel Stone37816df2012-05-16 18:45:18 +01001982 struct weston_seat *seat = resource->data;
1983 struct weston_compositor *compositor = seat->compositor;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001984 struct wl_buffer *buffer = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001985
Daniel Stone37816df2012-05-16 18:45:18 +01001986 if (serial < seat->seat.pointer->focus_serial)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001987 return;
Daniel Stone37816df2012-05-16 18:45:18 +01001988 if (seat->seat.pointer->focus == NULL)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001989 return;
Daniel Stone37816df2012-05-16 18:45:18 +01001990 if (seat->seat.pointer->focus->resource.client != client)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001991 return;
1992
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001993 if (buffer_resource)
1994 buffer = buffer_resource->data;
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04001995
Daniel Stone37816df2012-05-16 18:45:18 +01001996 weston_surface_attach(&seat->sprite->surface, buffer);
1997 empty_region(&seat->sprite->input);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001998
1999 if (!buffer)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002000 return;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002001
Daniel Stone37816df2012-05-16 18:45:18 +01002002 if (!weston_surface_is_mapped(seat->sprite)) {
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002003 wl_list_insert(&compositor->cursor_layer.surface_list,
Daniel Stone37816df2012-05-16 18:45:18 +01002004 &seat->sprite->layer_link);
2005 weston_surface_assign_output(seat->sprite);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002006 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002007
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002008
Daniel Stone37816df2012-05-16 18:45:18 +01002009 seat->hotspot_x = x;
2010 seat->hotspot_y = y;
2011 weston_surface_configure(seat->sprite,
2012 wl_fixed_to_int(seat->seat.pointer->x) - x,
2013 wl_fixed_to_int(seat->seat.pointer->y) - y,
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002014 buffer->width, buffer->height);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002015
Daniel Stone37816df2012-05-16 18:45:18 +01002016 surface_damage(NULL, &seat->sprite->surface.resource,
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002017 0, 0, buffer->width, buffer->height);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002018}
2019
Daniel Stone37816df2012-05-16 18:45:18 +01002020static const struct wl_pointer_interface pointer_interface = {
2021 pointer_attach,
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002022};
2023
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002024static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002025handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002026{
Daniel Stone37816df2012-05-16 18:45:18 +01002027 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002028
Daniel Stone37816df2012-05-16 18:45:18 +01002029 seat = container_of(listener, struct weston_seat,
2030 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002031
Daniel Stone37816df2012-05-16 18:45:18 +01002032 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002033}
2034
Casey Dahlin9074db52012-04-19 22:50:09 -04002035static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002036{
2037 wl_list_remove(&resource->link);
2038 free(resource);
2039}
2040
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002041static void
Daniel Stone37816df2012-05-16 18:45:18 +01002042seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2043 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002044{
Daniel Stone37816df2012-05-16 18:45:18 +01002045 struct weston_seat *seat = resource->data;
2046 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002047
Daniel Stone37816df2012-05-16 18:45:18 +01002048 if (!seat->seat.pointer)
2049 return;
2050
2051 cr = wl_client_add_object(client, &wl_pointer_interface,
2052 &pointer_interface, id, seat);
2053 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
2054}
2055
2056static void
2057seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2058 uint32_t id)
2059{
2060 struct weston_seat *seat = resource->data;
2061 struct wl_resource *cr;
2062
2063 if (!seat->seat.keyboard)
2064 return;
2065
2066 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2067 seat);
2068 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
2069}
2070
2071static void
2072seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2073 uint32_t id)
2074{
2075 struct weston_seat *seat = resource->data;
2076 struct wl_resource *cr;
2077
2078 if (!seat->seat.touch)
2079 return;
2080
2081 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2082 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
2083}
2084
2085static const struct wl_seat_interface seat_interface = {
2086 seat_get_pointer,
2087 seat_get_keyboard,
2088 seat_get_touch,
2089};
2090
2091static void
2092bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2093{
2094 struct wl_seat *seat = data;
2095 struct wl_resource *resource;
2096 enum wl_seat_capability caps = 0;
2097
2098 resource = wl_client_add_object(client, &wl_seat_interface,
2099 &seat_interface, id, data);
2100 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002101 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002102
2103 if (seat->pointer)
2104 caps |= WL_SEAT_CAPABILITY_POINTER;
2105 if (seat->keyboard)
2106 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2107 if (seat->touch)
2108 caps |= WL_SEAT_CAPABILITY_TOUCH;
2109
2110 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002111}
2112
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002113static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002114device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002115{
Daniel Stone37816df2012-05-16 18:45:18 +01002116 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002117
Daniel Stone37816df2012-05-16 18:45:18 +01002118 seat = container_of(listener, struct weston_seat,
2119 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002120
Daniel Stone37816df2012-05-16 18:45:18 +01002121 weston_seat_update_drag_surface(&seat->seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002122}
2123
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002124WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002125weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002126{
Daniel Stone37816df2012-05-16 18:45:18 +01002127 wl_seat_init(&seat->seat);
2128 wl_pointer_init(&seat->pointer);
2129 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2130 wl_keyboard_init(&seat->keyboard);
2131 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2132 wl_touch_init(&seat->touch);
2133 wl_seat_set_touch(&seat->seat, &seat->touch);
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002134
Daniel Stone37816df2012-05-16 18:45:18 +01002135 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2136 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002137
Daniel Stone37816df2012-05-16 18:45:18 +01002138 seat->sprite = weston_surface_create(ec);
2139 seat->sprite->surface.resource.data = seat->sprite;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002140
Daniel Stone37816df2012-05-16 18:45:18 +01002141 seat->compositor = ec;
2142 seat->hotspot_x = 16;
2143 seat->hotspot_y = 16;
2144 seat->modifier_state = 0;
2145 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002146
Daniel Stone37816df2012-05-16 18:45:18 +01002147 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002148 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002149
Daniel Stone37816df2012-05-16 18:45:18 +01002150 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002151
Daniel Stone37816df2012-05-16 18:45:18 +01002152 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2153 wl_signal_add(&seat->seat.drag_icon_signal,
2154 &seat->new_drag_icon_listener);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002155}
2156
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002157WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002158weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002159{
Daniel Stone37816df2012-05-16 18:45:18 +01002160 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002161 /* The global object is destroyed at wl_display_destroy() time. */
2162
Daniel Stone37816df2012-05-16 18:45:18 +01002163 if (seat->sprite)
2164 destroy_surface(&seat->sprite->surface.resource);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002165
Daniel Stone37816df2012-05-16 18:45:18 +01002166 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002167}
2168
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002169static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002170drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2171{
2172 weston_surface_configure(es,
2173 es->geometry.x + sx, es->geometry.y + sy,
2174 es->buffer->width, es->buffer->height);
2175}
2176
2177static int
Daniel Stone37816df2012-05-16 18:45:18 +01002178device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002179 struct weston_surface *surface)
2180{
Daniel Stone37816df2012-05-16 18:45:18 +01002181 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002182
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002183 if (surface->configure) {
2184 wl_resource_post_error(&surface->surface.resource,
2185 WL_DISPLAY_ERROR_INVALID_OBJECT,
2186 "surface->configure already set");
2187 return 0;
2188 }
2189
Daniel Stone37816df2012-05-16 18:45:18 +01002190 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002191
Daniel Stone37816df2012-05-16 18:45:18 +01002192 weston_surface_set_position(ws->drag_surface,
2193 wl_fixed_to_double(seat->pointer->x),
2194 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002195
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002196 surface->configure = drag_surface_configure;
2197
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002198 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002199 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002200
2201 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002202}
2203
2204static void
Daniel Stone37816df2012-05-16 18:45:18 +01002205device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002206{
Daniel Stone37816df2012-05-16 18:45:18 +01002207 seat->drag_surface->configure = NULL;
2208 undef_region(&seat->drag_surface->input);
2209 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2210 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002211}
2212
2213static void
Daniel Stone37816df2012-05-16 18:45:18 +01002214device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002215{
Daniel Stone37816df2012-05-16 18:45:18 +01002216 if (weston_surface_is_mapped(seat->drag_surface) ||
2217 !seat->drag_surface->buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002218 return;
2219
Daniel Stone37816df2012-05-16 18:45:18 +01002220 wl_list_insert(&seat->sprite->layer_link,
2221 &seat->drag_surface->layer_link);
2222 weston_surface_assign_output(seat->drag_surface);
2223 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002224}
2225
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002226static void
Daniel Stone37816df2012-05-16 18:45:18 +01002227weston_seat_update_drag_surface(struct wl_seat *seat,
2228 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002229{
2230 int surface_changed = 0;
Daniel Stone37816df2012-05-16 18:45:18 +01002231 struct weston_seat *ws = (struct weston_seat *) seat;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002232
Daniel Stone37816df2012-05-16 18:45:18 +01002233 if (!ws->drag_surface && !seat->drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002234 return;
2235
Daniel Stone37816df2012-05-16 18:45:18 +01002236 if (ws->drag_surface && seat->drag_surface &&
2237 (&ws->drag_surface->surface.resource !=
2238 &seat->drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002239 /* between calls to this funcion we got a new drag_surface */
2240 surface_changed = 1;
2241
Daniel Stone37816df2012-05-16 18:45:18 +01002242 if (!seat->drag_surface || surface_changed) {
2243 device_release_drag_surface(ws);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002244 if (!surface_changed)
2245 return;
2246 }
2247
Daniel Stone37816df2012-05-16 18:45:18 +01002248 if (!ws->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002249 struct weston_surface *surface = (struct weston_surface *)
Daniel Stone37816df2012-05-16 18:45:18 +01002250 seat->drag_surface;
2251 if (!device_setup_new_drag_surface(ws, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002252 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002253 }
2254
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002255 /* the client may not have attached a buffer to the drag surface
2256 * when we setup it up, so check if map is needed on every update */
Daniel Stone37816df2012-05-16 18:45:18 +01002257 device_map_drag_surface(ws);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002258
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02002259 /* the client may have attached a buffer with a different size to
2260 * the drag surface, causing the input region to be reset */
Daniel Stone37816df2012-05-16 18:45:18 +01002261 if (region_is_undefined(&ws->drag_surface->input))
2262 empty_region(&ws->drag_surface->input);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02002263
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002264 if (!dx && !dy)
2265 return;
2266
Daniel Stone37816df2012-05-16 18:45:18 +01002267 weston_surface_set_position(ws->drag_surface,
2268 ws->drag_surface->geometry.x + wl_fixed_to_double(dx),
2269 ws->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002270}
2271
2272WL_EXPORT void
2273weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2274{
Daniel Stone37816df2012-05-16 18:45:18 +01002275 weston_seat_update_drag_surface(&compositor->seat->seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002276}
2277
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002278static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002279bind_output(struct wl_client *client,
2280 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002281{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002282 struct weston_output *output = data;
2283 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002284 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002285
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002286 resource = wl_client_add_object(client,
2287 &wl_output_interface, NULL, id, data);
2288
Casey Dahlin9074db52012-04-19 22:50:09 -04002289 wl_list_insert(&output->resource_list, &resource->link);
2290 resource->destroy = unbind_resource;
2291
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002292 wl_output_send_geometry(resource,
2293 output->x,
2294 output->y,
2295 output->mm_width,
2296 output->mm_height,
2297 output->subpixel,
2298 output->make, output->model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002299
2300 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002301 wl_output_send_mode(resource,
2302 mode->flags,
2303 mode->width,
2304 mode->height,
2305 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002306 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002307}
2308
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002309static const char vertex_shader[] =
2310 "uniform mat4 proj;\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05002311 "attribute vec2 position;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002312 "attribute vec2 texcoord;\n"
2313 "varying vec2 v_texcoord;\n"
2314 "void main()\n"
2315 "{\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05002316 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002317 " v_texcoord = texcoord;\n"
2318 "}\n";
2319
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002320static const char texture_fragment_shader[] =
Kristian Høgsbergdfce71d2010-12-07 20:19:10 -05002321 "precision mediump float;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002322 "varying vec2 v_texcoord;\n"
2323 "uniform sampler2D tex;\n"
Kristian Høgsberg541e5552011-12-14 09:24:11 -05002324 "uniform float alpha;\n"
Scott Moreau69367082012-04-17 19:06:19 -06002325 "uniform float bright;\n"
2326 "uniform float saturation;\n"
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02002327 "uniform float texwidth;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002328 "void main()\n"
2329 "{\n"
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02002330 " if (v_texcoord.x < 0.0 || v_texcoord.x > texwidth ||\n"
2331 " v_texcoord.y < 0.0 || v_texcoord.y > 1.0)\n"
2332 " discard;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002333 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
Scott Moreau69367082012-04-17 19:06:19 -06002334 " float gray = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114));\n"
2335 " vec3 range = (gl_FragColor.rgb - vec3 (gray, gray, gray)) * saturation;\n"
2336 " gl_FragColor = vec4(vec3(gray + range), gl_FragColor.a);\n"
2337 " gl_FragColor = vec4(vec3(bright, bright, bright) * gl_FragColor.rgb, gl_FragColor.a);\n"
Kristian Høgsberg541e5552011-12-14 09:24:11 -05002338 " gl_FragColor = alpha * gl_FragColor;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002339 "}\n";
2340
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002341static const char solid_fragment_shader[] =
2342 "precision mediump float;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002343 "uniform vec4 color;\n"
Kristian Høgsbergdbae80e2012-03-29 11:34:39 -04002344 "uniform float alpha;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002345 "void main()\n"
2346 "{\n"
Kristian Høgsbergdbae80e2012-03-29 11:34:39 -04002347 " gl_FragColor = alpha * color\n;"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002348 "}\n";
2349
Kristian Høgsberga9468212010-06-14 21:03:11 -04002350static int
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002351compile_shader(GLenum type, const char *source)
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002352{
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002353 GLuint s;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002354 char msg[512];
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002355 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002356
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002357 s = glCreateShader(type);
2358 glShaderSource(s, 1, &source, NULL);
2359 glCompileShader(s);
2360 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002361 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002362 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
2363 fprintf(stderr, "shader info: %s\n", msg);
2364 return GL_NONE;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002365 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002366
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002367 return s;
2368}
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002369
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002370static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002371weston_shader_init(struct weston_shader *shader,
2372 const char *vertex_source, const char *fragment_source)
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002373{
2374 char msg[512];
2375 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002376
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002377 shader->vertex_shader =
2378 compile_shader(GL_VERTEX_SHADER, vertex_source);
2379 shader->fragment_shader =
2380 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
2381
2382 shader->program = glCreateProgram();
2383 glAttachShader(shader->program, shader->vertex_shader);
2384 glAttachShader(shader->program, shader->fragment_shader);
2385 glBindAttribLocation(shader->program, 0, "position");
2386 glBindAttribLocation(shader->program, 1, "texcoord");
2387
2388 glLinkProgram(shader->program);
2389 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002390 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002391 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002392 fprintf(stderr, "link info: %s\n", msg);
2393 return -1;
2394 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002395
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002396 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
2397 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
Kristian Høgsberg541e5552011-12-14 09:24:11 -05002398 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
Scott Moreau69367082012-04-17 19:06:19 -06002399 shader->brightness_uniform = glGetUniformLocation(shader->program, "bright");
2400 shader->saturation_uniform = glGetUniformLocation(shader->program, "saturation");
Pekka Paalanenf55f5442012-02-02 16:49:05 +02002401 shader->color_uniform = glGetUniformLocation(shader->program, "color");
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02002402 shader->texwidth_uniform = glGetUniformLocation(shader->program,
2403 "texwidth");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002404
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002405 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002406}
2407
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002408WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002409weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002410{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002411 struct weston_compositor *c = output->compositor;
2412
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002413 pixman_region32_fini(&output->region);
Kristian Høgsberg8b72f602011-06-23 20:46:34 -04002414 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002415 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002416
2417 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002418}
2419
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002420WL_EXPORT void
Scott Moreaudc549932012-05-16 08:36:42 -06002421weston_output_update_zoom(struct weston_output *output, wl_fixed_t fx, wl_fixed_t fy)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002422{
2423 float ratio;
Scott Moreaudc549932012-05-16 08:36:42 -06002424 int32_t x, y;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002425
2426 if (output->zoom.level <= 0)
2427 return;
2428
Scott Moreaudc549932012-05-16 08:36:42 -06002429 x = wl_fixed_to_int(fx);
2430 y = wl_fixed_to_int(fy);
2431
Scott Moreauccbf29d2012-02-22 14:21:41 -07002432 output->zoom.magnification = 1 / output->zoom.level;
2433 ratio = 1 - (1 / output->zoom.magnification);
2434
2435 output->zoom.trans_x = (((float)(x - output->x) / output->current->width) * (ratio * 2)) - ratio;
2436 output->zoom.trans_y = (((float)(y - output->y) / output->current->height) * (ratio * 2)) - ratio;
2437
2438 output->dirty = 1;
2439 weston_output_damage(output);
2440}
2441
2442WL_EXPORT void
2443weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002444{
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002445 int flip;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002446 struct weston_matrix camera;
2447 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002448
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002449 weston_matrix_init(&output->matrix);
2450 weston_matrix_translate(&output->matrix,
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002451 -(output->x + (output->border.right + output->current->width - output->border.left) / 2.0),
2452 -(output->y + (output->border.bottom + output->current->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002453
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002454 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002455 weston_matrix_scale(&output->matrix,
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002456 2.0 / (output->current->width + output->border.left + output->border.right),
2457 flip * 2.0 / (output->current->height + output->border.top + output->border.bottom), 1);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002458 if (output->zoom.active) {
2459 weston_matrix_init(&camera);
2460 weston_matrix_init(&modelview);
2461 weston_matrix_translate(&camera, output->zoom.trans_x, flip * output->zoom.trans_y, 0);
2462 weston_matrix_invert(&modelview, &camera);
2463 weston_matrix_scale(&modelview, output->zoom.magnification, output->zoom.magnification, 1.0);
2464 weston_matrix_multiply(&output->matrix, &modelview);
2465 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002466
Scott Moreauccbf29d2012-02-22 14:21:41 -07002467 output->dirty = 0;
2468}
2469
2470WL_EXPORT void
2471weston_output_move(struct weston_output *output, int x, int y)
2472{
2473 output->x = x;
2474 output->y = y;
2475
2476 pixman_region32_init(&output->previous_damage);
2477 pixman_region32_init_rect(&output->region, x, y,
2478 output->current->width,
2479 output->current->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002480}
2481
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002482WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002483weston_output_init(struct weston_output *output, struct weston_compositor *c,
2484 int x, int y, int width, int height, uint32_t flags)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002485{
2486 output->compositor = c;
2487 output->x = x;
2488 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002489 output->border.top = 0;
2490 output->border.bottom = 0;
2491 output->border.left = 0;
2492 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002493 output->mm_width = width;
2494 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002495 output->dirty = 1;
Scott Moreau062be7e2012-04-20 13:37:33 -06002496 wl_list_init(&output->read_pixels_list);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002497
2498 output->zoom.active = 0;
2499 output->zoom.increment = 0.05;
2500 output->zoom.level = 1.0;
2501 output->zoom.magnification = 1.0;
2502 output->zoom.trans_x = 0.0;
2503 output->zoom.trans_y = 0.0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002504
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002505 output->flags = flags;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002506 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002507 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002508
Kristian Høgsberg33418202011-08-16 23:01:28 -04002509 wl_list_init(&output->frame_callback_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002510 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002511
Casey Dahlin58ba1372012-04-19 22:50:08 -04002512 output->id = ffs(~output->compositor->output_id_pool) - 1;
2513 output->compositor->output_id_pool |= 1 << output->id;
2514
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002515 output->global =
2516 wl_display_add_global(c->wl_display, &wl_output_interface,
2517 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002518}
2519
Scott Moreau062be7e2012-04-20 13:37:33 -06002520WL_EXPORT void
2521weston_output_do_read_pixels(struct weston_output *output)
2522{
2523 struct weston_read_pixels *r, *next;
2524
2525 glPixelStorei(GL_PACK_ALIGNMENT, 1);
2526 wl_list_for_each_safe(r, next, &output->read_pixels_list, link) {
2527 glReadPixels(r->x, r->y, r->width, r->height,
2528 output->compositor->read_format,
2529 GL_UNSIGNED_BYTE, r->data);
2530 r->done(r, output);
2531 }
2532}
2533
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002534static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002535compositor_bind(struct wl_client *client,
2536 void *data, uint32_t version, uint32_t id)
2537{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002538 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002539
2540 wl_client_add_object(client, &wl_compositor_interface,
2541 &compositor_interface, id, compositor);
2542}
2543
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002544WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002545weston_compositor_init(struct weston_compositor *ec, struct wl_display *display)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002546{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002547 struct wl_event_loop *loop;
Kristian Høgsbergd711d0c2011-01-14 17:28:21 -05002548 const char *extensions;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002549
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002550 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002551 wl_signal_init(&ec->destroy_signal);
2552 wl_signal_init(&ec->activate_signal);
2553 wl_signal_init(&ec->lock_signal);
2554 wl_signal_init(&ec->unlock_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002555 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002556
Casey Dahlin58ba1372012-04-19 22:50:08 -04002557 ec->output_id_pool = 0;
2558
Kristian Høgsberga8873122011-11-23 10:39:34 -05002559 if (!wl_display_add_global(display, &wl_compositor_interface,
2560 ec, compositor_bind))
2561 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002562
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002563 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002564
2565 ec->image_target_texture_2d =
2566 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2567 ec->image_target_renderbuffer_storage = (void *)
2568 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
2569 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2570 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2571 ec->bind_display =
2572 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
2573 ec->unbind_display =
2574 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
2575
2576 extensions = (const char *) glGetString(GL_EXTENSIONS);
Pekka Paalanen299e58d2012-04-12 14:45:35 +03002577 if (!extensions) {
2578 fprintf(stderr, "Retrieving GL extension string failed.\n");
2579 return -1;
2580 }
2581
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002582 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
2583 fprintf(stderr,
2584 "GL_EXT_texture_format_BGRA8888 not available\n");
2585 return -1;
2586 }
2587
Pekka Paalanena1d57db2012-04-17 15:02:08 +03002588 if (strstr(extensions, "GL_EXT_read_format_bgra"))
2589 ec->read_format = GL_BGRA_EXT;
2590 else
2591 ec->read_format = GL_RGBA;
Kristian Høgsbergf9247dd2012-03-27 15:25:46 -04002592
Pekka Paalanen52bfbaa2012-04-11 16:19:37 +03002593 if (strstr(extensions, "GL_EXT_unpack_subimage"))
2594 ec->has_unpack_subimage = 1;
Kristian Høgsbergfb6de222012-03-27 17:10:13 -04002595
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002596 extensions =
2597 (const char *) eglQueryString(ec->display, EGL_EXTENSIONS);
Pekka Paalanen299e58d2012-04-12 14:45:35 +03002598 if (!extensions) {
2599 fprintf(stderr, "Retrieving EGL extension string failed.\n");
2600 return -1;
2601 }
2602
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002603 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
2604 ec->has_bind_display = 1;
2605 if (ec->has_bind_display)
2606 ec->bind_display(ec->display, ec->wl_display);
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04002607
Kristian Høgsberg201a9042008-12-10 00:40:50 -05002608 wl_list_init(&ec->surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002609 wl_list_init(&ec->layer_list);
Daniel Stone37816df2012-05-16 18:45:18 +01002610 wl_list_init(&ec->seat_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002611 wl_list_init(&ec->output_list);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04002612 wl_list_init(&ec->binding_list);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002613 wl_list_init(&ec->animation_list);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002614 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002615 ec->fade.animation.frame = fade_frame;
2616 wl_list_init(&ec->fade.animation.link);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04002617
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002618 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2619 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2620
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002621 screenshooter_create(ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002622
Scott Moreauff1db4a2012-04-17 19:06:18 -06002623 ec->ping_handler = NULL;
2624
Kristian Høgsbergdade6492012-01-04 21:47:30 -05002625 wl_data_device_manager_init(ec->wl_display);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002626
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002627 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002628
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002629 if (weston_shader_init(&ec->texture_shader,
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002630 vertex_shader, texture_fragment_shader) < 0)
Kristian Høgsberga9468212010-06-14 21:03:11 -04002631 return -1;
Pekka Paalanenf55f5442012-02-02 16:49:05 +02002632 if (weston_shader_init(&ec->solid_shader,
2633 vertex_shader, solid_fragment_shader) < 0)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002634 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05002635
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002636 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002637 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Pekka Paalanen7296e792011-12-07 16:22:00 +02002638 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002639
Kristian Høgsberg7ea10862012-03-05 17:49:30 -05002640 ec->input_loop = wl_event_loop_create();
2641
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002642 weston_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002643
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002644 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002645}
2646
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002647WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002648weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002649{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002650 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002651
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002652 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002653 if (ec->input_loop_source)
2654 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002655
Matt Roper361d2ad2011-08-29 13:52:23 -07002656 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002657 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002658 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002659
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05002660 weston_binding_list_destroy_all(&ec->binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002661
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002662 wl_array_release(&ec->vertices);
2663 wl_array_release(&ec->indices);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002664
2665 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07002666}
2667
Daniel Stone855028f2012-05-01 20:37:10 +01002668static int weston_compositor_xkb_init(struct weston_compositor *ec,
2669 struct xkb_rule_names *names)
2670{
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002671 ec->xkb_info.context = xkb_context_new(0);
Daniel Stoned42f4572012-05-01 20:37:12 +01002672 if (ec->xkb_info.context == NULL) {
2673 fprintf(stderr, "failed to create XKB context\n");
2674 return -1;
2675 }
2676
Daniel Stone855028f2012-05-01 20:37:10 +01002677 ec->xkb_info.names = *names;
2678 if (!ec->xkb_info.names.rules)
2679 ec->xkb_info.names.rules = strdup("evdev");
2680 if (!ec->xkb_info.names.model)
2681 ec->xkb_info.names.model = strdup("pc105");
2682 if (!ec->xkb_info.names.layout)
2683 ec->xkb_info.names.layout = strdup("us");
2684
Daniel Stoned42f4572012-05-01 20:37:12 +01002685 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_info.context,
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002686 &ec->xkb_info.names, 0);
Daniel Stoned42f4572012-05-01 20:37:12 +01002687 if (ec->xkb_info.keymap == NULL) {
2688 fprintf(stderr, "failed to compile XKB keymap\n");
2689 return -1;
2690 }
2691
2692 ec->xkb_info.state = xkb_state_new(ec->xkb_info.keymap);
2693 if (ec->xkb_info.state == NULL) {
2694 fprintf(stderr, "failed to initialise XKB state\n");
2695 return -1;
2696 }
2697
Daniel Stone855028f2012-05-01 20:37:10 +01002698 return 0;
2699}
2700
2701static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2702{
Daniel Stoned42f4572012-05-01 20:37:12 +01002703 xkb_state_unref(ec->xkb_info.state);
2704 xkb_map_unref(ec->xkb_info.keymap);
2705 xkb_context_unref(ec->xkb_info.context);
2706
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002707 free((char *) ec->xkb_info.names.rules);
2708 free((char *) ec->xkb_info.names.model);
2709 free((char *) ec->xkb_info.names.layout);
2710 free((char *) ec->xkb_info.names.variant);
2711 free((char *) ec->xkb_info.names.options);
Daniel Stone855028f2012-05-01 20:37:10 +01002712}
2713
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002714static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002715{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002716 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002717
Kristian Høgsberg3cad4362011-07-15 21:09:24 -04002718 fprintf(stderr, "caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002719 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002720
2721 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002722}
2723
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002724static void
2725on_segv_signal(int s, siginfo_t *siginfo, void *context)
2726{
2727 void *buffer[32];
2728 int i, count;
2729 Dl_info info;
2730
2731 fprintf(stderr, "caught segv\n");
2732
2733 count = backtrace(buffer, ARRAY_LENGTH(buffer));
2734 for (i = 0; i < count; i++) {
2735 dladdr(buffer[i], &info);
2736 fprintf(stderr, " [%016lx] %s (%s)\n",
2737 (long) buffer[i],
2738 info.dli_sname ? info.dli_sname : "--",
2739 info.dli_fname);
2740 }
2741
2742 longjmp(segv_jmp_buf, 1);
2743}
2744
2745
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002746static void *
2747load_module(const char *name, const char *entrypoint, void **handle)
2748{
2749 char path[PATH_MAX];
2750 void *module, *init;
2751
2752 if (name[0] != '/')
2753 snprintf(path, sizeof path, MODULEDIR "/%s", name);
2754 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02002755 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002756
2757 module = dlopen(path, RTLD_LAZY);
2758 if (!module) {
2759 fprintf(stderr,
Pekka Paalanenf31ad102012-04-26 10:31:36 +03002760 "failed to load module '%s': %s\n", path, dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002761 return NULL;
2762 }
2763
2764 init = dlsym(module, entrypoint);
2765 if (!init) {
2766 fprintf(stderr,
Pekka Paalanenf31ad102012-04-26 10:31:36 +03002767 "failed to lookup init function in '%s': %s\n",
2768 path, dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002769 return NULL;
2770 }
2771
2772 return init;
2773}
2774
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002775int main(int argc, char *argv[])
2776{
2777 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002778 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02002779 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002780 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002781 struct sigaction segv_action;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04002782 void *shell_module, *backend_module, *xserver_module;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04002783 int (*module_init)(struct weston_compositor *ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002784 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002785 *(*backend_init)(struct wl_display *display,
2786 int argc, char *argv[]);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02002787 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002788 char *backend = NULL;
2789 char *shell = NULL;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04002790 char *module = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002791 int32_t idle_time = 300;
Scott Moreaue17f6102012-04-25 10:03:06 -06002792 int32_t xserver = 0;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002793 char *socket_name = NULL;
Tiago Vignatti9a206c42012-03-21 19:49:18 +02002794 char *config_file;
Daniel Stone855028f2012-05-01 20:37:10 +01002795 struct xkb_rule_names xkb_names;
Tiago Vignatti9a206c42012-03-21 19:49:18 +02002796
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002797 const struct config_key shell_config_keys[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02002798 { "type", CONFIG_KEY_STRING, &shell },
2799 };
2800
Daniel Stone855028f2012-05-01 20:37:10 +01002801 const struct config_key keyboard_config_keys[] = {
2802 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2803 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2804 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2805 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2806 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2807 };
2808
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002809 const struct config_section cs[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02002810 { "shell",
2811 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
Daniel Stone855028f2012-05-01 20:37:10 +01002812 { "keyboard",
2813 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02002814 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002815
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002816 const struct weston_option core_options[] = {
2817 { WESTON_OPTION_STRING, "backend", 'B', &backend },
2818 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
2819 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002820 { WESTON_OPTION_BOOLEAN, "xserver", 0, &xserver },
Kristian Høgsberg306e3612012-04-12 12:54:14 -04002821 { WESTON_OPTION_STRING, "module", 0, &module },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002822 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05002823
Daniel Stone855028f2012-05-01 20:37:10 +01002824 memset(&xkb_names, 0, sizeof(xkb_names));
2825
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002826 argc = parse_options(core_options,
2827 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002828
2829 display = wl_display_create();
2830
Tiago Vignatti2116b892011-08-08 05:52:59 -07002831 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02002832 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
2833 display);
2834 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
2835 display);
2836 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
2837 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07002838
2839 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02002840 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
2841 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05002842
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002843 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
2844 segv_action.sa_sigaction = on_segv_signal;
Pekka Paalanen8423a892012-01-19 16:48:37 +02002845 sigemptyset(&segv_action.sa_mask);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002846 sigaction(SIGSEGV, &segv_action, NULL);
2847
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002848 if (!backend) {
2849 if (getenv("WAYLAND_DISPLAY"))
2850 backend = "wayland-backend.so";
2851 else if (getenv("DISPLAY"))
2852 backend = "x11-backend.so";
2853 else if (getenv("OPENWFD"))
2854 backend = "openwfd-backend.so";
2855 else
2856 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002857 }
2858
Tiago Vignatti9a206c42012-03-21 19:49:18 +02002859 config_file = config_file_path("weston.ini");
2860 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
2861 free(config_file);
2862
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002863 backend_init = load_module(backend, "backend_init", &backend_module);
2864 if (!backend_init)
2865 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05002866
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002867 ec = backend_init(display, argc, argv);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05002868 if (ec == NULL) {
2869 fprintf(stderr, "failed to create compositor\n");
2870 exit(EXIT_FAILURE);
2871 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04002872
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002873 for (i = 1; argv[i]; i++)
2874 fprintf(stderr, "unhandled option: %s\n", argv[i]);
2875 if (argv[1])
2876 exit(EXIT_FAILURE);
2877
Daniel Stone855028f2012-05-01 20:37:10 +01002878 if (weston_compositor_xkb_init(ec, &xkb_names) == -1) {
2879 fprintf(stderr, "failed to initialise keyboard support\n");
2880 exit(EXIT_FAILURE);
2881 }
2882
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002883 ec->option_idle_time = idle_time;
2884 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002885
Kristian Høgsberg306e3612012-04-12 12:54:14 -04002886 module_init = NULL;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04002887 if (xserver)
Kristian Høgsberg306e3612012-04-12 12:54:14 -04002888 module_init = load_module("xserver-launcher.so",
2889 "weston_xserver_init",
2890 &xserver_module);
2891 if (module_init && module_init(ec) < 0)
2892 exit(EXIT_FAILURE);
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04002893
Kristian Høgsberg306e3612012-04-12 12:54:14 -04002894 if (!shell)
2895 shell = "desktop-shell.so";
2896 module_init = load_module(shell, "shell_init", &shell_module);
2897 if (!module_init || module_init(ec) < 0)
2898 exit(EXIT_FAILURE);
2899
2900
2901 module_init = NULL;
2902 if (module)
2903 module_init = load_module(module, "module_init", NULL);
2904 if (module_init && module_init(ec) < 0)
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03002905 exit(EXIT_FAILURE);
2906
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002907 if (wl_display_add_socket(display, socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002908 fprintf(stderr, "failed to add socket: %m\n");
2909 exit(EXIT_FAILURE);
2910 }
2911
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002912 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02002913 weston_compositor_wake(ec);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002914 if (setjmp(segv_jmp_buf) == 0)
2915 wl_display_run(display);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002916
Pekka Paalanen0135abe2012-01-03 10:01:20 +02002917 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05002918 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02002919
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002920 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02002921
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002922 if (ec->has_bind_display)
2923 ec->unbind_display(ec->display, display);
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002924
Pekka Paalanen51c769f2012-01-02 16:03:26 +02002925 for (i = ARRAY_LENGTH(signals); i;)
2926 wl_event_source_remove(signals[--i]);
2927
Daniel Stone855028f2012-05-01 20:37:10 +01002928 weston_compositor_xkb_destroy(ec);
2929
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002930 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02002931 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002932
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002933 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002934}