blob: 6a1bebd2250d2991bdd45fbbe97ec182c0a84824 [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øgsbergd34912c2011-05-02 10:36:04 -040045#include <getopt.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040046#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050047#include <setjmp.h>
48#include <execinfo.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050049
Pekka Paalanen50719bc2011-11-22 14:18:50 +020050#include <wayland-server.h>
Kristian Høgsberg82863022010-06-04 21:52:02 -040051#include "compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050052
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -050053static const char *option_socket_name = NULL;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050054
Kristian Høgsberg27da5382011-06-21 17:32:25 -040055static struct wl_list child_process_list;
Kristian Høgsberg0690da62012-01-16 11:53:54 -050056static jmp_buf segv_jmp_buf;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040057
58static int
59sigchld_handler(int signal_number, void *data)
60{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050061 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040062 int status;
63 pid_t pid;
64
65 pid = wait(&status);
66 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
82WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050083weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -040084{
85 wl_list_insert(&child_process_list, &process->link);
86}
87
Benjamin Franzke06286262011-05-06 19:12:33 +020088static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020089child_client_exec(int sockfd, const char *path)
90{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -050091 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020092 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +020093 sigset_t allsigs;
94
95 /* do not give our signal mask to the new process */
96 sigfillset(&allsigs);
97 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020098
Kristian Høgsbergeb764842012-01-31 22:17:25 -050099 /* Launch clients as the user. */
100 seteuid(getuid());
101
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500102 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
103 * non-CLOEXEC fd to pass through exec. */
104 clientfd = dup(sockfd);
105 if (clientfd == -1) {
106 fprintf(stderr, "compositor: dup failed: %m\n");
107 return;
108 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200109
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500110 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200111 setenv("WAYLAND_SOCKET", s, 1);
112
113 if (execl(path, path, NULL) < 0)
114 fprintf(stderr, "compositor: executing '%s' failed: %m\n",
115 path);
116}
117
118WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500119weston_client_launch(struct weston_compositor *compositor,
120 struct weston_process *proc,
121 const char *path,
122 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200123{
124 int sv[2];
125 pid_t pid;
126 struct wl_client *client;
127
128 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500129 fprintf(stderr, "weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200130 "socketpair failed while launching '%s': %m\n",
131 path);
132 return NULL;
133 }
134
135 pid = fork();
136 if (pid == -1) {
137 close(sv[0]);
138 close(sv[1]);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500139 fprintf(stderr, "weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200140 "fork failed while launching '%s': %m\n", path);
141 return NULL;
142 }
143
144 if (pid == 0) {
145 child_client_exec(sv[1], path);
146 exit(-1);
147 }
148
149 close(sv[1]);
150
151 client = wl_client_create(compositor->wl_display, sv[0]);
152 if (!client) {
153 close(sv[0]);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500154 fprintf(stderr, "weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200155 "wl_client_create failed while launching '%s'.\n",
156 path);
157 return NULL;
158 }
159
160 proc->pid = pid;
161 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500162 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200163
164 return client;
165}
166
167static void
Benjamin Franzke06286262011-05-06 19:12:33 +0200168surface_handle_buffer_destroy(struct wl_listener *listener,
169 struct wl_resource *resource, uint32_t time)
170{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500171 struct weston_surface *es =
172 container_of(listener, struct weston_surface,
173 buffer_destroy_listener);
Benjamin Franzke06286262011-05-06 19:12:33 +0200174
Kristian Høgsberg24596942011-10-24 17:51:02 -0400175 es->buffer = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200176}
177
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500178static const pixman_region32_data_t undef_region_data;
179
180static void
181undef_region(pixman_region32_t *region)
182{
183 pixman_region32_fini(region);
184 region->data = (pixman_region32_data_t *) &undef_region_data;
185}
186
187static int
188region_is_undefined(pixman_region32_t *region)
189{
190 return region->data == &undef_region_data;
191}
192
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500193WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500194weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500195{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500196 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400197
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200198 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400199 if (surface == NULL)
200 return NULL;
201
Kristian Høgsberg48891542012-02-23 17:38:33 -0500202 wl_list_init(&surface->surface.resource.destroy_listener_list);
203
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500204 wl_list_init(&surface->link);
Benjamin Franzkebab41fb2011-03-07 18:04:59 +0100205 wl_list_init(&surface->buffer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500206
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400207 surface->surface.resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400208
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500209 surface->compositor = compositor;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200210 surface->image = EGL_NO_IMAGE_KHR;
Kristian Høgsberg541e5552011-12-14 09:24:11 -0500211 surface->alpha = 255;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400212
Benjamin Franzke06286262011-05-06 19:12:33 +0200213 surface->buffer = NULL;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400214 surface->output = NULL;
Alex Wu8811bf92012-02-28 18:07:54 +0800215 surface->force_configure = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200216
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400217 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500218 pixman_region32_init(&surface->opaque);
219 undef_region(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200220 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500221 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400222
Benjamin Franzke06286262011-05-06 19:12:33 +0200223 surface->buffer_destroy_listener.func = surface_handle_buffer_destroy;
Benjamin Franzke06286262011-05-06 19:12:33 +0200224
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200225 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200226 wl_list_insert(&surface->geometry.transformation_list,
227 &surface->transform.position.link);
228 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200229 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200230 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500231
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400232 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500233}
234
Alex Wu8811bf92012-02-28 18:07:54 +0800235WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500236weston_surface_set_color(struct weston_surface *surface,
237 GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
238{
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500239 surface->color[0] = red;
240 surface->color[1] = green;
241 surface->color[2] = blue;
242 surface->color[3] = alpha;
243 surface->shader = &surface->compositor->solid_shader;
244}
245
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200246static void
Pekka Paalanenece8a012012-02-08 15:23:15 +0200247surface_to_global_float(struct weston_surface *surface,
248 int32_t sx, int32_t sy, GLfloat *x, GLfloat *y)
249{
250 if (surface->transform.enabled) {
251 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
252
253 weston_matrix_transform(&surface->transform.matrix, &v);
254
255 if (fabsf(v.f[3]) < 1e-6) {
256 fprintf(stderr, "warning: numerical instability in "
257 "weston_surface_to_global(), divisor = %g\n",
258 v.f[3]);
259 *x = 0;
260 *y = 0;
261 return;
262 }
263
264 *x = v.f[0] / v.f[3];
265 *y = v.f[1] / v.f[3];
266 } else {
267 *x = sx + surface->geometry.x;
268 *y = sy + surface->geometry.y;
269 }
270}
271
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500272WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500273weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200274{
275 struct weston_surface *below;
276
277 if (surface->output == NULL)
278 return;
279
280 if (surface->link.next == &surface->compositor->surface_list)
281 return;
282
283 below = container_of(surface->link.next, struct weston_surface, link);
284 pixman_region32_union(&below->damage, &below->damage,
285 &surface->transform.boundingbox);
286}
287
288static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200289surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
290 int32_t width, int32_t height,
291 pixman_region32_t *bbox)
292{
Pekka Paalanen219b9822012-02-08 15:38:37 +0200293 GLfloat min_x = HUGE_VALF, min_y = HUGE_VALF;
294 GLfloat max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200295 int32_t s[4][2] = {
296 { sx, sy },
297 { sx, sy + height },
298 { sx + width, sy },
299 { sx + width, sy + height }
300 };
Pekka Paalanen219b9822012-02-08 15:38:37 +0200301 GLfloat int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200302 int i;
303
304 for (i = 0; i < 4; ++i) {
Pekka Paalanen219b9822012-02-08 15:38:37 +0200305 GLfloat x, y;
306 surface_to_global_float(surface, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200307 if (x < min_x)
308 min_x = x;
309 if (x > max_x)
310 max_x = x;
311 if (y < min_y)
312 min_y = y;
313 if (y > max_y)
314 max_y = y;
315 }
316
Pekka Paalanen219b9822012-02-08 15:38:37 +0200317 int_x = floorf(min_x);
318 int_y = floorf(min_y);
319 pixman_region32_init_rect(bbox, int_x, int_y,
320 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200321}
322
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200323static void
324weston_surface_update_transform_disable(struct weston_surface *surface)
325{
326 surface->transform.enabled = 0;
327
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200328 /* round off fractions when not transformed */
329 surface->geometry.x = roundf(surface->geometry.x);
330 surface->geometry.y = roundf(surface->geometry.y);
331
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200332 pixman_region32_init_rect(&surface->transform.boundingbox,
333 surface->geometry.x,
334 surface->geometry.y,
335 surface->geometry.width,
336 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500337
338 pixman_region32_copy(&surface->transform.opaque, &surface->opaque);
339 pixman_region32_translate(&surface->transform.opaque,
340 surface->geometry.x, surface->geometry.y);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200341}
342
343static int
344weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200345{
346 struct weston_matrix *matrix = &surface->transform.matrix;
347 struct weston_matrix *inverse = &surface->transform.inverse;
348 struct weston_transform *tform;
349
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200350 surface->transform.enabled = 1;
351
352 /* Otherwise identity matrix, but with x and y translation. */
353 surface->transform.position.matrix.d[12] = surface->geometry.x;
354 surface->transform.position.matrix.d[13] = surface->geometry.y;
355
356 weston_matrix_init(matrix);
357 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
358 weston_matrix_multiply(matrix, &tform->matrix);
359
360 if (weston_matrix_invert(inverse, matrix) < 0) {
361 /* Oops, bad total transformation, not invertible */
362 fprintf(stderr, "error: weston_surface %p"
363 " transformation not invertible.\n", surface);
364 return -1;
365 }
366
367 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
368 surface->geometry.height,
369 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500370
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200371 return 0;
372}
373
374WL_EXPORT void
375weston_surface_update_transform(struct weston_surface *surface)
376{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200377 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200378 return;
379
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200380 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200381
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500382 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200383
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200384 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500385 pixman_region32_fini(&surface->transform.opaque);
386 pixman_region32_init(&surface->transform.opaque);
387
388 if (region_is_undefined(&surface->input))
389 pixman_region32_init_rect(&surface->input, 0, 0,
390 surface->geometry.width,
391 surface->geometry.height);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200392
Pekka Paalanencd403622012-01-25 13:37:39 +0200393 /* transform.position is always in transformation_list */
394 if (surface->geometry.transformation_list.next ==
395 &surface->transform.position.link &&
396 surface->geometry.transformation_list.prev ==
397 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200398 weston_surface_update_transform_disable(surface);
399 } else {
400 if (weston_surface_update_transform_enable(surface) < 0)
401 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200402 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200403
404 /* weston_surface_damage() without update */
405 pixman_region32_union(&surface->damage, &surface->damage,
406 &surface->transform.boundingbox);
407
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +0200408 if (surface->output)
409 weston_surface_assign_output(surface);
410
Pekka Paalanen96516782012-02-09 15:32:15 +0200411 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200412}
413
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200414WL_EXPORT void
415weston_surface_to_global(struct weston_surface *surface,
416 int32_t sx, int32_t sy, int32_t *x, int32_t *y)
417{
418 GLfloat xf, yf;
419
420 weston_surface_update_transform(surface);
421
422 surface_to_global_float(surface, sx, sy, &xf, &yf);
423 *x = floorf(xf);
424 *y = floorf(yf);
425}
426
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200427static void
428surface_from_global_float(struct weston_surface *surface,
429 int32_t x, int32_t y, GLfloat *sx, GLfloat *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200430{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200431 if (surface->transform.enabled) {
432 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
433
434 weston_matrix_transform(&surface->transform.inverse, &v);
435
436 if (fabsf(v.f[3]) < 1e-6) {
437 fprintf(stderr, "warning: numerical instability in "
438 "weston_surface_from_global(), divisor = %g\n",
439 v.f[3]);
440 *sx = 0;
441 *sy = 0;
442 return;
443 }
444
Pekka Paalanencd403622012-01-25 13:37:39 +0200445 *sx = v.f[0] / v.f[3];
446 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200447 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200448 *sx = x - surface->geometry.x;
449 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200450 }
451}
452
453WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200454weston_surface_from_global(struct weston_surface *surface,
455 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
456{
457 GLfloat sxf, syf;
458
459 weston_surface_update_transform(surface);
460
461 surface_from_global_float(surface, x, y, &sxf, &syf);
462 *sx = floorf(sxf);
463 *sy = floorf(syf);
464}
465
Tiago Vignatti9d393522012-02-10 16:26:19 +0200466static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500467weston_surface_damage_rectangle(struct weston_surface *surface,
Pekka Paalanen2267d452012-01-26 13:12:45 +0200468 int32_t sx, int32_t sy,
469 int32_t width, int32_t height)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500470{
Pekka Paalanen2267d452012-01-26 13:12:45 +0200471 weston_surface_update_transform(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500472
Pekka Paalanen2267d452012-01-26 13:12:45 +0200473 if (surface->transform.enabled) {
474 pixman_region32_t box;
475 surface_compute_bbox(surface, sx, sy, width, height, &box);
476 pixman_region32_union(&surface->damage, &surface->damage,
477 &box);
478 pixman_region32_fini(&box);
479 } else {
Pekka Paalanen2267d452012-01-26 13:12:45 +0200480 pixman_region32_union_rect(&surface->damage, &surface->damage,
Pekka Paalanen1d5035c2012-02-09 15:27:45 +0200481 surface->geometry.x + sx,
482 surface->geometry.y + sy,
483 width, height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200484 }
485
486 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500487}
488
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400489WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500490weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500491{
Pekka Paalanen2267d452012-01-26 13:12:45 +0200492 weston_surface_update_transform(surface);
493
494 pixman_region32_union(&surface->damage, &surface->damage,
495 &surface->transform.boundingbox);
496
497 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500498}
499
Tiago Vignatti9d393522012-02-10 16:26:19 +0200500static void
Kristian Høgsberg90b53812012-01-18 21:41:37 -0500501weston_surface_flush_damage(struct weston_surface *surface)
502{
503 struct weston_surface *below;
504
505 if (surface->output &&
506 surface->link.next != &surface->compositor->surface_list) {
507 below = container_of(surface->link.next,
508 struct weston_surface, link);
509
510 pixman_region32_union(&below->damage,
511 &below->damage, &surface->damage);
512 }
513}
514
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400515WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500516weston_surface_configure(struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200517 GLfloat x, GLfloat y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400518{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200519 surface->geometry.x = x;
520 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200521 surface->geometry.width = width;
522 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200523 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400524}
525
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200526WL_EXPORT void
527weston_surface_set_position(struct weston_surface *surface,
528 GLfloat x, GLfloat y)
529{
530 surface->geometry.x = x;
531 surface->geometry.y = y;
532 surface->geometry.dirty = 1;
533}
534
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400535WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500536weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500537{
538 struct timeval tv;
539
540 gettimeofday(&tv, NULL);
541
542 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
543}
544
Tiago Vignatti9d393522012-02-10 16:26:19 +0200545static struct weston_surface *
546weston_compositor_pick_surface(struct weston_compositor *compositor,
547 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
548{
549 struct weston_surface *surface;
550
551 wl_list_for_each(surface, &compositor->surface_list, link) {
Tiago Vignatti9d393522012-02-10 16:26:19 +0200552 weston_surface_from_global(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500553 if (pixman_region32_contains_point(&surface->input,
554 *sx, *sy, NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200555 return surface;
556 }
557
558 return NULL;
559}
560
561static void
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500562weston_device_repick(struct wl_input_device *device, uint32_t time)
563{
564 struct weston_input_device *wd = (struct weston_input_device *) device;
Scott Moreau447013d2012-02-18 05:05:29 -0700565 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500566 struct weston_surface *surface, *focus;
567
568 surface = weston_compositor_pick_surface(wd->compositor,
569 device->x, device->y,
570 &device->current_x,
571 &device->current_y);
572
573 if (&surface->surface != device->current) {
Scott Moreau447013d2012-02-18 05:05:29 -0700574 interface = device->pointer_grab->interface;
575 interface->focus(device->pointer_grab, time, &surface->surface,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500576 device->current_x, device->current_y);
577 device->current = &surface->surface;
578 }
579
Scott Moreau447013d2012-02-18 05:05:29 -0700580 focus = (struct weston_surface *) device->pointer_grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500581 if (focus)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200582 weston_surface_from_global(focus, device->x, device->y,
Scott Moreau447013d2012-02-18 05:05:29 -0700583 &device->pointer_grab->x, &device->pointer_grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500584}
585
586WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500587weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400588{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500589 struct weston_input_device *device;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400590 uint32_t time;
591
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500592 if (!compositor->focus)
593 return;
594
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500595 time = weston_compositor_get_time();
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500596 wl_list_for_each(device, &compositor->input_device_list, link)
597 weston_device_repick(&device->input_device, time);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400598}
599
600static void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500601weston_surface_unmap(struct weston_surface *surface)
602{
603 weston_surface_damage_below(surface);
604 weston_surface_flush_damage(surface);
605 surface->output = NULL;
606 wl_list_remove(&surface->link);
607 weston_compositor_repick(surface->compositor);
608 weston_compositor_schedule_repaint(surface->compositor);
609}
610
611static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400612destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500613{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500614 struct weston_surface *surface =
615 container_of(resource,
616 struct weston_surface, surface.resource);
617 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400618
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500619 if (surface->output)
620 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400621
Kristian Høgsberg12bbf812012-02-17 12:15:27 -0500622 if (surface->texture)
623 glDeleteTextures(1, &surface->texture);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200624
Benjamin Franzke06286262011-05-06 19:12:33 +0200625 if (surface->buffer)
626 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400627
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200628 if (surface->image != EGL_NO_IMAGE_KHR)
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400629 compositor->destroy_image(compositor->display,
630 surface->image);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200631
Benjamin Franzkebab41fb2011-03-07 18:04:59 +0100632 wl_list_remove(&surface->buffer_link);
633
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200634 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200635 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500636 pixman_region32_fini(&surface->opaque);
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500637 if (!region_is_undefined(&surface->input))
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500638 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200639
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400640 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500641}
642
Alex Wu8811bf92012-02-28 18:07:54 +0800643WL_EXPORT void
644weston_surface_destroy(struct weston_surface *surface)
645{
646 /* Not a valid way to destroy a client surface */
647 assert(surface->surface.resource.client == NULL);
648
649 destroy_surface(&surface->surface.resource);
650}
651
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100652static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500653weston_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100654{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500655 struct weston_surface *es = (struct weston_surface *) surface;
656 struct weston_compositor *ec = es->compositor;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100657 struct wl_list *surfaces_attached_to;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100658
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500659 if (!es->texture) {
660 glGenTextures(1, &es->texture);
661 glBindTexture(GL_TEXTURE_2D, es->texture);
662 glTexParameteri(GL_TEXTURE_2D,
663 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
664 glTexParameteri(GL_TEXTURE_2D,
665 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
666 es->shader = &ec->texture_shader;
667 } else {
668 glBindTexture(GL_TEXTURE_2D, es->texture);
669 }
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100670
671 if (wl_buffer_is_shm(buffer)) {
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200672 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100673 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200674 es->pitch, buffer->height, 0,
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100675 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
676 wl_shm_buffer_get_data(buffer));
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400677
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100678 surfaces_attached_to = buffer->user_data;
679
Kristian Høgsbergfac11d22011-05-02 13:35:17 -0400680 wl_list_remove(&es->buffer_link);
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100681 wl_list_insert(surfaces_attached_to, &es->buffer_link);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100682 } else {
Benjamin Franzkefb4b5a22011-06-21 10:44:37 +0200683 if (es->image != EGL_NO_IMAGE_KHR)
684 ec->destroy_image(ec->display, es->image);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400685 es->image = ec->create_image(ec->display, NULL,
686 EGL_WAYLAND_BUFFER_WL,
687 buffer, NULL);
688
689 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400690
Pekka Paalanen60921e52012-01-25 15:55:43 +0200691 es->pitch = es->geometry.width;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100692 }
693}
694
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400695static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500696texture_region(struct weston_surface *es, pixman_region32_t *region)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500697{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500698 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500699 GLfloat *v, inv_width, inv_height;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200700 GLfloat sx, sy;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500701 pixman_box32_t *rectangles;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400702 unsigned int *p;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500703 int i, n;
704
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400705 rectangles = pixman_region32_rectangles(region, &n);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500706 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
707 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200708 inv_width = 1.0 / es->pitch;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200709 inv_height = 1.0 / es->geometry.height;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400710
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500711 for (i = 0; i < n; i++, v += 16, p += 6) {
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200712 surface_from_global_float(es, rectangles[i].x1,
713 rectangles[i].y1, &sx, &sy);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500714 v[ 0] = rectangles[i].x1;
715 v[ 1] = rectangles[i].y1;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200716 v[ 2] = sx * inv_width;
717 v[ 3] = sy * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500718
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200719 surface_from_global_float(es, rectangles[i].x1,
720 rectangles[i].y2, &sx, &sy);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500721 v[ 4] = rectangles[i].x1;
722 v[ 5] = rectangles[i].y2;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200723 v[ 6] = sx * inv_width;
724 v[ 7] = sy * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500725
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200726 surface_from_global_float(es, rectangles[i].x2,
727 rectangles[i].y1, &sx, &sy);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500728 v[ 8] = rectangles[i].x2;
729 v[ 9] = rectangles[i].y1;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200730 v[10] = sx * inv_width;
731 v[11] = sy * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500732
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200733 surface_from_global_float(es, rectangles[i].x2,
734 rectangles[i].y2, &sx, &sy);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500735 v[12] = rectangles[i].x2;
736 v[13] = rectangles[i].y2;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200737 v[14] = sx * inv_width;
738 v[15] = sy * inv_height;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500739
740 p[0] = i * 4 + 0;
741 p[1] = i * 4 + 1;
742 p[2] = i * 4 + 2;
743 p[3] = i * 4 + 2;
744 p[4] = i * 4 + 1;
745 p[5] = i * 4 + 3;
746 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500747
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400748 return n;
749}
750
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500751WL_EXPORT void
Kristian Høgsberga0d6dc42012-01-25 22:02:00 -0500752weston_surface_draw(struct weston_surface *es, struct weston_output *output)
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400753{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500754 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400755 GLfloat *v;
756 pixman_region32_t repaint;
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400757 GLint filter;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400758 int n;
759
Pekka Paalanena9f8a212012-01-26 11:28:08 +0200760 pixman_region32_init(&repaint);
761 pixman_region32_intersect(&repaint, &es->transform.boundingbox,
762 &output->region);
Kristian Høgsberga0d6dc42012-01-25 22:02:00 -0500763 pixman_region32_intersect(&repaint, &repaint, &es->damage);
764
Pekka Paalanenc7b45f62012-02-06 12:16:07 +0200765 /* Clear damage, assume outputs do not overlap. */
766 pixman_region32_subtract(&es->damage, &es->damage, &output->region);
767
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400768 if (!pixman_region32_not_empty(&repaint))
Pekka Paalanena9f8a212012-01-26 11:28:08 +0200769 goto out;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400770
Kristian Høgsberg101cb652012-02-17 10:45:16 -0500771 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
772 glEnable(GL_BLEND);
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400773
Kristian Høgsberg07632622012-01-25 23:02:06 -0500774 if (ec->current_shader != es->shader) {
775 glUseProgram(es->shader->program);
Kristian Høgsberg07632622012-01-25 23:02:06 -0500776 ec->current_shader = es->shader;
777 }
778
Kristian Høgsberg0452abc2012-01-31 15:38:36 -0500779 glUniformMatrix4fv(es->shader->proj_uniform,
780 1, GL_FALSE, output->matrix.d);
Pekka Paalanenf55f5442012-02-02 16:49:05 +0200781 glUniform1i(es->shader->tex_uniform, 0);
782 glUniform4fv(es->shader->color_uniform, 1, es->color);
783 glUniform1f(es->shader->alpha_uniform, es->alpha / 255.0);
784 glUniform1f(es->shader->texwidth_uniform,
785 (GLfloat)es->geometry.width / es->pitch);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200786
Scott Moreauccbf29d2012-02-22 14:21:41 -0700787 if (es->transform.enabled || output->zoom.active)
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400788 filter = GL_LINEAR;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200789 else
Pekka Paalanenc61eca62012-01-06 14:10:06 +0200790 filter = GL_NEAREST;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200791
792 n = texture_region(es, &repaint);
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400793
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500794 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400795 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
796 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
797
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500798 v = ec->vertices.data;
799 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
800 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400801 glEnableVertexAttribArray(0);
802 glEnableVertexAttribArray(1);
Pekka Paalanen3df327f2012-01-24 15:53:35 +0200803
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500804 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
805
Pekka Paalanen3df327f2012-01-24 15:53:35 +0200806 glDisableVertexAttribArray(1);
807 glDisableVertexAttribArray(0);
808
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500809 ec->vertices.size = 0;
810 ec->indices.size = 0;
Pekka Paalanena9f8a212012-01-26 11:28:08 +0200811
812out:
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500813 pixman_region32_fini(&repaint);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500814}
815
Kristian Høgsberga82c4862012-01-26 01:03:58 -0500816WL_EXPORT struct wl_list *
817weston_compositor_top(struct weston_compositor *compositor)
818{
819 struct weston_input_device *input_device;
820 struct wl_list *list;
821
822 input_device = (struct weston_input_device *) compositor->input_device;
823
824 /* Insert below pointer */
825 list = &compositor->surface_list;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500826 if (compositor->fade.surface &&
827 list->next == &compositor->fade.surface->link)
828 list = list->next;
Kristian Høgsberga82c4862012-01-26 01:03:58 -0500829 if (list->next == &input_device->sprite->link)
830 list = list->next;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +0200831 if (input_device->drag_surface &&
832 list->next == &input_device->drag_surface->link)
833 list = list->next;
Kristian Høgsberga82c4862012-01-26 01:03:58 -0500834
835 return list;
836}
837
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500838static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500839weston_surface_raise(struct weston_surface *surface)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400840{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500841 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberga82c4862012-01-26 01:03:58 -0500842 struct wl_list *list = weston_compositor_top(compositor);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400843
844 wl_list_remove(&surface->link);
Kristian Høgsberga82c4862012-01-26 01:03:58 -0500845 wl_list_insert(list, &surface->link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500846 weston_compositor_repick(compositor);
847 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400848}
849
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400850WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500851weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400852{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500853 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400854
855 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500856 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400857}
858
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500859WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500860weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +0200861{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400862 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200863 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200864
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400865 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -0500866 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200867}
868
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400869WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500870weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400871{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500872 struct weston_compositor *compositor = output->compositor;
873 struct weston_surface *es;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400874
Kristian Høgsbergd8264042011-11-11 11:48:12 -0500875 if (wl_list_empty(&compositor->surface_list))
876 return;
877
878 es = container_of(compositor->surface_list.next,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500879 struct weston_surface, link);
Kristian Høgsbergd8264042011-11-11 11:48:12 -0500880 pixman_region32_union(&es->damage, &es->damage, &output->region);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500881 weston_compositor_schedule_repaint(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400882}
883
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400884static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500885fade_frame(struct weston_animation *animation,
886 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400887{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500888 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400889 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500890 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500891 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400892
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500893 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500894 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500895 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
896 compositor->fade.spring.current);
897 weston_surface_damage(surface);
898
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500899 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400900 compositor->fade.spring.current =
901 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400902 wl_list_remove(&animation->link);
903 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200904
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500905 if (compositor->fade.spring.current < 0.001) {
906 destroy_surface(&surface->surface.resource);
907 compositor->fade.surface = NULL;
908 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500909 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200910 compositor->shell->lock(compositor->shell);
911 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400912 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400913}
914
Kristian Høgsberg30c018b2012-01-26 08:40:37 -0500915struct weston_frame_callback {
916 struct wl_resource resource;
917 struct wl_list link;
918};
919
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400920static void
Kristian Høgsberg30c018b2012-01-26 08:40:37 -0500921weston_output_repaint(struct weston_output *output, int msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400922{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500923 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500924 struct weston_surface *es;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -0500925 struct weston_animation *animation, *next;
926 struct weston_frame_callback *cb, *cnext;
Kristian Høgsbergd15fbe32012-02-23 23:06:13 -0500927 pixman_region32_t opaque, new_damage, total_damage;
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500928 int32_t width, height;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500929
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +0200930 weston_compositor_update_drag_surfaces(ec);
931
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500932 width = output->current->width +
933 output->border.left + output->border.right;
934 height = output->current->height +
935 output->border.top + output->border.bottom;
936 glViewport(0, 0, width, height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500937
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500938 pixman_region32_init(&new_damage);
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400939 pixman_region32_init(&opaque);
Kristian Høgsberg765bcdf2012-01-25 22:20:30 -0500940
Kristian Høgsbergd15fbe32012-02-23 23:06:13 -0500941 wl_list_for_each(es, &ec->surface_list, link)
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +0200942 /* Update surface transform now to avoid calling it ever
943 * again from the repaint sub-functions. */
Pekka Paalanen15d60ef2012-01-27 14:38:33 +0200944 weston_surface_update_transform(es);
945
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800946 if (output->assign_planes)
947 /*
948 * This will queue flips for the fbs and sprites where
949 * applicable and clear the damage for those surfaces.
950 * The repaint loop below will repaint everything
951 * else.
952 */
953 output->assign_planes(output);
954
Kristian Høgsberga82c4862012-01-26 01:03:58 -0500955 wl_list_for_each(es, &ec->surface_list, link) {
956 pixman_region32_subtract(&es->damage, &es->damage, &opaque);
957 pixman_region32_union(&new_damage, &new_damage, &es->damage);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200958 pixman_region32_union(&opaque, &opaque, &es->transform.opaque);
Kristian Høgsberga82c4862012-01-26 01:03:58 -0500959 }
960
Kristian Høgsberg8b72f602011-06-23 20:46:34 -0400961 pixman_region32_init(&total_damage);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500962 pixman_region32_union(&total_damage, &new_damage,
Kristian Høgsberg8b72f602011-06-23 20:46:34 -0400963 &output->previous_damage);
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400964 pixman_region32_intersect(&output->previous_damage,
965 &new_damage, &output->region);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500966
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400967 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -0400968 pixman_region32_fini(&new_damage);
969
Kristian Høgsbergac587412012-01-25 21:48:26 -0500970 wl_list_for_each(es, &ec->surface_list, link) {
971 pixman_region32_copy(&es->damage, &total_damage);
972 pixman_region32_subtract(&total_damage,
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200973 &total_damage, &es->transform.opaque);
Kristian Høgsbergac587412012-01-25 21:48:26 -0500974 }
975
Scott Moreauccbf29d2012-02-22 14:21:41 -0700976 if (output->dirty)
977 weston_output_update_matrix(output);
978
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500979 output->repaint(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400980
Pekka Paalanencc9102e2012-01-02 15:42:44 +0200981 pixman_region32_fini(&total_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500982
Kristian Høgsbergef044142011-06-21 15:02:12 -0400983 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100984
Kristian Høgsberg33418202011-08-16 23:01:28 -0400985 wl_list_for_each_safe(cb, cnext, &output->frame_callback_list, link) {
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400986 wl_resource_post_event(&cb->resource, WL_CALLBACK_DONE, msecs);
987 wl_resource_destroy(&cb->resource, 0);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500988 }
989
Kristian Høgsberg30c018b2012-01-26 08:40:37 -0500990 wl_list_for_each_safe(animation, next, &ec->animation_list, link)
Kristian Høgsbergef044142011-06-21 15:02:12 -0400991 animation->frame(animation, output, msecs);
992}
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400993
Kristian Høgsbergef044142011-06-21 15:02:12 -0400994static void
995idle_repaint(void *data)
996{
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -0500997 struct weston_output *output = data;
998
999 /* An idle repaint may have been cancelled by vt switching away. */
1000 if (output->repaint_needed)
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001001 weston_output_repaint(output, weston_compositor_get_time());
Kristian Høgsberg2200d702012-01-26 08:47:04 -05001002 else
1003 output->repaint_scheduled = 0;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001004}
1005
1006WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001007weston_output_finish_frame(struct weston_output *output, int msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001008{
Kristian Høgsbergef044142011-06-21 15:02:12 -04001009 if (output->repaint_needed)
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001010 weston_output_repaint(output, msecs);
Ander Conselvan de Oliveira22e22a52011-10-24 16:30:14 +03001011 else
1012 output->repaint_scheduled = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001013}
1014
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001015WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001016weston_compositor_schedule_repaint(struct weston_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001017{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001018 struct weston_output *output;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001019 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001020
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001021 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001022 return;
1023
Kristian Høgsbergef044142011-06-21 15:02:12 -04001024 loop = wl_display_get_event_loop(compositor->wl_display);
1025 wl_list_for_each(output, &compositor->output_list, link) {
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001026 output->repaint_needed = 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001027 if (output->repaint_scheduled)
1028 continue;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001029
Kristian Høgsbergef044142011-06-21 15:02:12 -04001030 wl_event_loop_add_idle(loop, idle_repaint, output);
1031 output->repaint_scheduled = 1;
1032 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001033}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001034
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001035WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001036weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001037{
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001038 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001039 int done;
1040
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001041 done = weston_spring_done(&compositor->fade.spring);
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001042 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001043 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001044 return;
1045
1046 if (done)
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001047 compositor->fade.spring.timestamp =
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001048 weston_compositor_get_time();
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001049
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001050 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001051 surface = weston_surface_create(compositor);
1052 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001053 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
1054 wl_list_insert(&compositor->surface_list, &surface->link);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001055 weston_surface_assign_output(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001056 compositor->fade.surface = surface;
1057 }
1058
1059 weston_surface_damage(compositor->fade.surface);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001060 if (wl_list_empty(&compositor->fade.animation.link))
1061 wl_list_insert(compositor->animation_list.prev,
1062 &compositor->fade.animation.link);
1063}
1064
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001065static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001066surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001067{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001068 wl_resource_destroy(resource, weston_compositor_get_time());
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001069}
1070
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001071WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001072weston_surface_assign_output(struct weston_surface *es)
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001073{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001074 struct weston_compositor *ec = es->compositor;
1075 struct weston_output *output, *new_output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001076 pixman_region32_t region;
1077 uint32_t max, area;
1078 pixman_box32_t *e;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001079
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001080 weston_surface_update_transform(es);
1081
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001082 new_output = NULL;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001083 max = 0;
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001084 pixman_region32_init(&region);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001085 wl_list_for_each(output, &ec->output_list, link) {
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001086 pixman_region32_intersect(&region, &es->transform.boundingbox,
1087 &output->region);
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001088
1089 e = pixman_region32_extents(&region);
1090 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1091
1092 if (area >= max) {
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001093 new_output = output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001094 max = area;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001095 }
1096 }
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001097 pixman_region32_fini(&region);
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001098
1099 es->output = new_output;
1100 if (!wl_list_empty(&es->frame_callback_list)) {
1101 wl_list_insert_list(new_output->frame_callback_list.prev,
1102 &es->frame_callback_list);
1103 wl_list_init(&es->frame_callback_list);
1104 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001105}
1106
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001107static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001108surface_attach(struct wl_client *client,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001109 struct wl_resource *resource,
Pekka Paalanena4f80f32012-01-31 12:04:54 +02001110 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001111{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001112 struct weston_surface *es = resource->data;
1113 struct weston_shell *shell = es->compositor->shell;
Kristian Høgsbergd0e2ef72012-02-19 22:15:21 -05001114 struct wl_buffer *buffer;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001115
Ander Conselvan de Oliveirab63e3e02012-02-15 17:02:53 +02001116 if (!buffer_resource && !es->output)
1117 return;
1118
Kristian Høgsberg24596942011-10-24 17:51:02 -04001119 if (es->buffer) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001120 weston_buffer_post_release(es->buffer);
Kristian Høgsberg24596942011-10-24 17:51:02 -04001121 wl_list_remove(&es->buffer_destroy_listener.link);
1122 }
Benjamin Franzke06286262011-05-06 19:12:33 +02001123
Kristian Høgsbergd1e86252012-01-30 09:10:14 -05001124 if (!buffer_resource && es->output) {
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001125 weston_surface_unmap(es);
Ander Conselvan de Oliveirab63e3e02012-02-15 17:02:53 +02001126 es->buffer = NULL;
Kristian Høgsbergd1e86252012-01-30 09:10:14 -05001127 return;
1128 }
1129
1130 buffer = buffer_resource->data;
Kristian Høgsberg24596942011-10-24 17:51:02 -04001131 buffer->busy_count++;
Benjamin Franzke06286262011-05-06 19:12:33 +02001132 es->buffer = buffer;
Benjamin Franzke06286262011-05-06 19:12:33 +02001133 wl_list_insert(es->buffer->resource.destroy_listener_list.prev,
1134 &es->buffer_destroy_listener.link);
1135
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001136 if (es->geometry.width != buffer->width ||
1137 es->geometry.height != buffer->height) {
1138 undef_region(&es->input);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001139 pixman_region32_fini(&es->opaque);
1140 pixman_region32_init(&es->opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001141 }
1142
Kristian Høgsbergd0e2ef72012-02-19 22:15:21 -05001143 if (es->output == NULL) {
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001144 shell->map(shell, es, buffer->width, buffer->height, sx, sy);
Alex Wu8811bf92012-02-28 18:07:54 +08001145 } else if (es->force_configure || sx != 0 || sy != 0 ||
Pekka Paalanen60921e52012-01-25 15:55:43 +02001146 es->geometry.width != buffer->width ||
1147 es->geometry.height != buffer->height) {
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001148 GLfloat from_x, from_y;
1149 GLfloat to_x, to_y;
Pekka Paalanena4f80f32012-01-31 12:04:54 +02001150
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001151 surface_to_global_float(es, 0, 0, &from_x, &from_y);
1152 surface_to_global_float(es, sx, sy, &to_x, &to_y);
Pekka Paalanena4f80f32012-01-31 12:04:54 +02001153 shell->configure(shell, es,
1154 es->geometry.x + to_x - from_x,
1155 es->geometry.y + to_y - from_y,
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001156 buffer->width, buffer->height);
Alex Wu8811bf92012-02-28 18:07:54 +08001157 es->force_configure = 0;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001158 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001159
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001160 weston_buffer_attach(buffer, &es->surface);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001161}
1162
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001163static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001164surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001165 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001166 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001167{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001168 struct weston_surface *es = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001169
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001170 weston_surface_damage_rectangle(es, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001171}
1172
Kristian Høgsberg33418202011-08-16 23:01:28 -04001173static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001174destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001175{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001176 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001177
1178 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001179 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001180}
1181
1182static void
1183surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001184 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001185{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001186 struct weston_frame_callback *cb;
1187 struct weston_surface *es = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001188
1189 cb = malloc(sizeof *cb);
1190 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001191 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001192 return;
1193 }
1194
1195 cb->resource.object.interface = &wl_callback_interface;
1196 cb->resource.object.id = callback;
1197 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001198 cb->resource.client = client;
1199 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001200
1201 wl_client_add_resource(client, &cb->resource);
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -04001202
1203 if (es->output) {
1204 wl_list_insert(es->output->frame_callback_list.prev,
1205 &cb->link);
1206 } else {
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001207 wl_list_insert(es->frame_callback_list.prev, &cb->link);
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -04001208 }
Kristian Høgsberg33418202011-08-16 23:01:28 -04001209}
1210
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001211static void
1212surface_set_opaque_region(struct wl_client *client,
1213 struct wl_resource *resource,
1214 struct wl_resource *region_resource)
1215{
1216 struct weston_surface *surface = resource->data;
1217 struct weston_region *region;
1218
1219 pixman_region32_fini(&surface->opaque);
1220
1221 if (region_resource) {
1222 region = region_resource->data;
1223 pixman_region32_init_rect(&surface->opaque, 0, 0,
1224 surface->geometry.width,
1225 surface->geometry.height);
1226 pixman_region32_intersect(&surface->opaque,
1227 &surface->opaque, &region->region);
1228 } else {
1229 pixman_region32_init(&surface->opaque);
1230 }
1231
1232 surface->geometry.dirty = 1;
1233}
1234
1235static void
1236surface_set_input_region(struct wl_client *client,
1237 struct wl_resource *resource,
1238 struct wl_resource *region_resource)
1239{
1240 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001241 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001242
1243 if (region_resource) {
1244 region = region_resource->data;
1245 pixman_region32_init_rect(&surface->input, 0, 0,
1246 surface->geometry.width,
1247 surface->geometry.height);
1248 pixman_region32_intersect(&surface->input,
1249 &surface->input, &region->region);
1250 } else {
1251 pixman_region32_init_rect(&surface->input, 0, 0,
1252 surface->geometry.width,
1253 surface->geometry.height);
1254 }
1255
1256 weston_compositor_repick(surface->compositor);
1257}
1258
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001259const static struct wl_surface_interface surface_interface = {
1260 surface_destroy,
1261 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001262 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001263 surface_frame,
1264 surface_set_opaque_region,
1265 surface_set_input_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001266};
1267
1268static void
1269compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001270 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001271{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001272 struct weston_compositor *ec = resource->data;
1273 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001274
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001275 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001276 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001277 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001278 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001279 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001280
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001281 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001282
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001283 surface->surface.resource.object.id = id;
1284 surface->surface.resource.object.interface = &wl_surface_interface;
1285 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001286 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001287 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001288
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001289 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001290}
1291
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001292static void
1293destroy_region(struct wl_resource *resource)
1294{
1295 struct weston_region *region =
1296 container_of(resource, struct weston_region, resource);
1297
1298 pixman_region32_fini(&region->region);
1299 free(region);
1300}
1301
1302static void
1303region_destroy(struct wl_client *client, struct wl_resource *resource)
1304{
1305 wl_resource_destroy(resource, weston_compositor_get_time());
1306}
1307
1308static void
1309region_add(struct wl_client *client, struct wl_resource *resource,
1310 int32_t x, int32_t y, int32_t width, int32_t height)
1311{
1312 struct weston_region *region = resource->data;
1313
1314 pixman_region32_union_rect(&region->region, &region->region,
1315 x, y, width, height);
1316}
1317
1318static void
1319region_subtract(struct wl_client *client, struct wl_resource *resource,
1320 int32_t x, int32_t y, int32_t width, int32_t height)
1321{
1322 struct weston_region *region = resource->data;
1323 pixman_region32_t rect;
1324
1325 pixman_region32_init_rect(&rect, x, y, width, height);
1326 pixman_region32_subtract(&region->region, &region->region, &rect);
1327 pixman_region32_fini(&rect);
1328}
1329
1330static const struct wl_region_interface region_interface = {
1331 region_destroy,
1332 region_add,
1333 region_subtract
1334};
1335
1336static void
1337compositor_create_region(struct wl_client *client,
1338 struct wl_resource *resource, uint32_t id)
1339{
1340 struct weston_region *region;
1341
1342 region = malloc(sizeof *region);
1343 if (region == NULL) {
1344 wl_resource_post_no_memory(resource);
1345 return;
1346 }
1347
1348 region->resource.destroy = destroy_region;
1349
1350 region->resource.object.id = id;
1351 region->resource.object.interface = &wl_region_interface;
1352 region->resource.object.implementation =
1353 (void (**)(void)) &region_interface;
1354 region->resource.data = region;
1355
1356 pixman_region32_init(&region->region);
1357
1358 wl_client_add_resource(client, &region->resource);
1359}
1360
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001361const static struct wl_compositor_interface compositor_interface = {
1362 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001363 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001364};
1365
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001366WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001367weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001368{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001369 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1370 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001371
1372 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001373 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001374}
1375
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001376WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001377weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001378{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001379 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1380 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001381 } else {
1382 compositor->shell->unlock(compositor->shell);
1383 }
1384}
1385
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001386static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001387weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001388{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001389 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001390 compositor->idle_inhibit++;
1391}
1392
1393static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001394weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001395{
1396 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001397 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001398}
1399
1400static int
1401idle_handler(void *data)
1402{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001403 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001404
1405 if (compositor->idle_inhibit)
1406 return 1;
1407
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001408 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001409
1410 return 1;
1411}
1412
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001413static void
1414weston_input_update_drag_surface(struct wl_input_device *input_device,
1415 int dx, int dy);
1416
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001417WL_EXPORT void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001418notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001419{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001420 struct weston_output *output;
Scott Moreau447013d2012-02-18 05:05:29 -07001421 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001422 struct weston_input_device *wd = (struct weston_input_device *) device;
1423 struct weston_compositor *ec = wd->compositor;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001424 int x_valid = 0, y_valid = 0;
1425 int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001426
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001427 weston_compositor_activity(ec);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001428
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001429 wl_list_for_each(output, &ec->output_list, link) {
Scott Moreau2493be42012-02-22 13:57:51 -07001430 if (output->x <= x && x < output->x + output->current->width)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001431 x_valid = 1;
1432
Scott Moreau2493be42012-02-22 13:57:51 -07001433 if (output->y <= y && y < output->y + output->current->height)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001434 y_valid = 1;
1435
1436 /* FIXME: calculate this only on output addition/deletion */
1437 if (output->x < min_x)
1438 min_x = output->x;
1439 if (output->y < min_y)
1440 min_y = output->y;
1441
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001442 if (output->x + output->current->width > max_x)
Scott Moreau2493be42012-02-22 13:57:51 -07001443 max_x = output->x + output->current->width - 1;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001444 if (output->y + output->current->height > max_y)
Scott Moreau2493be42012-02-22 13:57:51 -07001445 max_y = output->y + output->current->height - 1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001446 }
1447
1448 if (!x_valid) {
1449 if (x < min_x)
1450 x = min_x;
1451 else if (x >= max_x)
1452 x = max_x;
1453 }
1454 if (!y_valid) {
1455 if (y < min_y)
1456 y = min_y;
1457 else if (y >= max_y)
1458 y = max_y;
1459 }
Ray Strode90e701d2008-12-18 23:05:43 -05001460
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001461 weston_input_update_drag_surface(device,
1462 x - device->x, y - device->y);
1463
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -05001464 device->x = x;
1465 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001466
Scott Moreauccbf29d2012-02-22 14:21:41 -07001467 wl_list_for_each(output, &ec->output_list, link)
1468 if (output->zoom.active &&
1469 pixman_region32_contains_point(&output->region, x, y, NULL))
1470 weston_output_update_zoom(output, x, y);
1471
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001472 weston_device_repick(device, time);
Scott Moreau447013d2012-02-18 05:05:29 -07001473 interface = device->pointer_grab->interface;
1474 interface->motion(device->pointer_grab, time,
1475 device->pointer_grab->x, device->pointer_grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001476
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001477 if (wd->sprite) {
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001478 weston_surface_set_position(wd->sprite,
1479 device->x - wd->hotspot_x,
1480 device->y - wd->hotspot_y);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001481
Pekka Paalanenf8c6aae2012-02-13 11:01:59 +02001482 weston_compositor_schedule_repaint(ec);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001483 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001484}
1485
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001486WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001487weston_surface_activate(struct weston_surface *surface,
1488 struct weston_input_device *device, uint32_t time)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001489{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001490 weston_surface_raise(surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001491 wl_input_device_set_keyboard_focus(&device->input_device,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001492 &surface->surface, time);
Kristian Høgsbergdade6492012-01-04 21:47:30 -05001493 wl_data_device_set_keyboard_focus(&device->input_device);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001494}
1495
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001496WL_EXPORT void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001497notify_button(struct wl_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001498 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001499{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001500 struct weston_input_device *wd = (struct weston_input_device *) device;
1501 struct weston_compositor *compositor = wd->compositor;
Kristian Høgsbergea081152010-12-07 08:59:51 -05001502
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001503 if (state) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001504 weston_compositor_idle_inhibit(compositor);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001505 if (device->button_count == 0) {
1506 device->grab_button = button;
1507 device->grab_time = time;
1508 device->grab_x = device->x;
1509 device->grab_y = device->y;
1510 }
1511 device->button_count++;
1512 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001513 weston_compositor_idle_release(compositor);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001514 device->button_count--;
1515 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001516
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001517 weston_compositor_run_binding(compositor, wd, time, 0, button, state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001518
Scott Moreau447013d2012-02-18 05:05:29 -07001519 device->pointer_grab->interface->button(device->pointer_grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001520
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001521}
1522
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001523static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001524update_modifier_state(struct weston_input_device *device,
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001525 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001526{
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001527 uint32_t modifier;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001528
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001529 switch (key) {
1530 case KEY_LEFTCTRL:
1531 case KEY_RIGHTCTRL:
1532 modifier = MODIFIER_CTRL;
1533 break;
1534
1535 case KEY_LEFTALT:
1536 case KEY_RIGHTALT:
1537 modifier = MODIFIER_ALT;
1538 break;
1539
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -04001540 case KEY_LEFTMETA:
1541 case KEY_RIGHTMETA:
1542 modifier = MODIFIER_SUPER;
1543 break;
1544
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001545 default:
1546 modifier = 0;
1547 break;
1548 }
1549
1550 if (state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001551 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001552 else
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001553 device->modifier_state &= ~modifier;
1554}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001555
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001556WL_EXPORT void
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001557notify_key(struct wl_input_device *device,
1558 uint32_t time, uint32_t key, uint32_t state)
1559{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001560 struct weston_input_device *wd = (struct weston_input_device *) device;
1561 struct weston_compositor *compositor = wd->compositor;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001562 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001563
Scott Moreauec286eb2012-02-18 05:05:30 -07001564 if (state) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001565 weston_compositor_idle_inhibit(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001566 device->grab_key = key;
1567 device->grab_time = time;
1568 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001569 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001570 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001571
Kristian Høgsbergafa264c2012-02-19 18:50:55 -05001572 if (device->keyboard_grab == &device->default_keyboard_grab)
1573 weston_compositor_run_binding(compositor, wd,
1574 time, key, 0, state);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001575
1576 update_modifier_state(wd, key, state);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001577 end = device->keys.data + device->keys.size;
1578 for (k = device->keys.data; k < end; k++) {
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001579 if (*k == key)
1580 *k = *--end;
1581 }
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001582 device->keys.size = (void *) end - device->keys.data;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001583 if (state) {
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001584 k = wl_array_add(&device->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001585 *k = key;
1586 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001587
Scott Moreauec286eb2012-02-18 05:05:30 -07001588 device->keyboard_grab->interface->key(device->keyboard_grab,
1589 time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001590}
1591
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001592WL_EXPORT void
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001593notify_pointer_focus(struct wl_input_device *device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001594 uint32_t time, struct weston_output *output,
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001595 int32_t x, int32_t y)
1596{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001597 struct weston_input_device *wd = (struct weston_input_device *) device;
1598 struct weston_compositor *compositor = wd->compositor;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001599
1600 if (output) {
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001601 weston_input_update_drag_surface(device, x - device->x,
1602 y - device->y);
1603
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001604 device->x = x;
1605 device->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001606 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001607 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001608 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001609 compositor->focus = 0;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001610 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001611 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001612}
1613
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001614WL_EXPORT void
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001615notify_keyboard_focus(struct wl_input_device *device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001616 uint32_t time, struct weston_output *output,
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001617 struct wl_array *keys)
1618{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001619 struct weston_input_device *wd =
1620 (struct weston_input_device *) device;
1621 struct weston_compositor *compositor = wd->compositor;
1622 struct weston_surface *es;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001623 uint32_t *k, *end;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001624
1625 if (!wl_list_empty(&compositor->surface_list))
1626 es = container_of(compositor->surface_list.next,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001627 struct weston_surface, link);
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001628 else
1629 es = NULL;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001630
1631 if (output) {
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001632 wl_array_copy(&wd->input_device.keys, keys);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001633 wd->modifier_state = 0;
1634 end = device->keys.data + device->keys.size;
1635 for (k = device->keys.data; k < end; k++) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001636 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001637 update_modifier_state(wd, *k, 1);
1638 }
1639
Kristian Høgsbergf59da392011-09-06 13:44:56 -04001640 if (es && es->surface.resource.client)
Benjamin Franzkeb7c00a42011-06-23 23:30:30 +02001641 wl_input_device_set_keyboard_focus(&wd->input_device,
1642 &es->surface, time);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001643 } else {
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001644 end = device->keys.data + device->keys.size;
1645 for (k = device->keys.data; k < end; k++)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001646 weston_compositor_idle_release(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001647
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001648 wd->modifier_state = 0;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001649 wl_input_device_set_keyboard_focus(&wd->input_device,
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001650 NULL, time);
1651 }
1652}
1653
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001654/* TODO: share this function with wayland-server.c */
1655static struct wl_resource *
1656find_resource_for_surface(struct wl_list *list, struct wl_surface *surface)
1657{
1658 struct wl_resource *r;
1659
1660 if (!surface)
1661 return NULL;
1662
1663 wl_list_for_each(r, list, link) {
1664 if (r->client == surface->resource.client)
1665 return r;
1666 }
1667
1668 return NULL;
1669}
1670
1671static void
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001672lose_touch_focus_resource(struct wl_listener *listener,
1673 struct wl_resource *resource, uint32_t time)
1674{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001675 struct weston_input_device *device =
1676 container_of(listener, struct weston_input_device,
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001677 touch_focus_resource_listener);
1678
1679 device->touch_focus_resource = NULL;
1680}
1681
1682static void
1683lose_touch_focus(struct wl_listener *listener,
1684 struct wl_resource *resource, uint32_t time)
1685{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001686 struct weston_input_device *device =
1687 container_of(listener, struct weston_input_device,
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001688 touch_focus_listener);
1689
1690 device->touch_focus = NULL;
1691}
1692
1693static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001694touch_set_focus(struct weston_input_device *device,
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001695 struct wl_surface *surface, uint32_t time)
1696{
1697 struct wl_input_device *input_device = &device->input_device;
1698 struct wl_resource *resource;
1699
1700 if (device->touch_focus == surface)
1701 return;
1702
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001703 if (surface) {
1704 resource =
1705 find_resource_for_surface(&input_device->resource_list,
1706 surface);
1707 if (!resource) {
1708 fprintf(stderr, "couldn't find resource\n");
1709 return;
1710 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001711
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001712 device->touch_focus_resource_listener.func =
1713 lose_touch_focus_resource;
1714 wl_list_insert(resource->destroy_listener_list.prev,
1715 &device->touch_focus_resource_listener.link);
1716 device->touch_focus_listener.func = lose_touch_focus;
1717 wl_list_insert(surface->resource.destroy_listener_list.prev,
1718 &device->touch_focus_listener.link);
1719
1720 device->touch_focus = surface;
1721 device->touch_focus_resource = resource;
1722 } else {
1723 if (device->touch_focus)
1724 wl_list_remove(&device->touch_focus_listener.link);
1725 if (device->touch_focus_resource)
1726 wl_list_remove(&device->touch_focus_resource_listener.link);
1727 device->touch_focus = NULL;
1728 device->touch_focus_resource = NULL;
1729 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001730}
1731
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001732/**
1733 * notify_touch - emulates button touches and notifies surfaces accordingly.
1734 *
1735 * It assumes always the correct cycle sequence until it gets here: touch_down
1736 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1737 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001738 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001739 */
1740WL_EXPORT void
1741notify_touch(struct wl_input_device *device, uint32_t time, int touch_id,
1742 int x, int y, int touch_type)
1743{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001744 struct weston_input_device *wd = (struct weston_input_device *) device;
1745 struct weston_compositor *ec = wd->compositor;
1746 struct weston_surface *es;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001747 int32_t sx, sy;
1748
1749 switch (touch_type) {
1750 case WL_INPUT_DEVICE_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001751 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001752
1753 wd->num_tp++;
1754
1755 /* the first finger down picks the surface, and all further go
1756 * to that surface for the remainder of the touch session i.e.
1757 * until all touch points are up again. */
1758 if (wd->num_tp == 1) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001759 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001760 touch_set_focus(wd, &es->surface, time);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001761 } else if (wd->touch_focus) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001762 es = (struct weston_surface *) wd->touch_focus;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001763 weston_surface_from_global(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001764 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001765
1766 if (wd->touch_focus_resource && wd->touch_focus)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001767 wl_resource_post_event(wd->touch_focus_resource,
1768 touch_type, time,
1769 wd->touch_focus,
1770 touch_id, sx, sy);
1771 break;
1772 case WL_INPUT_DEVICE_TOUCH_MOTION:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001773 es = (struct weston_surface *) wd->touch_focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001774 if (!es)
1775 break;
1776
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001777 weston_surface_from_global(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001778 if (wd->touch_focus_resource)
1779 wl_resource_post_event(wd->touch_focus_resource,
Kristian Høgsberg3543ff42011-12-21 17:25:54 -05001780 touch_type, time,
1781 touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001782 break;
1783 case WL_INPUT_DEVICE_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001784 weston_compositor_idle_release(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001785 wd->num_tp--;
1786
Kristian Høgsberg3543ff42011-12-21 17:25:54 -05001787 if (wd->touch_focus_resource)
1788 wl_resource_post_event(wd->touch_focus_resource,
1789 touch_type, time, touch_id);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001790 if (wd->num_tp == 0)
1791 touch_set_focus(wd, NULL, time);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001792 break;
1793 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001794}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001795
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001796static void
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001797input_device_attach(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001798 struct wl_resource *resource,
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001799 uint32_t time,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001800 struct wl_resource *buffer_resource, int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001801{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001802 struct weston_input_device *device = resource->data;
1803 struct weston_compositor *compositor = device->compositor;
Kristian Høgsberg57295eb2011-08-29 16:02:57 -04001804 struct wl_buffer *buffer;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001805
1806 if (time < device->input_device.pointer_focus_time)
1807 return;
1808 if (device->input_device.pointer_focus == NULL)
1809 return;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001810 if (device->input_device.pointer_focus->resource.client != client)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001811 return;
1812
Kristian Høgsbergaded1f62012-02-08 16:33:57 -05001813 if (!buffer_resource && device->sprite->output) {
1814 wl_list_remove(&device->sprite->link);
Kristian Høgsbergaded1f62012-02-08 16:33:57 -05001815 device->sprite->output = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001816 return;
1817 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001818
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001819 if (!device->sprite->output) {
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001820 wl_list_insert(&compositor->surface_list,
1821 &device->sprite->link);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001822 weston_surface_assign_output(device->sprite);
1823 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001824
1825 buffer = buffer_resource->data;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001826 device->hotspot_x = x;
1827 device->hotspot_y = y;
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001828 weston_surface_configure(device->sprite,
1829 device->input_device.x - device->hotspot_x,
1830 device->input_device.y - device->hotspot_y,
1831 buffer->width, buffer->height);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001832
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001833 weston_buffer_attach(buffer, &device->sprite->surface);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001834}
1835
1836const static struct wl_input_device_interface input_device_interface = {
1837 input_device_attach,
1838};
1839
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001840static void unbind_input_device(struct wl_resource *resource)
1841{
1842 wl_list_remove(&resource->link);
1843 free(resource);
1844}
1845
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001846static void
1847bind_input_device(struct wl_client *client,
1848 void *data, uint32_t version, uint32_t id)
1849{
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04001850 struct wl_input_device *device = data;
1851 struct wl_resource *resource;
1852
1853 resource = wl_client_add_object(client, &wl_input_device_interface,
1854 &input_device_interface, id, data);
1855 wl_list_insert(&device->resource_list, &resource->link);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001856 resource->destroy = unbind_input_device;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001857}
1858
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001859WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001860weston_input_device_init(struct weston_input_device *device,
1861 struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001862{
Kristian Høgsberga8873122011-11-23 10:39:34 -05001863 wl_input_device_init(&device->input_device);
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05001864
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001865 wl_display_add_global(ec->wl_display, &wl_input_device_interface,
1866 device, bind_input_device);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001867
Kristian Høgsbergaded1f62012-02-08 16:33:57 -05001868 device->sprite = weston_surface_create(ec);
Kristian Høgsberg8244b442011-06-23 15:44:14 -04001869
Kristian Høgsberga8873122011-11-23 10:39:34 -05001870 device->compositor = ec;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001871 device->hotspot_x = 16;
1872 device->hotspot_y = 16;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001873 device->modifier_state = 0;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001874 device->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001875
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001876 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001877}
1878
Pekka Paalanen07753fb2012-01-02 15:31:01 +02001879WL_EXPORT void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001880weston_input_device_release(struct weston_input_device *device)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02001881{
1882 wl_list_remove(&device->link);
1883 /* The global object is destroyed at wl_display_destroy() time. */
1884
1885 if (device->sprite)
1886 destroy_surface(&device->sprite->surface.resource);
1887
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001888 wl_input_device_release(&device->input_device);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02001889}
1890
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001891static void
1892weston_input_update_drag_surface(struct wl_input_device *input_device,
1893 int dx, int dy)
1894{
1895 int surface_changed = 0;
1896 struct weston_input_device *device = (struct weston_input_device *)
1897 input_device;
1898
1899 if (!device->drag_surface && !input_device->drag_surface)
1900 return;
1901
1902 if (device->drag_surface && input_device->drag_surface &&
1903 (&device->drag_surface->surface.resource !=
1904 &input_device->drag_surface->resource))
1905 /* between calls to this funcion we got a new drag_surface */
1906 surface_changed = 1;
1907
1908 if (!input_device->drag_surface || surface_changed) {
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001909 device->drag_surface = NULL;
1910 if (!surface_changed)
1911 return;
1912 }
1913
1914 if (!device->drag_surface || surface_changed) {
1915 device->drag_surface = (struct weston_surface *)
1916 input_device->drag_surface;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001917
1918 weston_surface_set_position(device->drag_surface,
1919 input_device->x, input_device->y);
1920 }
1921
1922 if (device->drag_surface->output == NULL &&
1923 device->drag_surface->buffer) {
1924 wl_list_insert(weston_compositor_top(device->compositor),
1925 &device->drag_surface->link);
Ander Conselvan de Oliveiraa2f11292012-02-23 13:29:26 +02001926 weston_surface_assign_output(device->drag_surface);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001927 }
1928
1929 if (!dx && !dy)
1930 return;
1931
1932 weston_surface_set_position(device->drag_surface,
1933 device->drag_surface->geometry.x + dx,
1934 device->drag_surface->geometry.y + dy);
1935}
1936
1937WL_EXPORT void
1938weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
1939{
1940 weston_input_update_drag_surface(compositor->input_device, 0, 0);
1941}
1942
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001943static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001944bind_output(struct wl_client *client,
1945 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001946{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001947 struct weston_output *output = data;
1948 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04001949 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001950
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04001951 resource = wl_client_add_object(client,
1952 &wl_output_interface, NULL, id, data);
1953
1954 wl_resource_post_event(resource,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001955 WL_OUTPUT_GEOMETRY,
1956 output->x,
1957 output->y,
1958 output->mm_width,
1959 output->mm_height,
1960 output->subpixel,
1961 output->make, output->model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001962
1963 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04001964 wl_resource_post_event(resource,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001965 WL_OUTPUT_MODE,
1966 mode->flags,
1967 mode->width,
1968 mode->height,
1969 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001970 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001971}
1972
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001973static const char vertex_shader[] =
1974 "uniform mat4 proj;\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001975 "attribute vec2 position;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001976 "attribute vec2 texcoord;\n"
1977 "varying vec2 v_texcoord;\n"
1978 "void main()\n"
1979 "{\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001980 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001981 " v_texcoord = texcoord;\n"
1982 "}\n";
1983
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001984static const char texture_fragment_shader[] =
Kristian Høgsbergdfce71d2010-12-07 20:19:10 -05001985 "precision mediump float;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001986 "varying vec2 v_texcoord;\n"
1987 "uniform sampler2D tex;\n"
Kristian Høgsberg541e5552011-12-14 09:24:11 -05001988 "uniform float alpha;\n"
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001989 "uniform float texwidth;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001990 "void main()\n"
1991 "{\n"
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001992 " if (v_texcoord.x < 0.0 || v_texcoord.x > texwidth ||\n"
1993 " v_texcoord.y < 0.0 || v_texcoord.y > 1.0)\n"
1994 " discard;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001995 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
Kristian Høgsberg541e5552011-12-14 09:24:11 -05001996 " gl_FragColor = alpha * gl_FragColor;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001997 "}\n";
1998
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001999static const char solid_fragment_shader[] =
2000 "precision mediump float;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002001 "uniform vec4 color;\n"
2002 "void main()\n"
2003 "{\n"
2004 " gl_FragColor = color\n;"
2005 "}\n";
2006
Kristian Høgsberga9468212010-06-14 21:03:11 -04002007static int
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002008compile_shader(GLenum type, const char *source)
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002009{
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002010 GLuint s;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002011 char msg[512];
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002012 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002013
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002014 s = glCreateShader(type);
2015 glShaderSource(s, 1, &source, NULL);
2016 glCompileShader(s);
2017 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002018 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002019 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
2020 fprintf(stderr, "shader info: %s\n", msg);
2021 return GL_NONE;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002022 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002023
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002024 return s;
2025}
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002026
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002027static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002028weston_shader_init(struct weston_shader *shader,
2029 const char *vertex_source, const char *fragment_source)
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002030{
2031 char msg[512];
2032 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002033
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002034 shader->vertex_shader =
2035 compile_shader(GL_VERTEX_SHADER, vertex_source);
2036 shader->fragment_shader =
2037 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
2038
2039 shader->program = glCreateProgram();
2040 glAttachShader(shader->program, shader->vertex_shader);
2041 glAttachShader(shader->program, shader->fragment_shader);
2042 glBindAttribLocation(shader->program, 0, "position");
2043 glBindAttribLocation(shader->program, 1, "texcoord");
2044
2045 glLinkProgram(shader->program);
2046 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002047 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002048 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002049 fprintf(stderr, "link info: %s\n", msg);
2050 return -1;
2051 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002052
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002053 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
2054 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
Kristian Høgsberg541e5552011-12-14 09:24:11 -05002055 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
Pekka Paalanenf55f5442012-02-02 16:49:05 +02002056 shader->color_uniform = glGetUniformLocation(shader->program, "color");
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02002057 shader->texwidth_uniform = glGetUniformLocation(shader->program,
2058 "texwidth");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002059
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002060 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002061}
2062
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002063WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002064weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002065{
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002066 pixman_region32_fini(&output->region);
Kristian Høgsberg8b72f602011-06-23 20:46:34 -04002067 pixman_region32_fini(&output->previous_damage);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002068}
2069
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002070WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002071weston_output_update_zoom(struct weston_output *output, int x, int y)
2072{
2073 float ratio;
2074
2075 if (output->zoom.level <= 0)
2076 return;
2077
2078 output->zoom.magnification = 1 / output->zoom.level;
2079 ratio = 1 - (1 / output->zoom.magnification);
2080
2081 output->zoom.trans_x = (((float)(x - output->x) / output->current->width) * (ratio * 2)) - ratio;
2082 output->zoom.trans_y = (((float)(y - output->y) / output->current->height) * (ratio * 2)) - ratio;
2083
2084 output->dirty = 1;
2085 weston_output_damage(output);
2086}
2087
2088WL_EXPORT void
2089weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002090{
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002091 int flip;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002092 struct weston_matrix camera;
2093 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002094
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002095 weston_matrix_init(&output->matrix);
2096 weston_matrix_translate(&output->matrix,
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002097 -(output->x + (output->border.right + output->current->width - output->border.left) / 2.0),
2098 -(output->y + (output->border.bottom + output->current->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002099
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002100 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002101 weston_matrix_scale(&output->matrix,
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002102 2.0 / (output->current->width + output->border.left + output->border.right),
2103 flip * 2.0 / (output->current->height + output->border.top + output->border.bottom), 1);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002104 if (output->zoom.active) {
2105 weston_matrix_init(&camera);
2106 weston_matrix_init(&modelview);
2107 weston_matrix_translate(&camera, output->zoom.trans_x, flip * output->zoom.trans_y, 0);
2108 weston_matrix_invert(&modelview, &camera);
2109 weston_matrix_scale(&modelview, output->zoom.magnification, output->zoom.magnification, 1.0);
2110 weston_matrix_multiply(&output->matrix, &modelview);
2111 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002112
Scott Moreauccbf29d2012-02-22 14:21:41 -07002113 output->dirty = 0;
2114}
2115
2116WL_EXPORT void
2117weston_output_move(struct weston_output *output, int x, int y)
2118{
2119 output->x = x;
2120 output->y = y;
2121
2122 pixman_region32_init(&output->previous_damage);
2123 pixman_region32_init_rect(&output->region, x, y,
2124 output->current->width,
2125 output->current->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002126}
2127
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002128WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002129weston_output_init(struct weston_output *output, struct weston_compositor *c,
2130 int x, int y, int width, int height, uint32_t flags)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002131{
2132 output->compositor = c;
2133 output->x = x;
2134 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002135 output->border.top = 0;
2136 output->border.bottom = 0;
2137 output->border.left = 0;
2138 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002139 output->mm_width = width;
2140 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002141 output->dirty = 1;
2142
2143 output->zoom.active = 0;
2144 output->zoom.increment = 0.05;
2145 output->zoom.level = 1.0;
2146 output->zoom.magnification = 1.0;
2147 output->zoom.trans_x = 0.0;
2148 output->zoom.trans_y = 0.0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002149
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002150 output->flags = flags;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002151 weston_output_move(output, x, y);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002152
Kristian Høgsberg33418202011-08-16 23:01:28 -04002153 wl_list_init(&output->frame_callback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002154
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002155 wl_display_add_global(c->wl_display,
2156 &wl_output_interface, output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002157}
2158
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002159static void
2160shm_buffer_created(struct wl_buffer *buffer)
2161{
2162 struct wl_list *surfaces_attached_to;
2163
2164 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
2165 if (!surfaces_attached_to) {
2166 buffer->user_data = NULL;
2167 return;
2168 }
2169
2170 wl_list_init(surfaces_attached_to);
2171
2172 buffer->user_data = surfaces_attached_to;
2173}
2174
2175static void
2176shm_buffer_damaged(struct wl_buffer *buffer,
2177 int32_t x, int32_t y, int32_t width, int32_t height)
2178{
2179 struct wl_list *surfaces_attached_to = buffer->user_data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002180 struct weston_surface *es;
Benjamin Franzkefab5ec12011-04-25 19:44:47 +02002181 GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002182
2183 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
2184 glBindTexture(GL_TEXTURE_2D, es->texture);
2185 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Benjamin Franzkefab5ec12011-04-25 19:44:47 +02002186 tex_width, buffer->height, 0,
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002187 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
2188 wl_shm_buffer_get_data(buffer));
2189 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
2190 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
2191 }
2192}
2193
2194static void
2195shm_buffer_destroyed(struct wl_buffer *buffer)
2196{
2197 struct wl_list *surfaces_attached_to = buffer->user_data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002198 struct weston_surface *es, *next;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002199
2200 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002201 wl_list_remove(&es->buffer_link);
Kristian Høgsbergfac11d22011-05-02 13:35:17 -04002202 wl_list_init(&es->buffer_link);
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002203 }
2204
2205 free(surfaces_attached_to);
2206}
2207
2208const static struct wl_shm_callbacks shm_callbacks = {
2209 shm_buffer_created,
2210 shm_buffer_damaged,
2211 shm_buffer_destroyed
2212};
2213
Kristian Høgsberga8873122011-11-23 10:39:34 -05002214static void
2215compositor_bind(struct wl_client *client,
2216 void *data, uint32_t version, uint32_t id)
2217{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002218 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002219
2220 wl_client_add_object(client, &wl_compositor_interface,
2221 &compositor_interface, id, compositor);
2222}
2223
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002224WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002225weston_compositor_init(struct weston_compositor *ec, struct wl_display *display)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002226{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002227 struct wl_event_loop *loop;
Kristian Høgsbergd711d0c2011-01-14 17:28:21 -05002228 const char *extensions;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002229
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002230 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002231
Kristian Høgsberga8873122011-11-23 10:39:34 -05002232 if (!wl_display_add_global(display, &wl_compositor_interface,
2233 ec, compositor_bind))
2234 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002235
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002236 ec->shm = wl_shm_init(display, &shm_callbacks);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002237
2238 ec->image_target_texture_2d =
2239 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2240 ec->image_target_renderbuffer_storage = (void *)
2241 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
2242 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2243 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2244 ec->bind_display =
2245 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
2246 ec->unbind_display =
2247 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
2248
2249 extensions = (const char *) glGetString(GL_EXTENSIONS);
2250 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
2251 fprintf(stderr,
2252 "GL_EXT_texture_format_BGRA8888 not available\n");
2253 return -1;
2254 }
2255
2256 extensions =
2257 (const char *) eglQueryString(ec->display, EGL_EXTENSIONS);
2258 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
2259 ec->has_bind_display = 1;
2260 if (ec->has_bind_display)
2261 ec->bind_display(ec->display, ec->wl_display);
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04002262
Kristian Høgsberg201a9042008-12-10 00:40:50 -05002263 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002264 wl_list_init(&ec->input_device_list);
2265 wl_list_init(&ec->output_list);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04002266 wl_list_init(&ec->binding_list);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002267 wl_list_init(&ec->animation_list);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002268 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002269 ec->fade.animation.frame = fade_frame;
2270 wl_list_init(&ec->fade.animation.link);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04002271
Pekka Paalanen35ce06d2012-01-03 11:39:13 +02002272 ec->screenshooter = screenshooter_create(ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002273
Kristian Høgsbergdade6492012-01-04 21:47:30 -05002274 wl_data_device_manager_init(ec->wl_display);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002275
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002276 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002277
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002278 if (weston_shader_init(&ec->texture_shader,
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002279 vertex_shader, texture_fragment_shader) < 0)
Kristian Høgsberga9468212010-06-14 21:03:11 -04002280 return -1;
Pekka Paalanenf55f5442012-02-02 16:49:05 +02002281 if (weston_shader_init(&ec->solid_shader,
2282 vertex_shader, solid_fragment_shader) < 0)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002283 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05002284
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002285 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002286 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Pekka Paalanen7296e792011-12-07 16:22:00 +02002287 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002288
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002289 weston_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002290
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002291 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002292}
2293
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002294WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002295weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002296{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002297 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002298
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002299 wl_event_source_remove(ec->idle_source);
2300
Pekka Paalanen35ce06d2012-01-03 11:39:13 +02002301 if (ec->screenshooter)
2302 screenshooter_destroy(ec->screenshooter);
2303
Matt Roper361d2ad2011-08-29 13:52:23 -07002304 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002305 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002306 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002307
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05002308 weston_binding_list_destroy_all(&ec->binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002309
2310 wl_shm_finish(ec->shm);
2311
2312 wl_array_release(&ec->vertices);
2313 wl_array_release(&ec->indices);
Matt Roper361d2ad2011-08-29 13:52:23 -07002314}
2315
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002316static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002317{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002318 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002319
Kristian Høgsberg3cad4362011-07-15 21:09:24 -04002320 fprintf(stderr, "caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002321 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002322
2323 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002324}
2325
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002326static void
2327on_segv_signal(int s, siginfo_t *siginfo, void *context)
2328{
2329 void *buffer[32];
2330 int i, count;
2331 Dl_info info;
2332
2333 fprintf(stderr, "caught segv\n");
2334
2335 count = backtrace(buffer, ARRAY_LENGTH(buffer));
2336 for (i = 0; i < count; i++) {
2337 dladdr(buffer[i], &info);
2338 fprintf(stderr, " [%016lx] %s (%s)\n",
2339 (long) buffer[i],
2340 info.dli_sname ? info.dli_sname : "--",
2341 info.dli_fname);
2342 }
2343
2344 longjmp(segv_jmp_buf, 1);
2345}
2346
2347
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002348static void *
2349load_module(const char *name, const char *entrypoint, void **handle)
2350{
2351 char path[PATH_MAX];
2352 void *module, *init;
2353
2354 if (name[0] != '/')
2355 snprintf(path, sizeof path, MODULEDIR "/%s", name);
2356 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02002357 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002358
2359 module = dlopen(path, RTLD_LAZY);
2360 if (!module) {
2361 fprintf(stderr,
2362 "failed to load module: %s\n", dlerror());
2363 return NULL;
2364 }
2365
2366 init = dlsym(module, entrypoint);
2367 if (!init) {
2368 fprintf(stderr,
2369 "failed to lookup init function: %s\n", dlerror());
2370 return NULL;
2371 }
2372
2373 return init;
2374}
2375
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002376int main(int argc, char *argv[])
2377{
2378 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002379 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02002380 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002381 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002382 struct sigaction segv_action;
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002383 int o, xserver = 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002384 void *shell_module, *backend_module;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002385 int (*shell_init)(struct weston_compositor *ec);
2386 struct weston_compositor
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002387 *(*backend_init)(struct wl_display *display, char *options);
2388 char *backend = NULL;
2389 char *backend_options = "";
2390 char *shell = NULL;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002391 char *p;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002392 int option_idle_time = 300;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02002393 int i;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002394
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002395 static const char opts[] = "B:b:o:S:i:s:x";
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002396 static const struct option longopts[ ] = {
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002397 { "backend", 1, NULL, 'B' },
2398 { "backend-options", 1, NULL, 'o' },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002399 { "socket", 1, NULL, 'S' },
2400 { "idle-time", 1, NULL, 'i' },
2401 { "shell", 1, NULL, 's' },
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002402 { "xserver", 0, NULL, 'x' },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002403 { NULL, }
2404 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05002405
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002406 while (o = getopt_long(argc, argv, opts, longopts, &o), o > 0) {
2407 switch (o) {
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002408 case 'B':
2409 backend = optarg;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002410 break;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002411 case 'o':
2412 backend_options = optarg;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002413 break;
2414 case 'S':
2415 option_socket_name = optarg;
2416 break;
2417 case 'i':
2418 option_idle_time = strtol(optarg, &p, 0);
2419 if (*p != '\0') {
2420 fprintf(stderr,
2421 "invalid idle time option: %s\n",
2422 optarg);
2423 exit(EXIT_FAILURE);
2424 }
2425 break;
2426 case 's':
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002427 shell = optarg;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002428 break;
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002429 case 'x':
2430 xserver = 1;
2431 break;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002432 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04002433 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002434
2435 display = wl_display_create();
2436
Tiago Vignatti2116b892011-08-08 05:52:59 -07002437 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02002438 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
2439 display);
2440 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
2441 display);
2442 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
2443 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07002444
2445 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02002446 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
2447 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05002448
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002449 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
2450 segv_action.sa_sigaction = on_segv_signal;
Pekka Paalanen8423a892012-01-19 16:48:37 +02002451 sigemptyset(&segv_action.sa_mask);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002452 sigaction(SIGSEGV, &segv_action, NULL);
2453
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002454 if (!backend) {
2455 if (getenv("WAYLAND_DISPLAY"))
2456 backend = "wayland-backend.so";
2457 else if (getenv("DISPLAY"))
2458 backend = "x11-backend.so";
2459 else if (getenv("OPENWFD"))
2460 backend = "openwfd-backend.so";
2461 else
2462 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002463 }
2464
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002465 if (!shell)
2466 shell = "desktop-shell.so";
Kristian Høgsberga9410222011-01-14 17:22:35 -05002467
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002468 backend_init = load_module(backend, "backend_init", &backend_module);
2469 if (!backend_init)
2470 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05002471
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002472 shell_init = load_module(shell, "shell_init", &shell_module);
2473 if (!shell_init)
2474 exit(EXIT_FAILURE);
Benjamin Franzke5d007092011-04-04 00:30:25 +02002475
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002476 ec = backend_init(display, backend_options);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05002477 if (ec == NULL) {
2478 fprintf(stderr, "failed to create compositor\n");
2479 exit(EXIT_FAILURE);
2480 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04002481
Pekka Paalanen7296e792011-12-07 16:22:00 +02002482 ec->option_idle_time = option_idle_time;
2483 ec->idle_time = option_idle_time;
2484
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002485 if (xserver)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002486 weston_xserver_init(ec);
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002487
Kristian Høgsberg1760ebb2012-01-15 16:04:33 -05002488 if (shell_init(ec) < 0)
2489 exit(EXIT_FAILURE);
2490
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002491 if (wl_display_add_socket(display, option_socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002492 fprintf(stderr, "failed to add socket: %m\n");
2493 exit(EXIT_FAILURE);
2494 }
2495
Pekka Paalanenc0444e32012-01-05 16:28:21 +02002496 weston_compositor_wake(ec);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002497 if (setjmp(segv_jmp_buf) == 0)
2498 wl_display_run(display);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002499
Pekka Paalanen0135abe2012-01-03 10:01:20 +02002500 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05002501 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02002502
Kristian Høgsberg3cad4362011-07-15 21:09:24 -04002503 if (xserver)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002504 weston_xserver_destroy(ec);
Kristian Høgsberg3cad4362011-07-15 21:09:24 -04002505
Pekka Paalanen3c647232011-12-22 13:43:43 +02002506 ec->shell->destroy(ec->shell);
2507
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002508 if (ec->has_bind_display)
2509 ec->unbind_display(ec->display, display);
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002510
Pekka Paalanen51c769f2012-01-02 16:03:26 +02002511 for (i = ARRAY_LENGTH(signals); i;)
2512 wl_event_source_remove(signals[--i]);
2513
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002514 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02002515 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002516
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002517 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002518}