blob: 3b1ca1ed1150c6b3cb59d763e23226c7e93549fa [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanend581a8f2012-01-27 16:25:16 +02004 * Copyright © 2012 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04006 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050015 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040016 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Kristian Høgsberg06bc2642010-12-01 09:50:16 -050025#define _GNU_SOURCE
26
Kristian Høgsberga9410222011-01-14 17:22:35 -050027#include "config.h"
28
Daniel Stoneb7452fe2012-06-01 12:14:06 +010029#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040030#include <stdio.h>
31#include <string.h>
32#include <stdlib.h>
33#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010034#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050035#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020036#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040037#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010038#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040039#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020040#include <sys/socket.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040041#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050042#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040043#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040044#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040045#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050046#include <setjmp.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050047
Pekka Paalanen50719bc2011-11-22 14:18:50 +020048#include <wayland-server.h>
Kristian Høgsberg82863022010-06-04 21:52:02 -040049#include "compositor.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030050#include "../shared/os-compatibility.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050051
Kristian Høgsberg27da5382011-06-21 17:32:25 -040052static struct wl_list child_process_list;
Kristian Høgsberg0690da62012-01-16 11:53:54 -050053static jmp_buf segv_jmp_buf;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040054
55static int
56sigchld_handler(int signal_number, void *data)
57{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050058 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040059 int status;
60 pid_t pid;
61
Bill Spitzak027b9622012-03-17 15:22:03 -070062 pid = waitpid(-1, &status, WNOHANG);
63 if (!pid)
64 return 1;
65
Kristian Høgsberg27da5382011-06-21 17:32:25 -040066 wl_list_for_each(p, &child_process_list, link) {
67 if (p->pid == pid)
68 break;
69 }
70
71 if (&p->link == &child_process_list) {
72 fprintf(stderr, "unknown child process exited\n");
73 return 1;
74 }
75
76 wl_list_remove(&p->link);
77 p->cleanup(p, status);
78
79 return 1;
80}
81
Alex Wu2dda6042012-04-17 17:20:47 +080082WL_EXPORT int
83weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode)
84{
85 if (!output->switch_mode)
86 return -1;
87
88 return output->switch_mode(output, mode);
89}
90
Kristian Høgsberg27da5382011-06-21 17:32:25 -040091WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050092weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -040093{
94 wl_list_insert(&child_process_list, &process->link);
95}
96
Benjamin Franzke06286262011-05-06 19:12:33 +020097static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020098child_client_exec(int sockfd, const char *path)
99{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500100 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200101 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200102 sigset_t allsigs;
103
104 /* do not give our signal mask to the new process */
105 sigfillset(&allsigs);
106 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200107
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500108 /* Launch clients as the user. */
109 seteuid(getuid());
110
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500111 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
112 * non-CLOEXEC fd to pass through exec. */
113 clientfd = dup(sockfd);
114 if (clientfd == -1) {
115 fprintf(stderr, "compositor: dup failed: %m\n");
116 return;
117 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200118
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500119 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200120 setenv("WAYLAND_SOCKET", s, 1);
121
122 if (execl(path, path, NULL) < 0)
123 fprintf(stderr, "compositor: executing '%s' failed: %m\n",
124 path);
125}
126
127WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500128weston_client_launch(struct weston_compositor *compositor,
129 struct weston_process *proc,
130 const char *path,
131 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200132{
133 int sv[2];
134 pid_t pid;
135 struct wl_client *client;
136
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300137 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500138 fprintf(stderr, "weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200139 "socketpair failed while launching '%s': %m\n",
140 path);
141 return NULL;
142 }
143
144 pid = fork();
145 if (pid == -1) {
146 close(sv[0]);
147 close(sv[1]);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500148 fprintf(stderr, "weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200149 "fork failed while launching '%s': %m\n", path);
150 return NULL;
151 }
152
153 if (pid == 0) {
154 child_client_exec(sv[1], path);
155 exit(-1);
156 }
157
158 close(sv[1]);
159
160 client = wl_client_create(compositor->wl_display, sv[0]);
161 if (!client) {
162 close(sv[0]);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500163 fprintf(stderr, "weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200164 "wl_client_create failed while launching '%s'.\n",
165 path);
166 return NULL;
167 }
168
169 proc->pid = pid;
170 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500171 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200172
173 return client;
174}
175
176static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400177surface_handle_buffer_destroy(struct wl_listener *listener, void *data)
Benjamin Franzke06286262011-05-06 19:12:33 +0200178{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500179 struct weston_surface *es =
180 container_of(listener, struct weston_surface,
181 buffer_destroy_listener);
Benjamin Franzke06286262011-05-06 19:12:33 +0200182
Kristian Høgsberg24596942011-10-24 17:51:02 -0400183 es->buffer = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200184}
185
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500186static const pixman_region32_data_t undef_region_data;
187
188static void
189undef_region(pixman_region32_t *region)
190{
Kristian Høgsberg66a099b2012-05-29 16:49:45 -0400191 if (region->data != &undef_region_data)
192 pixman_region32_fini(region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500193 region->data = (pixman_region32_data_t *) &undef_region_data;
194}
195
196static int
197region_is_undefined(pixman_region32_t *region)
198{
199 return region->data == &undef_region_data;
200}
201
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200202static void
203empty_region(pixman_region32_t *region)
204{
205 if (!region_is_undefined(region))
206 pixman_region32_fini(region);
207
208 pixman_region32_init(region);
209}
210
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500211WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500212weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500213{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500214 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400215
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200216 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400217 if (surface == NULL)
218 return NULL;
219
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400220 wl_signal_init(&surface->surface.resource.destroy_signal);
Kristian Høgsberg48891542012-02-23 17:38:33 -0500221
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500222 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500223 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500224
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400225 surface->surface.resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400226
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500227 surface->compositor = compositor;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200228 surface->image = EGL_NO_IMAGE_KHR;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400229 surface->alpha = 1.0;
Kristian Høgsbergd6ad1222012-05-17 11:11:15 -0400230 surface->blend = 1;
Kristian Høgsberg0b0c2bb2012-05-15 23:16:53 -0400231 surface->opaque_rect[0] = 0.0;
232 surface->opaque_rect[1] = 0.0;
233 surface->opaque_rect[2] = 0.0;
234 surface->opaque_rect[3] = 0.0;
Juan Zhao46436612012-03-20 01:48:46 +0800235 surface->pitch = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400236
Benjamin Franzke06286262011-05-06 19:12:33 +0200237 surface->buffer = NULL;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400238 surface->output = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200239
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400240 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500241 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500242 pixman_region32_init(&surface->clip);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500243 undef_region(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200244 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500245 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400246
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400247 surface->buffer_destroy_listener.notify =
248 surface_handle_buffer_destroy;
Benjamin Franzke06286262011-05-06 19:12:33 +0200249
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200250 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200251 wl_list_insert(&surface->geometry.transformation_list,
252 &surface->transform.position.link);
253 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200254 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200255 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500256
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400257 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500258}
259
Alex Wu8811bf92012-02-28 18:07:54 +0800260WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500261weston_surface_set_color(struct weston_surface *surface,
262 GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
263{
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500264 surface->color[0] = red;
265 surface->color[1] = green;
266 surface->color[2] = blue;
267 surface->color[3] = alpha;
268 surface->shader = &surface->compositor->solid_shader;
269}
270
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200271static void
Pekka Paalanenece8a012012-02-08 15:23:15 +0200272surface_to_global_float(struct weston_surface *surface,
Daniel Stonebd3489b2012-05-08 17:17:53 +0100273 GLfloat sx, GLfloat sy, GLfloat *x, GLfloat *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200274{
275 if (surface->transform.enabled) {
276 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
277
278 weston_matrix_transform(&surface->transform.matrix, &v);
279
280 if (fabsf(v.f[3]) < 1e-6) {
281 fprintf(stderr, "warning: numerical instability in "
282 "weston_surface_to_global(), divisor = %g\n",
283 v.f[3]);
284 *x = 0;
285 *y = 0;
286 return;
287 }
288
289 *x = v.f[0] / v.f[3];
290 *y = v.f[1] / v.f[3];
291 } else {
292 *x = sx + surface->geometry.x;
293 *y = sy + surface->geometry.y;
294 }
295}
296
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500297WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500298weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200299{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500300 struct weston_compositor *compositor = surface->compositor;
301 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200302
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500303 pixman_region32_init(&damage);
304 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
305 &surface->clip);
306 pixman_region32_union(&compositor->damage,
307 &compositor->damage, &damage);
308 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200309}
310
311static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200312surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
313 int32_t width, int32_t height,
314 pixman_region32_t *bbox)
315{
Pekka Paalanen219b9822012-02-08 15:38:37 +0200316 GLfloat min_x = HUGE_VALF, min_y = HUGE_VALF;
317 GLfloat max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200318 int32_t s[4][2] = {
319 { sx, sy },
320 { sx, sy + height },
321 { sx + width, sy },
322 { sx + width, sy + height }
323 };
Pekka Paalanen219b9822012-02-08 15:38:37 +0200324 GLfloat int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200325 int i;
326
327 for (i = 0; i < 4; ++i) {
Pekka Paalanen219b9822012-02-08 15:38:37 +0200328 GLfloat x, y;
329 surface_to_global_float(surface, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200330 if (x < min_x)
331 min_x = x;
332 if (x > max_x)
333 max_x = x;
334 if (y < min_y)
335 min_y = y;
336 if (y > max_y)
337 max_y = y;
338 }
339
Pekka Paalanen219b9822012-02-08 15:38:37 +0200340 int_x = floorf(min_x);
341 int_y = floorf(min_y);
342 pixman_region32_init_rect(bbox, int_x, int_y,
343 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200344}
345
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200346static void
347weston_surface_update_transform_disable(struct weston_surface *surface)
348{
349 surface->transform.enabled = 0;
350
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200351 /* round off fractions when not transformed */
352 surface->geometry.x = roundf(surface->geometry.x);
353 surface->geometry.y = roundf(surface->geometry.y);
354
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200355 pixman_region32_init_rect(&surface->transform.boundingbox,
356 surface->geometry.x,
357 surface->geometry.y,
358 surface->geometry.width,
359 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500360
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400361 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500362 pixman_region32_copy(&surface->transform.opaque,
363 &surface->opaque);
364 pixman_region32_translate(&surface->transform.opaque,
365 surface->geometry.x,
366 surface->geometry.y);
367 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200368}
369
370static int
371weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200372{
373 struct weston_matrix *matrix = &surface->transform.matrix;
374 struct weston_matrix *inverse = &surface->transform.inverse;
375 struct weston_transform *tform;
376
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200377 surface->transform.enabled = 1;
378
379 /* Otherwise identity matrix, but with x and y translation. */
380 surface->transform.position.matrix.d[12] = surface->geometry.x;
381 surface->transform.position.matrix.d[13] = surface->geometry.y;
382
383 weston_matrix_init(matrix);
384 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
385 weston_matrix_multiply(matrix, &tform->matrix);
386
387 if (weston_matrix_invert(inverse, matrix) < 0) {
388 /* Oops, bad total transformation, not invertible */
389 fprintf(stderr, "error: weston_surface %p"
390 " transformation not invertible.\n", surface);
391 return -1;
392 }
393
394 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
395 surface->geometry.height,
396 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500397
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200398 return 0;
399}
400
401WL_EXPORT void
402weston_surface_update_transform(struct weston_surface *surface)
403{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200404 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200405 return;
406
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200407 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200408
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500409 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200410
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200411 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500412 pixman_region32_fini(&surface->transform.opaque);
413 pixman_region32_init(&surface->transform.opaque);
414
415 if (region_is_undefined(&surface->input))
416 pixman_region32_init_rect(&surface->input, 0, 0,
417 surface->geometry.width,
418 surface->geometry.height);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200419
Pekka Paalanencd403622012-01-25 13:37:39 +0200420 /* transform.position is always in transformation_list */
421 if (surface->geometry.transformation_list.next ==
422 &surface->transform.position.link &&
423 surface->geometry.transformation_list.prev ==
424 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200425 weston_surface_update_transform_disable(surface);
426 } else {
427 if (weston_surface_update_transform_enable(surface) < 0)
428 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200429 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200430
431 /* weston_surface_damage() without update */
432 pixman_region32_union(&surface->damage, &surface->damage,
433 &surface->transform.boundingbox);
434
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300435 if (weston_surface_is_mapped(surface))
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +0200436 weston_surface_assign_output(surface);
437
Pekka Paalanen96516782012-02-09 15:32:15 +0200438 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200439}
440
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200441WL_EXPORT void
Ander Conselvan de Oliveirafbf28942012-03-27 17:36:37 +0300442weston_surface_to_global_float(struct weston_surface *surface,
Daniel Stonebd3489b2012-05-08 17:17:53 +0100443 GLfloat sx, GLfloat sy, GLfloat *x, GLfloat *y)
Ander Conselvan de Oliveirafbf28942012-03-27 17:36:37 +0300444{
Ander Conselvan de Oliveirafbf28942012-03-27 17:36:37 +0300445 surface_to_global_float(surface, sx, sy, x, y);
446}
447
448WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100449weston_surface_to_global_fixed(struct weston_surface *surface,
450 wl_fixed_t sx, wl_fixed_t sy,
451 wl_fixed_t *x, wl_fixed_t *y)
452{
453 GLfloat xf, yf;
454
455 weston_surface_to_global_float(surface,
456 wl_fixed_to_double(sx),
457 wl_fixed_to_double(sy),
458 &xf, &yf);
459 *x = wl_fixed_from_double(xf);
460 *y = wl_fixed_from_double(yf);
461}
462
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200463static void
464surface_from_global_float(struct weston_surface *surface,
Daniel Stonebd3489b2012-05-08 17:17:53 +0100465 GLfloat x, GLfloat y, GLfloat *sx, GLfloat *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200466{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200467 if (surface->transform.enabled) {
468 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
469
470 weston_matrix_transform(&surface->transform.inverse, &v);
471
472 if (fabsf(v.f[3]) < 1e-6) {
473 fprintf(stderr, "warning: numerical instability in "
474 "weston_surface_from_global(), divisor = %g\n",
475 v.f[3]);
476 *sx = 0;
477 *sy = 0;
478 return;
479 }
480
Pekka Paalanencd403622012-01-25 13:37:39 +0200481 *sx = v.f[0] / v.f[3];
482 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200483 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200484 *sx = x - surface->geometry.x;
485 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200486 }
487}
488
489WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100490weston_surface_from_global_fixed(struct weston_surface *surface,
491 wl_fixed_t x, wl_fixed_t y,
492 wl_fixed_t *sx, wl_fixed_t *sy)
493{
494 GLfloat sxf, syf;
495
Daniel Stonebd3489b2012-05-08 17:17:53 +0100496 surface_from_global_float(surface,
497 wl_fixed_to_double(x),
498 wl_fixed_to_double(y),
499 &sxf, &syf);
500 *sx = wl_fixed_from_double(sxf);
501 *sy = wl_fixed_from_double(syf);
502}
503
504WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200505weston_surface_from_global(struct weston_surface *surface,
506 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
507{
508 GLfloat sxf, syf;
509
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200510 surface_from_global_float(surface, x, y, &sxf, &syf);
511 *sx = floorf(sxf);
512 *sy = floorf(syf);
513}
514
Tiago Vignatti9d393522012-02-10 16:26:19 +0200515static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500516weston_surface_damage_rectangle(struct weston_surface *surface,
Pekka Paalanen2267d452012-01-26 13:12:45 +0200517 int32_t sx, int32_t sy,
518 int32_t width, int32_t height)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500519{
Pekka Paalanen2267d452012-01-26 13:12:45 +0200520 if (surface->transform.enabled) {
521 pixman_region32_t box;
522 surface_compute_bbox(surface, sx, sy, width, height, &box);
523 pixman_region32_union(&surface->damage, &surface->damage,
524 &box);
525 pixman_region32_fini(&box);
526 } else {
Pekka Paalanen2267d452012-01-26 13:12:45 +0200527 pixman_region32_union_rect(&surface->damage, &surface->damage,
Pekka Paalanen1d5035c2012-02-09 15:27:45 +0200528 surface->geometry.x + sx,
529 surface->geometry.y + sy,
530 width, height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200531 }
532
533 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500534}
535
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400536WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500537weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500538{
Pekka Paalanen2267d452012-01-26 13:12:45 +0200539 pixman_region32_union(&surface->damage, &surface->damage,
540 &surface->transform.boundingbox);
541
542 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500543}
544
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400545WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500546weston_surface_configure(struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200547 GLfloat x, GLfloat y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400548{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200549 surface->geometry.x = x;
550 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200551 surface->geometry.width = width;
552 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200553 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400554}
555
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200556WL_EXPORT void
557weston_surface_set_position(struct weston_surface *surface,
558 GLfloat x, GLfloat y)
559{
560 surface->geometry.x = x;
561 surface->geometry.y = y;
562 surface->geometry.dirty = 1;
563}
564
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300565WL_EXPORT int
566weston_surface_is_mapped(struct weston_surface *surface)
567{
568 if (surface->output)
569 return 1;
570 else
571 return 0;
572}
573
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400574WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500575weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500576{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400577 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500578
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400579 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500580
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400581 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500582}
583
Tiago Vignatti9d393522012-02-10 16:26:19 +0200584static struct weston_surface *
585weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100586 wl_fixed_t x, wl_fixed_t y,
587 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200588{
589 struct weston_surface *surface;
590
591 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100592 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500593 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100594 wl_fixed_to_int(*sx),
595 wl_fixed_to_int(*sy),
596 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200597 return surface;
598 }
599
600 return NULL;
601}
602
603static void
Daniel Stone37816df2012-05-16 18:45:18 +0100604weston_device_repick(struct wl_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500605{
Daniel Stone37816df2012-05-16 18:45:18 +0100606 struct weston_seat *ws = (struct weston_seat *) seat;
Scott Moreau447013d2012-02-18 05:05:29 -0700607 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500608 struct weston_surface *surface, *focus;
609
Daniel Stonee9e92d22012-06-04 11:40:47 +0100610 if (!seat->pointer)
611 return;
612
Daniel Stone37816df2012-05-16 18:45:18 +0100613 surface = weston_compositor_pick_surface(ws->compositor,
614 seat->pointer->x,
615 seat->pointer->y,
616 &seat->pointer->current_x,
617 &seat->pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500618
Daniel Stone37816df2012-05-16 18:45:18 +0100619 if (&surface->surface != seat->pointer->current) {
620 interface = seat->pointer->grab->interface;
Kristian Høgsberg4e99ac42012-06-04 16:12:38 -0400621 seat->pointer->current = &surface->surface;
Daniel Stone37816df2012-05-16 18:45:18 +0100622 interface->focus(seat->pointer->grab, &surface->surface,
623 seat->pointer->current_x,
624 seat->pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500625 }
626
Daniel Stone37816df2012-05-16 18:45:18 +0100627 focus = (struct weston_surface *) seat->pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500628 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100629 weston_surface_from_global_fixed(focus,
630 seat->pointer->x,
631 seat->pointer->y,
632 &seat->pointer->grab->x,
633 &seat->pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500634}
635
636WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500637weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400638{
Daniel Stone37816df2012-05-16 18:45:18 +0100639 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400640
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500641 if (!compositor->focus)
642 return;
643
Daniel Stone37816df2012-05-16 18:45:18 +0100644 wl_list_for_each(seat, &compositor->seat_list, link)
645 weston_device_repick(&seat->seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400646}
647
648static void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500649weston_surface_unmap(struct weston_surface *surface)
650{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100651 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500652
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500653 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500654 surface->output = NULL;
655 wl_list_remove(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500656 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500657
Daniel Stone4dab5db2012-05-30 16:31:53 +0100658 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100659 if (seat->seat.keyboard &&
660 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100661 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100662 if (seat->seat.pointer &&
663 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100664 wl_pointer_set_focus(seat->seat.pointer,
665 NULL,
666 wl_fixed_from_int(0),
667 wl_fixed_from_int(0));
668 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500669
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500670 weston_compositor_schedule_repaint(surface->compositor);
671}
672
673static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400674destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500675{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500676 struct weston_surface *surface =
677 container_of(resource,
678 struct weston_surface, surface.resource);
679 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400680
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300681 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500682 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400683
Kristian Høgsberg12bbf812012-02-17 12:15:27 -0500684 if (surface->texture)
685 glDeleteTextures(1, &surface->texture);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200686
Benjamin Franzke06286262011-05-06 19:12:33 +0200687 if (surface->buffer)
688 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400689
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200690 if (surface->image != EGL_NO_IMAGE_KHR)
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400691 compositor->destroy_image(compositor->display,
692 surface->image);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200693
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200694 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200695 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500696 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500697 pixman_region32_fini(&surface->clip);
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500698 if (!region_is_undefined(&surface->input))
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500699 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200700
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400701 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500702}
703
Alex Wu8811bf92012-02-28 18:07:54 +0800704WL_EXPORT void
705weston_surface_destroy(struct weston_surface *surface)
706{
707 /* Not a valid way to destroy a client surface */
708 assert(surface->surface.resource.client == NULL);
709
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400710 wl_signal_emit(&surface->surface.resource.destroy_signal,
711 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800712 destroy_surface(&surface->surface.resource);
713}
714
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100715static void
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -0400716weston_surface_attach(struct wl_surface *surface, struct wl_buffer *buffer)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100717{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500718 struct weston_surface *es = (struct weston_surface *) surface;
719 struct weston_compositor *ec = es->compositor;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100720
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300721 if (es->buffer) {
722 weston_buffer_post_release(es->buffer);
723 wl_list_remove(&es->buffer_destroy_listener.link);
724 }
725
726 es->buffer = buffer;
727
728 if (!buffer) {
729 if (weston_surface_is_mapped(es))
730 weston_surface_unmap(es);
731 return;
732 }
733
734 buffer->busy_count++;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400735 wl_signal_add(&es->buffer->resource.destroy_signal,
736 &es->buffer_destroy_listener);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300737
738 if (es->geometry.width != buffer->width ||
739 es->geometry.height != buffer->height) {
740 undef_region(&es->input);
741 pixman_region32_fini(&es->opaque);
742 pixman_region32_init(&es->opaque);
743 }
744
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500745 if (!es->texture) {
746 glGenTextures(1, &es->texture);
747 glBindTexture(GL_TEXTURE_2D, es->texture);
748 glTexParameteri(GL_TEXTURE_2D,
749 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
750 glTexParameteri(GL_TEXTURE_2D,
751 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
752 es->shader = &ec->texture_shader;
753 } else {
754 glBindTexture(GL_TEXTURE_2D, es->texture);
755 }
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100756
757 if (wl_buffer_is_shm(buffer)) {
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200758 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
Kristian Høgsbergfb6de222012-03-27 17:10:13 -0400759 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
760 es->pitch, es->buffer->height, 0,
761 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
Kristian Høgsbergd6ad1222012-05-17 11:11:15 -0400762 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
763 es->blend = 0;
764 else
765 es->blend = 1;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100766 } else {
Benjamin Franzkefb4b5a22011-06-21 10:44:37 +0200767 if (es->image != EGL_NO_IMAGE_KHR)
768 ec->destroy_image(ec->display, es->image);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400769 es->image = ec->create_image(ec->display, NULL,
770 EGL_WAYLAND_BUFFER_WL,
771 buffer, NULL);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300772
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400773 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400774
Ander Conselvan de Oliveira9390ae32012-03-27 17:36:38 +0300775 es->pitch = buffer->width;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100776 }
777}
778
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400779static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500780texture_region(struct weston_surface *es, pixman_region32_t *region)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500781{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500782 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500783 GLfloat *v, inv_width, inv_height;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200784 GLfloat sx, sy;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500785 pixman_box32_t *rectangles;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400786 unsigned int *p;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500787 int i, n;
788
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400789 rectangles = pixman_region32_rectangles(region, &n);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500790 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
791 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200792 inv_width = 1.0 / es->pitch;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200793 inv_height = 1.0 / es->geometry.height;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400794
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500795 for (i = 0; i < n; i++, v += 16, p += 6) {
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200796 surface_from_global_float(es, rectangles[i].x1,
797 rectangles[i].y1, &sx, &sy);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500798 v[ 0] = rectangles[i].x1;
799 v[ 1] = rectangles[i].y1;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200800 v[ 2] = sx * inv_width;
801 v[ 3] = sy * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500802
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200803 surface_from_global_float(es, rectangles[i].x1,
804 rectangles[i].y2, &sx, &sy);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500805 v[ 4] = rectangles[i].x1;
806 v[ 5] = rectangles[i].y2;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200807 v[ 6] = sx * inv_width;
808 v[ 7] = sy * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500809
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200810 surface_from_global_float(es, rectangles[i].x2,
811 rectangles[i].y1, &sx, &sy);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500812 v[ 8] = rectangles[i].x2;
813 v[ 9] = rectangles[i].y1;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200814 v[10] = sx * inv_width;
815 v[11] = sy * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500816
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200817 surface_from_global_float(es, rectangles[i].x2,
818 rectangles[i].y2, &sx, &sy);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500819 v[12] = rectangles[i].x2;
820 v[13] = rectangles[i].y2;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200821 v[14] = sx * inv_width;
822 v[15] = sy * inv_height;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500823
824 p[0] = i * 4 + 0;
825 p[1] = i * 4 + 1;
826 p[2] = i * 4 + 2;
827 p[3] = i * 4 + 2;
828 p[4] = i * 4 + 1;
829 p[5] = i * 4 + 3;
830 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500831
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400832 return n;
833}
834
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500835WL_EXPORT void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500836weston_surface_draw(struct weston_surface *es, struct weston_output *output,
837 pixman_region32_t *damage)
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400838{
Scott Moreau9fb98242012-05-22 10:18:50 -0600839 GLfloat surface_rect[4] = { 0.0, 1.0, 0.0, 1.0 };
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500840 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400841 GLfloat *v;
842 pixman_region32_t repaint;
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400843 GLint filter;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400844 int n;
845
Pekka Paalanena9f8a212012-01-26 11:28:08 +0200846 pixman_region32_init(&repaint);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500847 pixman_region32_intersect(&repaint,
848 &es->transform.boundingbox, damage);
849 pixman_region32_subtract(&repaint, &repaint, &es->clip);
Pekka Paalanenc7b45f62012-02-06 12:16:07 +0200850
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400851 if (!pixman_region32_not_empty(&repaint))
Pekka Paalanena9f8a212012-01-26 11:28:08 +0200852 goto out;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400853
Kristian Høgsberg101cb652012-02-17 10:45:16 -0500854 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
Scott Moreau9fb98242012-05-22 10:18:50 -0600855 if (es->blend || es->alpha < 1.0)
Kristian Høgsbergd6ad1222012-05-17 11:11:15 -0400856 glEnable(GL_BLEND);
857 else
858 glDisable(GL_BLEND);
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400859
Kristian Høgsberg07632622012-01-25 23:02:06 -0500860 if (ec->current_shader != es->shader) {
861 glUseProgram(es->shader->program);
Kristian Høgsberg07632622012-01-25 23:02:06 -0500862 ec->current_shader = es->shader;
863 }
864
Kristian Høgsberg0452abc2012-01-31 15:38:36 -0500865 glUniformMatrix4fv(es->shader->proj_uniform,
866 1, GL_FALSE, output->matrix.d);
Pekka Paalanenf55f5442012-02-02 16:49:05 +0200867 glUniform1i(es->shader->tex_uniform, 0);
868 glUniform4fv(es->shader->color_uniform, 1, es->color);
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400869 glUniform1f(es->shader->alpha_uniform, es->alpha);
Pekka Paalanenf55f5442012-02-02 16:49:05 +0200870 glUniform1f(es->shader->texwidth_uniform,
871 (GLfloat)es->geometry.width / es->pitch);
Scott Moreau9fb98242012-05-22 10:18:50 -0600872 if (es->blend)
873 glUniform4fv(es->shader->opaque_uniform, 1, es->opaque_rect);
874 else
875 glUniform4fv(es->shader->opaque_uniform, 1, surface_rect);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200876
Scott Moreauccbf29d2012-02-22 14:21:41 -0700877 if (es->transform.enabled || output->zoom.active)
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400878 filter = GL_LINEAR;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200879 else
Pekka Paalanenc61eca62012-01-06 14:10:06 +0200880 filter = GL_NEAREST;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200881
882 n = texture_region(es, &repaint);
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400883
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500884 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400885 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
886 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
887
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500888 v = ec->vertices.data;
889 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
890 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400891 glEnableVertexAttribArray(0);
892 glEnableVertexAttribArray(1);
Pekka Paalanen3df327f2012-01-24 15:53:35 +0200893
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500894 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
895
Pekka Paalanen3df327f2012-01-24 15:53:35 +0200896 glDisableVertexAttribArray(1);
897 glDisableVertexAttribArray(0);
898
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500899 ec->vertices.size = 0;
900 ec->indices.size = 0;
Pekka Paalanena9f8a212012-01-26 11:28:08 +0200901
902out:
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500903 pixman_region32_fini(&repaint);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500904}
905
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500906WL_EXPORT void
907weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400908{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500909 wl_list_remove(&surface->layer_link);
910 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500911 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500912 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400913}
914
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400915WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500916weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400917{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500918 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400919
920 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500921 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400922}
923
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500924WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500925weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +0200926{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400927 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200928 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200929
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400930 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -0500931 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200932}
933
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400934WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500935weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400936{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500937 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400938
Kristian Høgsberg944236a2012-02-28 23:07:47 -0500939 pixman_region32_union(&compositor->damage,
940 &compositor->damage, &output->region);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500941 weston_compositor_schedule_repaint(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400942}
943
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400944static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500945fade_frame(struct weston_animation *animation,
946 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400947{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500948 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400949 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500950 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500951 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400952
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500953 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500954 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500955 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
956 compositor->fade.spring.current);
957 weston_surface_damage(surface);
958
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500959 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400960 compositor->fade.spring.current =
961 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400962 wl_list_remove(&animation->link);
963 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200964
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500965 if (compositor->fade.spring.current < 0.001) {
966 destroy_surface(&surface->surface.resource);
967 compositor->fade.surface = NULL;
968 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500969 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400970 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200971 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400972 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400973}
974
Kristian Høgsberg30c018b2012-01-26 08:40:37 -0500975struct weston_frame_callback {
976 struct wl_resource resource;
977 struct wl_list link;
978};
979
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400980static void
Kristian Høgsberg30c018b2012-01-26 08:40:37 -0500981weston_output_repaint(struct weston_output *output, int msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400982{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500983 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500984 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500985 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -0500986 struct weston_animation *animation, *next;
987 struct weston_frame_callback *cb, *cnext;
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500988 pixman_region32_t opaque, new_damage, output_damage;
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500989 int32_t width, height;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500990
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +0200991 weston_compositor_update_drag_surfaces(ec);
992
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500993 width = output->current->width +
994 output->border.left + output->border.right;
995 height = output->current->height +
996 output->border.top + output->border.bottom;
997 glViewport(0, 0, width, height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500998
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500999 /* Rebuild the surface list and update surface transforms up front. */
1000 wl_list_init(&ec->surface_list);
1001 wl_list_for_each(layer, &ec->layer_list, link) {
1002 wl_list_for_each(es, &layer->surface_list, layer_link) {
1003 weston_surface_update_transform(es);
1004 wl_list_insert(ec->surface_list.prev, &es->link);
1005 }
1006 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001007
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001008 if (output->assign_planes)
1009 /*
1010 * This will queue flips for the fbs and sprites where
1011 * applicable and clear the damage for those surfaces.
1012 * The repaint loop below will repaint everything
1013 * else.
1014 */
1015 output->assign_planes(output);
1016
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001017 pixman_region32_init(&new_damage);
1018 pixman_region32_init(&opaque);
1019
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001020 wl_list_for_each(es, &ec->surface_list, link) {
1021 pixman_region32_subtract(&es->damage, &es->damage, &opaque);
1022 pixman_region32_union(&new_damage, &new_damage, &es->damage);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001023 empty_region(&es->damage);
1024 pixman_region32_copy(&es->clip, &opaque);
Pekka Paalanen730d87e2012-02-09 16:39:38 +02001025 pixman_region32_union(&opaque, &opaque, &es->transform.opaque);
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001026 }
1027
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001028 pixman_region32_union(&ec->damage, &ec->damage, &new_damage);
1029
1030 pixman_region32_init(&output_damage);
1031 pixman_region32_union(&output_damage,
1032 &ec->damage, &output->previous_damage);
1033 pixman_region32_copy(&output->previous_damage, &ec->damage);
1034 pixman_region32_intersect(&output_damage,
1035 &output_damage, &output->region);
1036 pixman_region32_subtract(&ec->damage, &ec->damage, &output->region);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001037
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -04001038 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001039 pixman_region32_fini(&new_damage);
1040
Scott Moreauccbf29d2012-02-22 14:21:41 -07001041 if (output->dirty)
1042 weston_output_update_matrix(output);
1043
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001044 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001045
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001046 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001047
Kristian Høgsbergef044142011-06-21 15:02:12 -04001048 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001049
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001050 weston_compositor_repick(ec);
1051 wl_event_loop_dispatch(ec->input_loop, 0);
1052
Kristian Høgsberg33418202011-08-16 23:01:28 -04001053 wl_list_for_each_safe(cb, cnext, &output->frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001054 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001055 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001056 }
1057
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001058 wl_list_for_each_safe(animation, next, &ec->animation_list, link)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001059 animation->frame(animation, output, msecs);
1060}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001061
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001062static int
1063weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001064{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001065 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001066
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001067 wl_event_loop_dispatch(compositor->input_loop, 0);
1068
1069 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001070}
1071
1072WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001073weston_output_finish_frame(struct weston_output *output, int msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001074{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001075 struct weston_compositor *compositor = output->compositor;
1076 struct wl_event_loop *loop =
1077 wl_display_get_event_loop(compositor->wl_display);
1078 int fd;
1079
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04001080 wl_signal_emit(&output->frame_signal, &msecs);
1081
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001082 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001083 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001084 return;
1085 }
1086
1087 output->repaint_scheduled = 0;
1088 if (compositor->input_loop_source)
1089 return;
1090
1091 fd = wl_event_loop_get_fd(compositor->input_loop);
1092 compositor->input_loop_source =
1093 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1094 weston_compositor_read_input, compositor);
1095}
1096
1097static void
1098idle_repaint(void *data)
1099{
1100 struct weston_output *output = data;
1101
1102 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001103}
1104
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001105WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001106weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1107{
1108 wl_list_init(&layer->surface_list);
1109 wl_list_insert(below, &layer->link);
1110}
1111
1112WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001113weston_compositor_schedule_repaint(struct weston_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001114{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001115 struct weston_output *output;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001116 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001117
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001118 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001119 return;
1120
Kristian Høgsbergef044142011-06-21 15:02:12 -04001121 loop = wl_display_get_event_loop(compositor->wl_display);
1122 wl_list_for_each(output, &compositor->output_list, link) {
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001123 output->repaint_needed = 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001124 if (output->repaint_scheduled)
1125 continue;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001126
Kristian Høgsbergef044142011-06-21 15:02:12 -04001127 wl_event_loop_add_idle(loop, idle_repaint, output);
1128 output->repaint_scheduled = 1;
1129 }
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001130
1131 if (compositor->input_loop_source) {
1132 wl_event_source_remove(compositor->input_loop_source);
1133 compositor->input_loop_source = NULL;
1134 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001135}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001136
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001137WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001138weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001139{
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001140 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001141 int done;
1142
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001143 done = weston_spring_done(&compositor->fade.spring);
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001144 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001145 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001146 return;
1147
1148 if (done)
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001149 compositor->fade.spring.timestamp =
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001150 weston_compositor_get_time();
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001151
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001152 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001153 surface = weston_surface_create(compositor);
1154 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001155 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001156 wl_list_insert(&compositor->fade_layer.surface_list,
1157 &surface->layer_link);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001158 weston_surface_assign_output(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001159 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001160 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001161 }
1162
1163 weston_surface_damage(compositor->fade.surface);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001164 if (wl_list_empty(&compositor->fade.animation.link))
1165 wl_list_insert(compositor->animation_list.prev,
1166 &compositor->fade.animation.link);
1167}
1168
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001169static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001170surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001171{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001172 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001173}
1174
Casey Dahlin96d8a752012-04-19 22:50:07 -04001175static struct wl_resource *
1176find_resource_for_client(struct wl_list *list, struct wl_client *client)
1177{
1178 struct wl_resource *r;
1179
1180 wl_list_for_each(r, list, link) {
1181 if (r->client == client)
1182 return r;
1183 }
1184
1185 return NULL;
1186}
1187
Casey Dahlin9074db52012-04-19 22:50:09 -04001188static void
1189weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1190{
1191 uint32_t different = es->output_mask ^ mask;
1192 uint32_t entered = mask & different;
1193 uint32_t left = es->output_mask & different;
1194 struct weston_output *output;
Rob Bradford8667e772012-05-18 14:13:03 +01001195 struct wl_resource *resource = NULL;
Casey Dahlin9074db52012-04-19 22:50:09 -04001196 struct wl_client *client = es->surface.resource.client;
1197
1198 if (es->surface.resource.client == NULL)
1199 return;
1200 if (different == 0)
1201 return;
1202
1203 es->output_mask = mask;
1204 wl_list_for_each(output, &es->compositor->output_list, link) {
1205 if (1 << output->id & different)
1206 resource =
1207 find_resource_for_client(&output->resource_list,
1208 client);
1209 if (1 << output->id & entered)
1210 wl_surface_send_enter(&es->surface.resource, resource);
1211 if (1 << output->id & left)
1212 wl_surface_send_leave(&es->surface.resource, resource);
1213 }
1214}
1215
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001216WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001217weston_surface_assign_output(struct weston_surface *es)
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001218{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001219 struct weston_compositor *ec = es->compositor;
1220 struct weston_output *output, *new_output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001221 pixman_region32_t region;
Casey Dahlin9074db52012-04-19 22:50:09 -04001222 uint32_t max, area, mask;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001223 pixman_box32_t *e;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001224
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001225 new_output = NULL;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001226 max = 0;
Casey Dahlin9074db52012-04-19 22:50:09 -04001227 mask = 0;
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001228 pixman_region32_init(&region);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001229 wl_list_for_each(output, &ec->output_list, link) {
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001230 pixman_region32_intersect(&region, &es->transform.boundingbox,
1231 &output->region);
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001232
1233 e = pixman_region32_extents(&region);
1234 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1235
Casey Dahlin9074db52012-04-19 22:50:09 -04001236 if (area > 0)
1237 mask |= 1 << output->id;
1238
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001239 if (area >= max) {
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001240 new_output = output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001241 max = area;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001242 }
1243 }
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001244 pixman_region32_fini(&region);
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001245
1246 es->output = new_output;
Casey Dahlin9074db52012-04-19 22:50:09 -04001247 weston_surface_update_output_mask(es, mask);
1248
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001249 if (!wl_list_empty(&es->frame_callback_list)) {
1250 wl_list_insert_list(new_output->frame_callback_list.prev,
1251 &es->frame_callback_list);
1252 wl_list_init(&es->frame_callback_list);
1253 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001254}
1255
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001256static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001257surface_attach(struct wl_client *client,
1258 struct wl_resource *resource,
1259 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1260{
1261 struct weston_surface *es = resource->data;
1262 struct wl_buffer *buffer = NULL;
1263
1264 if (buffer_resource)
1265 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001266
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -04001267 weston_surface_attach(&es->surface, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001268
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001269 if (buffer && es->configure)
1270 es->configure(es, sx, sy);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001271}
1272
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001273static void
Pekka Paalanen52bfbaa2012-04-11 16:19:37 +03001274texture_set_subimage(struct weston_surface *surface,
1275 int32_t x, int32_t y, int32_t width, int32_t height)
1276{
1277 glBindTexture(GL_TEXTURE_2D, surface->texture);
1278
1279#ifdef GL_UNPACK_ROW_LENGTH
1280 /* Mesa does not define GL_EXT_unpack_subimage */
1281
1282 if (surface->compositor->has_unpack_subimage) {
1283 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch);
1284 glPixelStorei(GL_UNPACK_SKIP_PIXELS, x);
1285 glPixelStorei(GL_UNPACK_SKIP_ROWS, y);
1286
1287 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height,
1288 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1289 wl_shm_buffer_get_data(surface->buffer));
1290 return;
1291 }
1292#endif
1293
1294 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1295 surface->pitch, surface->buffer->height, 0,
1296 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1297 wl_shm_buffer_get_data(surface->buffer));
1298}
1299
1300static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001301surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001302 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001303 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001304{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001305 struct weston_surface *es = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001306
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001307 weston_surface_damage_rectangle(es, x, y, width, height);
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04001308
Pekka Paalanen52bfbaa2012-04-11 16:19:37 +03001309 if (es->buffer && wl_buffer_is_shm(es->buffer))
1310 texture_set_subimage(es, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001311}
1312
Kristian Høgsberg33418202011-08-16 23:01:28 -04001313static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001314destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001315{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001316 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001317
1318 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001319 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001320}
1321
1322static void
1323surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001324 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001325{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001326 struct weston_frame_callback *cb;
1327 struct weston_surface *es = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001328
1329 cb = malloc(sizeof *cb);
1330 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001331 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001332 return;
1333 }
1334
1335 cb->resource.object.interface = &wl_callback_interface;
1336 cb->resource.object.id = callback;
1337 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001338 cb->resource.client = client;
1339 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001340
1341 wl_client_add_resource(client, &cb->resource);
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -04001342
1343 if (es->output) {
1344 wl_list_insert(es->output->frame_callback_list.prev,
1345 &cb->link);
1346 } else {
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001347 wl_list_insert(es->frame_callback_list.prev, &cb->link);
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -04001348 }
Kristian Høgsberg33418202011-08-16 23:01:28 -04001349}
1350
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001351static void
1352surface_set_opaque_region(struct wl_client *client,
1353 struct wl_resource *resource,
1354 struct wl_resource *region_resource)
1355{
1356 struct weston_surface *surface = resource->data;
1357 struct weston_region *region;
1358
1359 pixman_region32_fini(&surface->opaque);
1360
1361 if (region_resource) {
1362 region = region_resource->data;
1363 pixman_region32_init_rect(&surface->opaque, 0, 0,
1364 surface->geometry.width,
1365 surface->geometry.height);
1366 pixman_region32_intersect(&surface->opaque,
1367 &surface->opaque, &region->region);
1368 } else {
1369 pixman_region32_init(&surface->opaque);
1370 }
1371
1372 surface->geometry.dirty = 1;
1373}
1374
1375static void
1376surface_set_input_region(struct wl_client *client,
1377 struct wl_resource *resource,
1378 struct wl_resource *region_resource)
1379{
1380 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001381 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001382
1383 if (region_resource) {
1384 region = region_resource->data;
1385 pixman_region32_init_rect(&surface->input, 0, 0,
1386 surface->geometry.width,
1387 surface->geometry.height);
1388 pixman_region32_intersect(&surface->input,
1389 &surface->input, &region->region);
1390 } else {
1391 pixman_region32_init_rect(&surface->input, 0, 0,
1392 surface->geometry.width,
1393 surface->geometry.height);
1394 }
1395
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001396 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001397}
1398
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001399static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001400 surface_destroy,
1401 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001402 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001403 surface_frame,
1404 surface_set_opaque_region,
1405 surface_set_input_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001406};
1407
1408static void
1409compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001410 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001411{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001412 struct weston_compositor *ec = resource->data;
1413 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001414
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001415 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001416 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001417 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001418 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001419 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001420
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001421 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001422
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001423 surface->surface.resource.object.id = id;
1424 surface->surface.resource.object.interface = &wl_surface_interface;
1425 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001426 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001427 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001428
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001429 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001430}
1431
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001432static void
1433destroy_region(struct wl_resource *resource)
1434{
1435 struct weston_region *region =
1436 container_of(resource, struct weston_region, resource);
1437
1438 pixman_region32_fini(&region->region);
1439 free(region);
1440}
1441
1442static void
1443region_destroy(struct wl_client *client, struct wl_resource *resource)
1444{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001445 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001446}
1447
1448static void
1449region_add(struct wl_client *client, struct wl_resource *resource,
1450 int32_t x, int32_t y, int32_t width, int32_t height)
1451{
1452 struct weston_region *region = resource->data;
1453
1454 pixman_region32_union_rect(&region->region, &region->region,
1455 x, y, width, height);
1456}
1457
1458static void
1459region_subtract(struct wl_client *client, struct wl_resource *resource,
1460 int32_t x, int32_t y, int32_t width, int32_t height)
1461{
1462 struct weston_region *region = resource->data;
1463 pixman_region32_t rect;
1464
1465 pixman_region32_init_rect(&rect, x, y, width, height);
1466 pixman_region32_subtract(&region->region, &region->region, &rect);
1467 pixman_region32_fini(&rect);
1468}
1469
1470static const struct wl_region_interface region_interface = {
1471 region_destroy,
1472 region_add,
1473 region_subtract
1474};
1475
1476static void
1477compositor_create_region(struct wl_client *client,
1478 struct wl_resource *resource, uint32_t id)
1479{
1480 struct weston_region *region;
1481
1482 region = malloc(sizeof *region);
1483 if (region == NULL) {
1484 wl_resource_post_no_memory(resource);
1485 return;
1486 }
1487
1488 region->resource.destroy = destroy_region;
1489
1490 region->resource.object.id = id;
1491 region->resource.object.interface = &wl_region_interface;
1492 region->resource.object.implementation =
1493 (void (**)(void)) &region_interface;
1494 region->resource.data = region;
1495
1496 pixman_region32_init(&region->region);
1497
1498 wl_client_add_resource(client, &region->resource);
1499}
1500
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001501static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001502 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001503 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001504};
1505
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001506WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001507weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001508{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001509 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1510 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001511
1512 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001513 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001514}
1515
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001516static void
1517weston_compositor_dpms_on(struct weston_compositor *compositor)
1518{
1519 struct weston_output *output;
1520
1521 wl_list_for_each(output, &compositor->output_list, link)
1522 if (output->set_dpms)
1523 output->set_dpms(output, WESTON_DPMS_ON);
1524}
1525
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001526WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001527weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001528{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001529 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1530 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001531 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001532 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001533 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001534 }
1535}
1536
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001537static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001538weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001539{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001540 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001541 compositor->idle_inhibit++;
1542}
1543
1544static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001545weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001546{
1547 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001548 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001549}
1550
1551static int
1552idle_handler(void *data)
1553{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001554 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001555
1556 if (compositor->idle_inhibit)
1557 return 1;
1558
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001559 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001560
1561 return 1;
1562}
1563
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001564static void
Daniel Stone37816df2012-05-16 18:45:18 +01001565weston_seat_update_drag_surface(struct wl_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001566
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001567static void
Daniel Stone37816df2012-05-16 18:45:18 +01001568clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001569{
Daniel Stone37816df2012-05-16 18:45:18 +01001570 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001571 struct weston_output *output, *prev = NULL;
1572 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001573
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001574 x = wl_fixed_to_int(*fx);
1575 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001576 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1577 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001578
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001579 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001580 if (pixman_region32_contains_point(&output->region,
1581 x, y, NULL))
1582 valid = 1;
1583 if (pixman_region32_contains_point(&output->region,
1584 old_x, old_y, NULL))
1585 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001586 }
Daniel Stone37816df2012-05-16 18:45:18 +01001587
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001588 if (!valid) {
1589 if (x < prev->x)
1590 *fx = wl_fixed_from_int(prev->x);
1591 else if (x >= prev->x + prev->current->width)
1592 *fx = wl_fixed_from_int(prev->x +
1593 prev->current->width - 1);
1594 if (y < prev->y)
1595 *fy = wl_fixed_from_int(prev->y);
1596 else if (y >= prev->y + prev->current->height)
1597 *fy = wl_fixed_from_int(prev->y +
1598 prev->current->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001599 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001600}
1601
1602WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01001603notify_motion(struct wl_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001604{
1605 const struct wl_pointer_grab_interface *interface;
Daniel Stone37816df2012-05-16 18:45:18 +01001606 struct weston_seat *ws = (struct weston_seat *) seat;
1607 struct weston_compositor *ec = ws->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001608 struct weston_output *output;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001609 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001610
1611 weston_compositor_activity(ec);
1612
Daniel Stone37816df2012-05-16 18:45:18 +01001613 clip_pointer_motion(ws, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001614
Daniel Stone37816df2012-05-16 18:45:18 +01001615 weston_seat_update_drag_surface(seat,
1616 x - seat->pointer->x,
1617 y - seat->pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001618
Daniel Stone37816df2012-05-16 18:45:18 +01001619 seat->pointer->x = x;
1620 seat->pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001621
1622 ix = wl_fixed_to_int(x);
1623 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001624
Scott Moreauccbf29d2012-02-22 14:21:41 -07001625 wl_list_for_each(output, &ec->output_list, link)
1626 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001627 pixman_region32_contains_point(&output->region,
1628 ix, iy, NULL))
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001629 weston_output_update_zoom(output, x, y, ZOOM_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001630
Daniel Stone37816df2012-05-16 18:45:18 +01001631 weston_device_repick(seat);
1632 interface = seat->pointer->grab->interface;
1633 interface->motion(seat->pointer->grab, time,
1634 seat->pointer->grab->x, seat->pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001635
Daniel Stone37816df2012-05-16 18:45:18 +01001636 if (ws->sprite) {
1637 weston_surface_set_position(ws->sprite,
1638 ix - ws->hotspot_x,
1639 iy - ws->hotspot_y);
Pekka Paalanenf8c6aae2012-02-13 11:01:59 +02001640 weston_compositor_schedule_repaint(ec);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001641 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001642}
1643
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001644WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001645weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001646 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001647{
Daniel Stone37816df2012-05-16 18:45:18 +01001648 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001649
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001650 if (seat->seat.keyboard) {
1651 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1652 wl_data_device_set_keyboard_focus(&seat->seat);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001653
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001654 if (seat->seat.keyboard->focus_resource) {
1655 wl_keyboard_send_modifiers(
1656 seat->seat.keyboard->focus_resource,
1657 wl_display_next_serial(compositor->wl_display),
1658 seat->xkb_state.mods_depressed,
1659 seat->xkb_state.mods_latched,
1660 seat->xkb_state.mods_locked,
1661 seat->xkb_state.group);
1662 }
Daniel Stone351eb612012-05-31 15:27:47 -04001663 }
1664
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001665 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001666}
1667
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001668WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01001669notify_button(struct wl_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001670 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001671{
Daniel Stone37816df2012-05-16 18:45:18 +01001672 struct weston_seat *ws = (struct weston_seat *) seat;
1673 struct weston_compositor *compositor = ws->compositor;
1674 struct weston_surface *focus =
1675 (struct weston_surface *) seat->pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001676 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001677
Daniel Stone4dbadb12012-05-30 16:31:51 +01001678 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001679 if (compositor->ping_handler && focus)
1680 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001681 weston_compositor_idle_inhibit(compositor);
Daniel Stone37816df2012-05-16 18:45:18 +01001682 if (seat->pointer->button_count == 0) {
1683 seat->pointer->grab_button = button;
1684 seat->pointer->grab_time = time;
1685 seat->pointer->grab_x = seat->pointer->x;
1686 seat->pointer->grab_y = seat->pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001687 }
Daniel Stone37816df2012-05-16 18:45:18 +01001688 seat->pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001689 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001690 weston_compositor_idle_release(compositor);
Daniel Stone37816df2012-05-16 18:45:18 +01001691 seat->pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001692 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001693
Daniel Stone325fc2d2012-05-30 16:31:58 +01001694 weston_compositor_run_button_binding(compositor, ws, time, button,
1695 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001696
Daniel Stone37816df2012-05-16 18:45:18 +01001697 seat->pointer->grab->interface->button(seat->pointer->grab, time,
1698 button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001699
Daniel Stone37816df2012-05-16 18:45:18 +01001700 if (seat->pointer->button_count == 1)
1701 seat->pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001702 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001703}
1704
Scott Moreau210d0792012-03-22 10:47:01 -06001705WL_EXPORT void
Daniel Stone878f0b72012-05-30 16:31:57 +01001706notify_axis(struct wl_seat *seat, uint32_t time, uint32_t axis,
1707 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001708{
Daniel Stone37816df2012-05-16 18:45:18 +01001709 struct weston_seat *ws = (struct weston_seat *) seat;
1710 struct weston_compositor *compositor = ws->compositor;
1711 struct weston_surface *focus =
1712 (struct weston_surface *) seat->pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001713 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1714
1715 if (compositor->ping_handler && focus)
1716 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001717
1718 weston_compositor_activity(compositor);
1719
Scott Moreau6a3633d2012-03-20 08:47:59 -06001720 if (value)
Daniel Stone325fc2d2012-05-30 16:31:58 +01001721 weston_compositor_run_axis_binding(compositor, ws, time, axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001722 value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06001723 else
1724 return;
1725
Daniel Stone37816df2012-05-16 18:45:18 +01001726 if (seat->pointer->focus_resource)
Daniel Stone878f0b72012-05-30 16:31:57 +01001727 wl_pointer_send_axis(seat->pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01001728 value);
Scott Moreau210d0792012-03-22 10:47:01 -06001729}
1730
Daniel Stone7ace3902012-05-30 16:31:40 +01001731static int
Daniel Stone37816df2012-05-16 18:45:18 +01001732update_modifier_state(struct weston_seat *seat, uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001733{
Daniel Stone7ace3902012-05-30 16:31:40 +01001734 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001735 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01001736 enum weston_led leds = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01001737 int ret = 0;
1738
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001739 /* First update the XKB state object with the keypress. */
Daniel Stone994679a2012-05-30 16:31:43 +01001740 xkb_state_update_key(seat->xkb_state.state, key + 8,
Daniel Stone7ace3902012-05-30 16:31:40 +01001741 state ? XKB_KEY_DOWN : XKB_KEY_UP);
1742
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001743 /* Serialize and update our internal state, checking to see if it's
1744 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01001745 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1746 XKB_STATE_DEPRESSED);
1747 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1748 XKB_STATE_LATCHED);
1749 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1750 XKB_STATE_LOCKED);
1751 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01001752 XKB_STATE_EFFECTIVE);
1753
Daniel Stone994679a2012-05-30 16:31:43 +01001754 if (mods_depressed != seat->xkb_state.mods_depressed ||
1755 mods_latched != seat->xkb_state.mods_latched ||
1756 mods_locked != seat->xkb_state.mods_locked ||
1757 group != seat->xkb_state.group)
Daniel Stone7ace3902012-05-30 16:31:40 +01001758 ret = 1;
1759
Daniel Stone994679a2012-05-30 16:31:43 +01001760 seat->xkb_state.mods_depressed = mods_depressed;
1761 seat->xkb_state.mods_latched = mods_latched;
1762 seat->xkb_state.mods_locked = mods_locked;
1763 seat->xkb_state.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001764
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001765 /* And update the modifier_state for bindings. */
1766 mods_lookup = mods_depressed | mods_latched;
1767 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01001768 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001769 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01001770 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001771 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01001772 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001773 seat->modifier_state |= MODIFIER_SUPER;
Daniel Stone7ace3902012-05-30 16:31:40 +01001774
Daniel Stone8c8164f2012-05-30 16:31:45 +01001775 /* Finally, notify the compositor that LEDs have changed. */
1776 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001777 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001778 leds |= LED_NUM_LOCK;
1779 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001780 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001781 leds |= LED_CAPS_LOCK;
1782 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001783 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001784 leds |= LED_SCROLL_LOCK;
1785 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01001786 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01001787 seat->xkb_state.leds = leds;
1788
Daniel Stone7ace3902012-05-30 16:31:40 +01001789 return ret;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001790}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001791
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001792WL_EXPORT void
Daniel Stonec9785ea2012-05-30 16:31:52 +01001793notify_key(struct wl_seat *seat, uint32_t time, uint32_t key,
1794 enum wl_keyboard_key_state state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001795{
Daniel Stone37816df2012-05-16 18:45:18 +01001796 struct weston_seat *ws = (struct weston_seat *) seat;
1797 struct weston_compositor *compositor = ws->compositor;
1798 struct weston_surface *focus =
1799 (struct weston_surface *) seat->pointer->focus;
Daniel Stone7ace3902012-05-30 16:31:40 +01001800 struct wl_keyboard_grab *grab = seat->keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001801 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001802 uint32_t *k, *end;
Daniel Stone7ace3902012-05-30 16:31:40 +01001803 int mods;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001804
Daniel Stonec9785ea2012-05-30 16:31:52 +01001805 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001806 if (compositor->ping_handler && focus)
1807 compositor->ping_handler(focus, serial);
1808
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001809 weston_compositor_idle_inhibit(compositor);
Daniel Stone37816df2012-05-16 18:45:18 +01001810 seat->keyboard->grab_key = key;
1811 seat->keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07001812 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001813 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001814 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001815
Daniel Stone7ace3902012-05-30 16:31:40 +01001816 mods = update_modifier_state(ws, key, state);
Daniel Stone37816df2012-05-16 18:45:18 +01001817 end = seat->keyboard->keys.data + seat->keyboard->keys.size;
1818 for (k = seat->keyboard->keys.data; k < end; k++) {
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001819 if (*k == key)
1820 *k = *--end;
1821 }
Daniel Stone37816df2012-05-16 18:45:18 +01001822 seat->keyboard->keys.size = (void *) end - seat->keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001823 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Daniel Stone37816df2012-05-16 18:45:18 +01001824 k = wl_array_add(&seat->keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001825 *k = key;
1826 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001827
Daniel Stone7ace3902012-05-30 16:31:40 +01001828 if (grab == &seat->keyboard->default_grab)
Daniel Stone325fc2d2012-05-30 16:31:58 +01001829 weston_compositor_run_key_binding(compositor, ws, time, key,
1830 state);
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001831
Daniel Stone7ace3902012-05-30 16:31:40 +01001832 grab->interface->key(grab, time, key, state);
Daniel Stone351eb612012-05-31 15:27:47 -04001833 if (mods)
1834 grab->interface->modifiers(grab,
1835 wl_display_get_serial(compositor->wl_display),
Daniel Stone994679a2012-05-30 16:31:43 +01001836 ws->xkb_state.mods_depressed,
1837 ws->xkb_state.mods_latched,
1838 ws->xkb_state.mods_locked,
1839 ws->xkb_state.group);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001840}
1841
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001842WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01001843notify_pointer_focus(struct wl_seat *seat, struct weston_output *output,
1844 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001845{
Daniel Stone37816df2012-05-16 18:45:18 +01001846 struct weston_seat *ws = (struct weston_seat *) seat;
1847 struct weston_compositor *compositor = ws->compositor;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001848
1849 if (output) {
Daniel Stone37816df2012-05-16 18:45:18 +01001850 weston_seat_update_drag_surface(seat,
1851 x - seat->pointer->x,
1852 y - seat->pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001853
Daniel Stone37816df2012-05-16 18:45:18 +01001854 seat->pointer->x = x;
1855 seat->pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001856 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001857 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001858 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001859 compositor->focus = 0;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001860 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001861 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001862}
1863
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001864static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001865destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001866{
Daniel Stone37816df2012-05-16 18:45:18 +01001867 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001868
Daniel Stone37816df2012-05-16 18:45:18 +01001869 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001870 saved_kbd_focus_listener);
1871
Daniel Stone37816df2012-05-16 18:45:18 +01001872 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001873}
1874
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001875WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01001876notify_keyboard_focus(struct wl_seat *seat, struct wl_array *keys)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001877{
Daniel Stone37816df2012-05-16 18:45:18 +01001878 struct weston_seat *ws = (struct weston_seat *) seat;
1879 struct weston_compositor *compositor = ws->compositor;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001880 struct wl_surface *surface;
Kristian Høgsbergd3c02752012-03-04 22:35:47 -05001881 uint32_t *k;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001882
Kristian Høgsberg31dd6b82012-04-09 22:10:00 -04001883 if (keys) {
Daniel Stone37816df2012-05-16 18:45:18 +01001884 wl_array_copy(&seat->keyboard->keys, keys);
1885 ws->modifier_state = 0;
1886 wl_array_for_each(k, &seat->keyboard->keys) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001887 weston_compositor_idle_inhibit(compositor);
Daniel Stone37816df2012-05-16 18:45:18 +01001888 update_modifier_state(ws, *k, 1);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001889 }
1890
Daniel Stone37816df2012-05-16 18:45:18 +01001891 surface = ws->saved_kbd_focus;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001892
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001893 if (surface) {
Daniel Stone37816df2012-05-16 18:45:18 +01001894 wl_list_remove(&ws->saved_kbd_focus_listener.link);
1895 wl_keyboard_set_focus(ws->seat.keyboard, surface);
Daniel Stone351eb612012-05-31 15:27:47 -04001896
1897 if (seat->keyboard->focus_resource) {
1898 wl_keyboard_send_modifiers(seat->keyboard->focus_resource,
1899 wl_display_next_serial(compositor->wl_display),
Daniel Stone994679a2012-05-30 16:31:43 +01001900 ws->xkb_state.mods_depressed,
1901 ws->xkb_state.mods_latched,
1902 ws->xkb_state.mods_locked,
1903 ws->xkb_state.group);
Daniel Stone351eb612012-05-31 15:27:47 -04001904 }
Daniel Stone37816df2012-05-16 18:45:18 +01001905 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001906 }
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001907 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001908 wl_array_for_each(k, &seat->keyboard->keys)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001909 weston_compositor_idle_release(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001910
Daniel Stone37816df2012-05-16 18:45:18 +01001911 ws->modifier_state = 0;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001912
Daniel Stone37816df2012-05-16 18:45:18 +01001913 surface = ws->seat.keyboard->focus;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001914
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001915 if (surface) {
Daniel Stone37816df2012-05-16 18:45:18 +01001916 ws->saved_kbd_focus = surface;
1917 ws->saved_kbd_focus_listener.notify =
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001918 destroy_device_saved_kbd_focus;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001919 wl_signal_add(&surface->resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01001920 &ws->saved_kbd_focus_listener);
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001921 }
1922
Daniel Stone37816df2012-05-16 18:45:18 +01001923 wl_keyboard_set_focus(ws->seat.keyboard, NULL);
Kristian Høgsberg035dd9c2012-04-10 00:33:40 -04001924 /* FIXME: We really need keyboard grab cancel here to
1925 * let the grab shut down properly. As it is we leak
1926 * the grab data. */
Daniel Stone37816df2012-05-16 18:45:18 +01001927 wl_keyboard_end_grab(ws->seat.keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001928 }
1929}
1930
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001931static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001932lose_touch_focus_resource(struct wl_listener *listener, void *data)
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001933{
Daniel Stone37816df2012-05-16 18:45:18 +01001934 struct weston_seat *seat = container_of(listener, struct weston_seat,
1935 touch_focus_resource_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001936
Daniel Stone37816df2012-05-16 18:45:18 +01001937 seat->touch_focus_resource = NULL;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001938}
1939
1940static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001941lose_touch_focus(struct wl_listener *listener, void *data)
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001942{
Daniel Stone37816df2012-05-16 18:45:18 +01001943 struct weston_seat *seat = container_of(listener, struct weston_seat,
1944 touch_focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001945
Daniel Stone37816df2012-05-16 18:45:18 +01001946 seat->touch_focus = NULL;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001947}
1948
1949static void
Daniel Stone37816df2012-05-16 18:45:18 +01001950touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001951{
Daniel Stone37816df2012-05-16 18:45:18 +01001952 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001953 struct wl_resource *resource;
1954
Daniel Stone37816df2012-05-16 18:45:18 +01001955 if (ws->touch_focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001956 return;
1957
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001958 if (surface) {
1959 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01001960 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04001961 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001962 if (!resource) {
1963 fprintf(stderr, "couldn't find resource\n");
1964 return;
1965 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001966
Daniel Stone37816df2012-05-16 18:45:18 +01001967 ws->touch_focus_resource_listener.notify =
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001968 lose_touch_focus_resource;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001969 wl_signal_add(&resource->destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01001970 &ws->touch_focus_resource_listener);
1971 ws->touch_focus_listener.notify = lose_touch_focus;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001972 wl_signal_add(&surface->resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01001973 &ws->touch_focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001974
Daniel Stone37816df2012-05-16 18:45:18 +01001975 seat->touch->focus = surface;
1976 seat->touch->focus_resource = resource;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001977 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001978 if (seat->touch->focus)
1979 wl_list_remove(&ws->touch_focus_listener.link);
1980 if (seat->touch->focus_resource)
1981 wl_list_remove(&ws->touch_focus_resource_listener.link);
1982 seat->touch->focus = NULL;
1983 seat->touch->focus_resource = NULL;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001984 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001985}
1986
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001987/**
1988 * notify_touch - emulates button touches and notifies surfaces accordingly.
1989 *
1990 * It assumes always the correct cycle sequence until it gets here: touch_down
1991 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1992 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001993 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001994 */
1995WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01001996notify_touch(struct wl_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001997 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001998{
Daniel Stone37816df2012-05-16 18:45:18 +01001999 struct weston_seat *ws = (struct weston_seat *) seat;
2000 struct weston_compositor *ec = ws->compositor;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002001 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002002 wl_fixed_t sx, sy;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002003 uint32_t serial = 0;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002004
2005 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01002006 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002007 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002008
Daniel Stone37816df2012-05-16 18:45:18 +01002009 ws->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002010
2011 /* the first finger down picks the surface, and all further go
2012 * to that surface for the remainder of the touch session i.e.
2013 * until all touch points are up again. */
Daniel Stone37816df2012-05-16 18:45:18 +01002014 if (ws->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002015 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Daniel Stone37816df2012-05-16 18:45:18 +01002016 touch_set_focus(ws, &es->surface);
2017 } else if (ws->touch_focus) {
2018 es = (struct weston_surface *) ws->touch_focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002019 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002020 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002021
Daniel Stone37816df2012-05-16 18:45:18 +01002022 if (ws->touch_focus_resource && ws->touch_focus)
2023 wl_touch_send_down(ws->touch_focus_resource,
2024 serial, time,
2025 &ws->touch_focus->resource,
2026 touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002027 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002028 case WL_TOUCH_MOTION:
2029 es = (struct weston_surface *) ws->touch_focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002030 if (!es)
2031 break;
2032
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002033 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Daniel Stone37816df2012-05-16 18:45:18 +01002034 if (ws->touch_focus_resource)
2035 wl_touch_send_motion(ws->touch_focus_resource,
2036 time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002037 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002038 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002039 weston_compositor_idle_release(ec);
Daniel Stone37816df2012-05-16 18:45:18 +01002040 ws->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002041
Daniel Stone37816df2012-05-16 18:45:18 +01002042 if (ws->touch_focus_resource)
2043 wl_touch_send_up(ws->touch_focus_resource,
2044 serial, time, touch_id);
2045 if (ws->num_tp == 0)
2046 touch_set_focus(ws, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002047 break;
2048 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002049}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002050
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04002051static void
Daniel Stone37816df2012-05-16 18:45:18 +01002052pointer_attach(struct wl_client *client, struct wl_resource *resource,
2053 uint32_t serial, struct wl_resource *buffer_resource,
2054 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002055{
Daniel Stone37816df2012-05-16 18:45:18 +01002056 struct weston_seat *seat = resource->data;
2057 struct weston_compositor *compositor = seat->compositor;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002058 struct wl_buffer *buffer = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002059
Daniel Stone37816df2012-05-16 18:45:18 +01002060 if (serial < seat->seat.pointer->focus_serial)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002061 return;
Daniel Stone37816df2012-05-16 18:45:18 +01002062 if (seat->seat.pointer->focus == NULL)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002063 return;
Daniel Stone37816df2012-05-16 18:45:18 +01002064 if (seat->seat.pointer->focus->resource.client != client)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002065 return;
2066
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002067 if (buffer_resource)
2068 buffer = buffer_resource->data;
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002069
Daniel Stone37816df2012-05-16 18:45:18 +01002070 weston_surface_attach(&seat->sprite->surface, buffer);
2071 empty_region(&seat->sprite->input);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002072
2073 if (!buffer)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002074 return;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002075
Daniel Stone37816df2012-05-16 18:45:18 +01002076 if (!weston_surface_is_mapped(seat->sprite)) {
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002077 wl_list_insert(&compositor->cursor_layer.surface_list,
Daniel Stone37816df2012-05-16 18:45:18 +01002078 &seat->sprite->layer_link);
2079 weston_surface_assign_output(seat->sprite);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002080 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002081
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002082
Daniel Stone37816df2012-05-16 18:45:18 +01002083 seat->hotspot_x = x;
2084 seat->hotspot_y = y;
2085 weston_surface_configure(seat->sprite,
2086 wl_fixed_to_int(seat->seat.pointer->x) - x,
2087 wl_fixed_to_int(seat->seat.pointer->y) - y,
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002088 buffer->width, buffer->height);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002089
Daniel Stone37816df2012-05-16 18:45:18 +01002090 surface_damage(NULL, &seat->sprite->surface.resource,
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002091 0, 0, buffer->width, buffer->height);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002092}
2093
Daniel Stone37816df2012-05-16 18:45:18 +01002094static const struct wl_pointer_interface pointer_interface = {
2095 pointer_attach,
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002096};
2097
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002098static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002099handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002100{
Daniel Stone37816df2012-05-16 18:45:18 +01002101 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002102
Daniel Stone37816df2012-05-16 18:45:18 +01002103 seat = container_of(listener, struct weston_seat,
2104 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002105
Daniel Stone37816df2012-05-16 18:45:18 +01002106 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002107}
2108
Casey Dahlin9074db52012-04-19 22:50:09 -04002109static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002110{
2111 wl_list_remove(&resource->link);
2112 free(resource);
2113}
2114
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002115static void
Daniel Stone37816df2012-05-16 18:45:18 +01002116seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2117 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002118{
Daniel Stone37816df2012-05-16 18:45:18 +01002119 struct weston_seat *seat = resource->data;
2120 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002121
Daniel Stone37816df2012-05-16 18:45:18 +01002122 if (!seat->seat.pointer)
2123 return;
2124
2125 cr = wl_client_add_object(client, &wl_pointer_interface,
2126 &pointer_interface, id, seat);
2127 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002128 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002129
2130 if (seat->seat.pointer->focus &&
2131 seat->seat.pointer->focus->resource.client == client) {
2132 struct weston_surface *surface;
2133 wl_fixed_t sx, sy;
2134
2135 surface = (struct weston_surface *) seat->seat.pointer->focus;
2136 weston_surface_from_global_fixed(surface,
2137 seat->seat.pointer->x,
2138 seat->seat.pointer->y,
2139 &sx,
2140 &sy);
2141 wl_pointer_set_focus(seat->seat.pointer,
2142 seat->seat.pointer->focus,
2143 sx,
2144 sy);
2145 }
Daniel Stone37816df2012-05-16 18:45:18 +01002146}
2147
2148static void
2149seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2150 uint32_t id)
2151{
2152 struct weston_seat *seat = resource->data;
2153 struct wl_resource *cr;
2154
2155 if (!seat->seat.keyboard)
2156 return;
2157
2158 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2159 seat);
2160 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002161 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002162
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002163 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2164 seat->xkb_info.keymap_fd,
2165 seat->xkb_info.keymap_size);
2166
Daniel Stone17b13bd2012-05-30 16:31:39 +01002167 if (seat->seat.keyboard->focus &&
2168 seat->seat.keyboard->focus->resource.client == client) {
2169 wl_keyboard_set_focus(seat->seat.keyboard,
2170 seat->seat.keyboard->focus);
2171 }
Daniel Stone37816df2012-05-16 18:45:18 +01002172}
2173
2174static void
2175seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2176 uint32_t id)
2177{
2178 struct weston_seat *seat = resource->data;
2179 struct wl_resource *cr;
2180
2181 if (!seat->seat.touch)
2182 return;
2183
2184 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2185 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002186 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002187}
2188
2189static const struct wl_seat_interface seat_interface = {
2190 seat_get_pointer,
2191 seat_get_keyboard,
2192 seat_get_touch,
2193};
2194
2195static void
2196bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2197{
2198 struct wl_seat *seat = data;
2199 struct wl_resource *resource;
2200 enum wl_seat_capability caps = 0;
2201
2202 resource = wl_client_add_object(client, &wl_seat_interface,
2203 &seat_interface, id, data);
2204 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002205 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002206
2207 if (seat->pointer)
2208 caps |= WL_SEAT_CAPABILITY_POINTER;
2209 if (seat->keyboard)
2210 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2211 if (seat->touch)
2212 caps |= WL_SEAT_CAPABILITY_TOUCH;
2213
2214 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002215}
2216
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002217static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002218device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002219{
Daniel Stone37816df2012-05-16 18:45:18 +01002220 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002221
Daniel Stone37816df2012-05-16 18:45:18 +01002222 seat = container_of(listener, struct weston_seat,
2223 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002224
Daniel Stone37816df2012-05-16 18:45:18 +01002225 weston_seat_update_drag_surface(&seat->seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002226}
2227
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002228static void weston_compositor_xkb_init(struct weston_compositor *ec,
2229 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002230{
Daniel Stonee379da92012-05-30 16:32:04 +01002231 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002232 ec->xkb_context = xkb_context_new(0);
2233 if (ec->xkb_context == NULL) {
2234 fprintf(stderr, "failed to create XKB context\n");
2235 exit(1);
2236 }
Daniel Stone994679a2012-05-30 16:31:43 +01002237 }
2238
2239 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002240 ec->xkb_names = *names;
2241 if (!ec->xkb_names.rules)
2242 ec->xkb_names.rules = strdup("evdev");
2243 if (!ec->xkb_names.model)
2244 ec->xkb_names.model = strdup("pc105");
2245 if (!ec->xkb_names.layout)
2246 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002247}
2248
Daniel Stonee379da92012-05-30 16:32:04 +01002249static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2250{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002251 if (xkb_info->keymap)
2252 xkb_map_unref(xkb_info->keymap);
2253
2254 if (xkb_info->keymap_area)
2255 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2256 if (xkb_info->keymap_fd >= 0)
2257 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002258}
2259
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002260static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2261{
Daniel Stonee379da92012-05-30 16:32:04 +01002262 free((char *) ec->xkb_names.rules);
2263 free((char *) ec->xkb_names.model);
2264 free((char *) ec->xkb_names.layout);
2265 free((char *) ec->xkb_names.variant);
2266 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002267
Daniel Stonee379da92012-05-30 16:32:04 +01002268 xkb_info_destroy(&ec->xkb_info);
2269 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002270}
2271
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002272static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002273weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002274{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002275 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002276
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002277 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2278 XKB_MOD_NAME_CTRL);
2279 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2280 XKB_MOD_NAME_ALT);
2281 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2282 XKB_MOD_NAME_LOGO);
2283
2284 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2285 XKB_LED_NAME_NUM);
2286 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2287 XKB_LED_NAME_CAPS);
2288 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2289 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002290
2291 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2292 if (keymap_str == NULL) {
2293 fprintf(stderr, "failed to get string version of keymap\n");
2294 exit(EXIT_FAILURE);
2295 }
2296 xkb_info->keymap_size = strlen(keymap_str) + 1;
2297
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002298 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002299 if (xkb_info->keymap_fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002300 fprintf(stderr,
2301 "creating a keymap file for %lu bytes failed: %m\n",
2302 (unsigned long) xkb_info->keymap_size);
2303 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002304 }
2305
2306 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2307 PROT_READ | PROT_WRITE,
2308 MAP_SHARED, xkb_info->keymap_fd, 0);
2309 if (xkb_info->keymap_area == MAP_FAILED) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002310 fprintf(stderr, "failed to mmap() %lu bytes\n",
2311 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002312 goto err_dev_zero;
2313 }
2314 strcpy(xkb_info->keymap_area, keymap_str);
2315 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002316
2317 return;
2318
2319err_dev_zero:
2320 close(xkb_info->keymap_fd);
2321 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002322err_keymap_str:
2323 free(keymap_str);
2324 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002325}
2326
2327static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002328weston_compositor_build_global_keymap(struct weston_compositor *ec)
2329{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002330 if (ec->xkb_info.keymap != NULL)
2331 return;
2332
Daniel Stonee379da92012-05-30 16:32:04 +01002333 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002334 &ec->xkb_names,
2335 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002336 if (ec->xkb_info.keymap == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002337 fprintf(stderr, "failed to compile global XKB keymap\n");
2338 fprintf(stderr,
2339 " tried rules %s, model %s, layout %s, variant %s, "
2340 "options %s",
2341 ec->xkb_names.rules, ec->xkb_names.model,
2342 ec->xkb_names.layout, ec->xkb_names.variant,
2343 ec->xkb_names.options);
2344 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002345 }
2346
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002347 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002348}
2349
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002350WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002351weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002352{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002353 if (seat->has_keyboard)
2354 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002355
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002356 if (keymap != NULL) {
2357 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002358 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002359 }
2360 else {
2361 weston_compositor_build_global_keymap(seat->compositor);
2362 seat->xkb_info = seat->compositor->xkb_info;
2363 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2364 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002365
2366 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2367 if (seat->xkb_state.state == NULL) {
2368 fprintf(stderr, "failed to initialise XKB state\n");
2369 exit(1);
2370 }
2371
2372 seat->xkb_state.mods_depressed = 0;
2373 seat->xkb_state.mods_latched = 0;
2374 seat->xkb_state.mods_locked = 0;
2375 seat->xkb_state.group = 0;
2376
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002377 wl_keyboard_init(&seat->keyboard);
2378 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2379
2380 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01002381}
2382
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002383WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002384weston_seat_init_pointer(struct weston_seat *seat)
2385{
2386 if (seat->has_pointer)
2387 return;
2388
2389 wl_pointer_init(&seat->pointer);
2390 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2391
2392 seat->has_pointer = 1;
2393}
2394
2395WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002396weston_seat_init_touch(struct weston_seat *seat)
2397{
2398 if (seat->has_touch)
2399 return;
2400
2401 wl_touch_init(&seat->touch);
2402 wl_seat_set_touch(&seat->seat, &seat->touch);
2403
2404 seat->has_touch = 1;
2405}
2406
2407WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002408weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002409{
Daniel Stone37816df2012-05-16 18:45:18 +01002410 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002411 seat->has_pointer = 0;
2412 seat->has_keyboard = 0;
2413 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002414
Daniel Stone37816df2012-05-16 18:45:18 +01002415 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2416 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002417
Daniel Stone37816df2012-05-16 18:45:18 +01002418 seat->sprite = weston_surface_create(ec);
2419 seat->sprite->surface.resource.data = seat->sprite;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002420
Daniel Stone37816df2012-05-16 18:45:18 +01002421 seat->compositor = ec;
2422 seat->hotspot_x = 16;
2423 seat->hotspot_y = 16;
2424 seat->modifier_state = 0;
2425 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002426
Daniel Stone37816df2012-05-16 18:45:18 +01002427 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002428 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002429
Daniel Stone37816df2012-05-16 18:45:18 +01002430 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002431
Daniel Stone37816df2012-05-16 18:45:18 +01002432 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2433 wl_signal_add(&seat->seat.drag_icon_signal,
2434 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002435
2436 clipboard_create(seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002437}
2438
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002439WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002440weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002441{
Daniel Stone37816df2012-05-16 18:45:18 +01002442 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002443 /* The global object is destroyed at wl_display_destroy() time. */
2444
Daniel Stone37816df2012-05-16 18:45:18 +01002445 if (seat->sprite)
2446 destroy_surface(&seat->sprite->surface.resource);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002447
Daniel Stone74419a22012-05-30 16:32:02 +01002448 if (seat->xkb_state.state != NULL)
2449 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01002450 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002451
Daniel Stone37816df2012-05-16 18:45:18 +01002452 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002453}
2454
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002455static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002456drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2457{
2458 weston_surface_configure(es,
2459 es->geometry.x + sx, es->geometry.y + sy,
2460 es->buffer->width, es->buffer->height);
2461}
2462
2463static int
Daniel Stone37816df2012-05-16 18:45:18 +01002464device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002465 struct weston_surface *surface)
2466{
Daniel Stone37816df2012-05-16 18:45:18 +01002467 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002468
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002469 if (surface->configure) {
2470 wl_resource_post_error(&surface->surface.resource,
2471 WL_DISPLAY_ERROR_INVALID_OBJECT,
2472 "surface->configure already set");
2473 return 0;
2474 }
2475
Daniel Stone37816df2012-05-16 18:45:18 +01002476 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002477
Daniel Stone37816df2012-05-16 18:45:18 +01002478 weston_surface_set_position(ws->drag_surface,
2479 wl_fixed_to_double(seat->pointer->x),
2480 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002481
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002482 surface->configure = drag_surface_configure;
2483
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002484 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002485 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002486
2487 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002488}
2489
2490static void
Daniel Stone37816df2012-05-16 18:45:18 +01002491device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002492{
Daniel Stone37816df2012-05-16 18:45:18 +01002493 seat->drag_surface->configure = NULL;
2494 undef_region(&seat->drag_surface->input);
2495 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2496 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002497}
2498
2499static void
Daniel Stone37816df2012-05-16 18:45:18 +01002500device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002501{
Daniel Stone37816df2012-05-16 18:45:18 +01002502 if (weston_surface_is_mapped(seat->drag_surface) ||
2503 !seat->drag_surface->buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002504 return;
2505
Daniel Stone37816df2012-05-16 18:45:18 +01002506 wl_list_insert(&seat->sprite->layer_link,
2507 &seat->drag_surface->layer_link);
2508 weston_surface_assign_output(seat->drag_surface);
2509 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002510}
2511
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002512static void
Daniel Stone37816df2012-05-16 18:45:18 +01002513weston_seat_update_drag_surface(struct wl_seat *seat,
2514 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002515{
2516 int surface_changed = 0;
Daniel Stone37816df2012-05-16 18:45:18 +01002517 struct weston_seat *ws = (struct weston_seat *) seat;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002518
Daniel Stone37816df2012-05-16 18:45:18 +01002519 if (!ws->drag_surface && !seat->drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002520 return;
2521
Daniel Stone37816df2012-05-16 18:45:18 +01002522 if (ws->drag_surface && seat->drag_surface &&
2523 (&ws->drag_surface->surface.resource !=
2524 &seat->drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002525 /* between calls to this funcion we got a new drag_surface */
2526 surface_changed = 1;
2527
Daniel Stone37816df2012-05-16 18:45:18 +01002528 if (!seat->drag_surface || surface_changed) {
2529 device_release_drag_surface(ws);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002530 if (!surface_changed)
2531 return;
2532 }
2533
Daniel Stone37816df2012-05-16 18:45:18 +01002534 if (!ws->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002535 struct weston_surface *surface = (struct weston_surface *)
Daniel Stone37816df2012-05-16 18:45:18 +01002536 seat->drag_surface;
2537 if (!device_setup_new_drag_surface(ws, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002538 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002539 }
2540
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002541 /* the client may not have attached a buffer to the drag surface
2542 * when we setup it up, so check if map is needed on every update */
Daniel Stone37816df2012-05-16 18:45:18 +01002543 device_map_drag_surface(ws);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002544
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02002545 /* the client may have attached a buffer with a different size to
2546 * the drag surface, causing the input region to be reset */
Daniel Stone37816df2012-05-16 18:45:18 +01002547 if (region_is_undefined(&ws->drag_surface->input))
2548 empty_region(&ws->drag_surface->input);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02002549
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002550 if (!dx && !dy)
2551 return;
2552
Daniel Stone37816df2012-05-16 18:45:18 +01002553 weston_surface_set_position(ws->drag_surface,
2554 ws->drag_surface->geometry.x + wl_fixed_to_double(dx),
2555 ws->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002556}
2557
2558WL_EXPORT void
2559weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2560{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002561 struct weston_seat *seat;
2562
2563 wl_list_for_each(seat, &compositor->seat_list, link)
2564 weston_seat_update_drag_surface(&seat->seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002565}
2566
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002567static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002568bind_output(struct wl_client *client,
2569 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002570{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002571 struct weston_output *output = data;
2572 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002573 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002574
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002575 resource = wl_client_add_object(client,
2576 &wl_output_interface, NULL, id, data);
2577
Casey Dahlin9074db52012-04-19 22:50:09 -04002578 wl_list_insert(&output->resource_list, &resource->link);
2579 resource->destroy = unbind_resource;
2580
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002581 wl_output_send_geometry(resource,
2582 output->x,
2583 output->y,
2584 output->mm_width,
2585 output->mm_height,
2586 output->subpixel,
2587 output->make, output->model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002588
2589 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002590 wl_output_send_mode(resource,
2591 mode->flags,
2592 mode->width,
2593 mode->height,
2594 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002595 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002596}
2597
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002598static const char vertex_shader[] =
2599 "uniform mat4 proj;\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05002600 "attribute vec2 position;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002601 "attribute vec2 texcoord;\n"
2602 "varying vec2 v_texcoord;\n"
2603 "void main()\n"
2604 "{\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05002605 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002606 " v_texcoord = texcoord;\n"
2607 "}\n";
2608
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002609static const char texture_fragment_shader[] =
Kristian Høgsbergdfce71d2010-12-07 20:19:10 -05002610 "precision mediump float;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002611 "varying vec2 v_texcoord;\n"
2612 "uniform sampler2D tex;\n"
Kristian Høgsberg541e5552011-12-14 09:24:11 -05002613 "uniform float alpha;\n"
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02002614 "uniform float texwidth;\n"
Kristian Høgsberg0b0c2bb2012-05-15 23:16:53 -04002615 "uniform vec4 opaque;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002616 "void main()\n"
2617 "{\n"
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02002618 " if (v_texcoord.x < 0.0 || v_texcoord.x > texwidth ||\n"
2619 " v_texcoord.y < 0.0 || v_texcoord.y > 1.0)\n"
2620 " discard;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002621 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
Kristian Høgsberg0b0c2bb2012-05-15 23:16:53 -04002622 " if (opaque.x <= v_texcoord.x && v_texcoord.x < opaque.y &&\n"
2623 " opaque.z <= v_texcoord.y && v_texcoord.y < opaque.w)\n"
2624 " gl_FragColor.a = 1.0;\n"
Kristian Høgsberg541e5552011-12-14 09:24:11 -05002625 " gl_FragColor = alpha * gl_FragColor;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002626 "}\n";
2627
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002628static const char solid_fragment_shader[] =
2629 "precision mediump float;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002630 "uniform vec4 color;\n"
Kristian Høgsbergdbae80e2012-03-29 11:34:39 -04002631 "uniform float alpha;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002632 "void main()\n"
2633 "{\n"
Kristian Høgsbergdbae80e2012-03-29 11:34:39 -04002634 " gl_FragColor = alpha * color\n;"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002635 "}\n";
2636
Kristian Høgsberga9468212010-06-14 21:03:11 -04002637static int
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002638compile_shader(GLenum type, const char *source)
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002639{
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002640 GLuint s;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002641 char msg[512];
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002642 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002643
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002644 s = glCreateShader(type);
2645 glShaderSource(s, 1, &source, NULL);
2646 glCompileShader(s);
2647 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002648 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002649 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
2650 fprintf(stderr, "shader info: %s\n", msg);
2651 return GL_NONE;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002652 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002653
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002654 return s;
2655}
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002656
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002657static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002658weston_shader_init(struct weston_shader *shader,
2659 const char *vertex_source, const char *fragment_source)
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002660{
2661 char msg[512];
2662 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002663
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002664 shader->vertex_shader =
2665 compile_shader(GL_VERTEX_SHADER, vertex_source);
2666 shader->fragment_shader =
2667 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
2668
2669 shader->program = glCreateProgram();
2670 glAttachShader(shader->program, shader->vertex_shader);
2671 glAttachShader(shader->program, shader->fragment_shader);
2672 glBindAttribLocation(shader->program, 0, "position");
2673 glBindAttribLocation(shader->program, 1, "texcoord");
2674
2675 glLinkProgram(shader->program);
2676 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002677 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002678 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002679 fprintf(stderr, "link info: %s\n", msg);
2680 return -1;
2681 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002682
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002683 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
2684 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
Kristian Høgsberg541e5552011-12-14 09:24:11 -05002685 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
Pekka Paalanenf55f5442012-02-02 16:49:05 +02002686 shader->color_uniform = glGetUniformLocation(shader->program, "color");
Scott Moreau9fb98242012-05-22 10:18:50 -06002687 shader->texwidth_uniform = glGetUniformLocation(shader->program, "texwidth");
2688 shader->opaque_uniform = glGetUniformLocation(shader->program, "opaque");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002689
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002690 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002691}
2692
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002693WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002694weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002695{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002696 struct weston_compositor *c = output->compositor;
2697
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002698 pixman_region32_fini(&output->region);
Kristian Høgsberg8b72f602011-06-23 20:46:34 -04002699 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002700 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002701
2702 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002703}
2704
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002705WL_EXPORT void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002706weston_text_cursor_position_notify(struct weston_surface *surface,
Scott Moreauae712202012-06-01 12:46:09 -06002707 wl_fixed_t cur_pos_x,
2708 wl_fixed_t cur_pos_y)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002709{
2710 struct weston_output *output;
Scott Moreauae712202012-06-01 12:46:09 -06002711 wl_fixed_t global_x, global_y;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002712
Scott Moreauae712202012-06-01 12:46:09 -06002713 weston_surface_to_global_fixed(surface, cur_pos_x, cur_pos_y,
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002714 &global_x, &global_y);
2715
2716 wl_list_for_each(output, &surface->compositor->output_list, link)
2717 if (output->zoom.active &&
2718 pixman_region32_contains_point(&output->region,
Scott Moreauae712202012-06-01 12:46:09 -06002719 wl_fixed_to_int(global_x),
2720 wl_fixed_to_int(global_y),
2721 NULL))
2722 weston_output_update_zoom(output, global_x, global_y,
2723 ZOOM_TEXT_CURSOR);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002724}
2725
2726WL_EXPORT void
2727weston_output_update_zoom(struct weston_output *output,
Scott Moreau3f79d662012-06-07 09:12:31 -06002728 wl_fixed_t x,
2729 wl_fixed_t y,
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002730 uint32_t type)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002731{
Scott Moreau3f79d662012-06-07 09:12:31 -06002732 float global_x, global_y;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002733 float trans_min, trans_max;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002734
Scott Moreau850ca422012-05-21 15:21:25 -06002735 if (output->zoom.level >= 1.0)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002736 return;
2737
Scott Moreau3f79d662012-06-07 09:12:31 -06002738 global_x = wl_fixed_to_double(x);
2739 global_y = wl_fixed_to_double(y);
Scott Moreaudc549932012-05-16 08:36:42 -06002740
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002741 output->zoom.trans_x =
Scott Moreau3f79d662012-06-07 09:12:31 -06002742 (((global_x - output->x) / output->current->width) *
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002743 (output->zoom.level * 2)) - output->zoom.level;
2744 output->zoom.trans_y =
Scott Moreau3f79d662012-06-07 09:12:31 -06002745 (((global_y - output->y) / output->current->height) *
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002746 (output->zoom.level * 2)) - output->zoom.level;
2747
2748 if (type == ZOOM_TEXT_CURSOR) {
2749 output->zoom.trans_x *= 1 / output->zoom.level;
2750 output->zoom.trans_y *= 1 / output->zoom.level;
2751
2752 trans_max = output->zoom.level * 2 - output->zoom.level;
2753 trans_min = -trans_max;
2754
2755 if (output->zoom.trans_x > trans_max)
2756 output->zoom.trans_x = trans_max;
2757 else if (output->zoom.trans_x < trans_min)
2758 output->zoom.trans_x = trans_min;
2759 if (output->zoom.trans_y > trans_max)
2760 output->zoom.trans_y = trans_max;
2761 else if (output->zoom.trans_y < trans_min)
2762 output->zoom.trans_y = trans_min;
2763 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002764
2765 output->dirty = 1;
2766 weston_output_damage(output);
2767}
2768
2769WL_EXPORT void
2770weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002771{
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002772 int flip;
Scott Moreau850ca422012-05-21 15:21:25 -06002773 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002774 struct weston_matrix camera;
2775 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002776
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002777 weston_matrix_init(&output->matrix);
2778 weston_matrix_translate(&output->matrix,
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002779 -(output->x + (output->border.right + output->current->width - output->border.left) / 2.0),
2780 -(output->y + (output->border.bottom + output->current->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002781
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002782 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002783 weston_matrix_scale(&output->matrix,
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002784 2.0 / (output->current->width + output->border.left + output->border.right),
2785 flip * 2.0 / (output->current->height + output->border.top + output->border.bottom), 1);
Scott Moreau850ca422012-05-21 15:21:25 -06002786
Scott Moreauccbf29d2012-02-22 14:21:41 -07002787 if (output->zoom.active) {
Scott Moreau850ca422012-05-21 15:21:25 -06002788 magnification = 1 / (1 - output->zoom.level);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002789 weston_matrix_init(&camera);
2790 weston_matrix_init(&modelview);
2791 weston_matrix_translate(&camera, output->zoom.trans_x, flip * output->zoom.trans_y, 0);
2792 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002793 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002794 weston_matrix_multiply(&output->matrix, &modelview);
2795 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002796
Scott Moreauccbf29d2012-02-22 14:21:41 -07002797 output->dirty = 0;
2798}
2799
2800WL_EXPORT void
2801weston_output_move(struct weston_output *output, int x, int y)
2802{
2803 output->x = x;
2804 output->y = y;
2805
2806 pixman_region32_init(&output->previous_damage);
2807 pixman_region32_init_rect(&output->region, x, y,
2808 output->current->width,
2809 output->current->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002810}
2811
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002812WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002813weston_output_init(struct weston_output *output, struct weston_compositor *c,
2814 int x, int y, int width, int height, uint32_t flags)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002815{
2816 output->compositor = c;
2817 output->x = x;
2818 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002819 output->border.top = 0;
2820 output->border.bottom = 0;
2821 output->border.left = 0;
2822 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002823 output->mm_width = width;
2824 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002825 output->dirty = 1;
Scott Moreau062be7e2012-04-20 13:37:33 -06002826 wl_list_init(&output->read_pixels_list);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002827
2828 output->zoom.active = 0;
2829 output->zoom.increment = 0.05;
Scott Moreau850ca422012-05-21 15:21:25 -06002830 output->zoom.level = 0.0;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002831 output->zoom.trans_x = 0.0;
2832 output->zoom.trans_y = 0.0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002833
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002834 output->flags = flags;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002835 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002836 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002837
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002838 wl_signal_init(&output->frame_signal);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002839 wl_list_init(&output->frame_callback_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002840 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002841
Casey Dahlin58ba1372012-04-19 22:50:08 -04002842 output->id = ffs(~output->compositor->output_id_pool) - 1;
2843 output->compositor->output_id_pool |= 1 << output->id;
2844
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002845 output->global =
2846 wl_display_add_global(c->wl_display, &wl_output_interface,
2847 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002848}
2849
Scott Moreau062be7e2012-04-20 13:37:33 -06002850WL_EXPORT void
2851weston_output_do_read_pixels(struct weston_output *output)
2852{
2853 struct weston_read_pixels *r, *next;
2854
2855 glPixelStorei(GL_PACK_ALIGNMENT, 1);
2856 wl_list_for_each_safe(r, next, &output->read_pixels_list, link) {
2857 glReadPixels(r->x, r->y, r->width, r->height,
2858 output->compositor->read_format,
2859 GL_UNSIGNED_BYTE, r->data);
2860 r->done(r, output);
2861 }
2862}
2863
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002864static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002865compositor_bind(struct wl_client *client,
2866 void *data, uint32_t version, uint32_t id)
2867{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002868 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002869
2870 wl_client_add_object(client, &wl_compositor_interface,
2871 &compositor_interface, id, compositor);
2872}
2873
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002874WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002875weston_compositor_init(struct weston_compositor *ec,
2876 struct wl_display *display,
2877 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04002878 char *argv[],
2879 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002880{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002881 struct wl_event_loop *loop;
Kristian Høgsbergd711d0c2011-01-14 17:28:21 -05002882 const char *extensions;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002883 struct xkb_rule_names xkb_names;
2884 const struct config_key keyboard_config_keys[] = {
2885 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2886 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2887 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2888 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2889 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2890 };
2891 const struct config_section cs[] = {
2892 { "keyboard",
2893 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
2894 };
2895
2896 memset(&xkb_names, 0, sizeof(xkb_names));
2897 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002898
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002899 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002900 wl_signal_init(&ec->destroy_signal);
2901 wl_signal_init(&ec->activate_signal);
2902 wl_signal_init(&ec->lock_signal);
2903 wl_signal_init(&ec->unlock_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002904 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002905
Casey Dahlin58ba1372012-04-19 22:50:08 -04002906 ec->output_id_pool = 0;
2907
Kristian Høgsberga8873122011-11-23 10:39:34 -05002908 if (!wl_display_add_global(display, &wl_compositor_interface,
2909 ec, compositor_bind))
2910 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002911
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002912 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002913
2914 ec->image_target_texture_2d =
2915 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2916 ec->image_target_renderbuffer_storage = (void *)
2917 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
2918 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2919 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2920 ec->bind_display =
2921 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
2922 ec->unbind_display =
2923 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
2924
2925 extensions = (const char *) glGetString(GL_EXTENSIONS);
Pekka Paalanen299e58d2012-04-12 14:45:35 +03002926 if (!extensions) {
2927 fprintf(stderr, "Retrieving GL extension string failed.\n");
2928 return -1;
2929 }
2930
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002931 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
2932 fprintf(stderr,
2933 "GL_EXT_texture_format_BGRA8888 not available\n");
2934 return -1;
2935 }
2936
Pekka Paalanena1d57db2012-04-17 15:02:08 +03002937 if (strstr(extensions, "GL_EXT_read_format_bgra"))
2938 ec->read_format = GL_BGRA_EXT;
2939 else
2940 ec->read_format = GL_RGBA;
Kristian Høgsbergf9247dd2012-03-27 15:25:46 -04002941
Pekka Paalanen52bfbaa2012-04-11 16:19:37 +03002942 if (strstr(extensions, "GL_EXT_unpack_subimage"))
2943 ec->has_unpack_subimage = 1;
Kristian Høgsbergfb6de222012-03-27 17:10:13 -04002944
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002945 extensions =
2946 (const char *) eglQueryString(ec->display, EGL_EXTENSIONS);
Pekka Paalanen299e58d2012-04-12 14:45:35 +03002947 if (!extensions) {
2948 fprintf(stderr, "Retrieving EGL extension string failed.\n");
2949 return -1;
2950 }
2951
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002952 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
2953 ec->has_bind_display = 1;
2954 if (ec->has_bind_display)
2955 ec->bind_display(ec->display, ec->wl_display);
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04002956
Kristian Høgsberg201a9042008-12-10 00:40:50 -05002957 wl_list_init(&ec->surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002958 wl_list_init(&ec->layer_list);
Daniel Stone37816df2012-05-16 18:45:18 +01002959 wl_list_init(&ec->seat_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002960 wl_list_init(&ec->output_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01002961 wl_list_init(&ec->key_binding_list);
2962 wl_list_init(&ec->button_binding_list);
2963 wl_list_init(&ec->axis_binding_list);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002964 wl_list_init(&ec->animation_list);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002965 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002966 ec->fade.animation.frame = fade_frame;
2967 wl_list_init(&ec->fade.animation.link);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04002968
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002969 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2970 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2971
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002972 screenshooter_create(ec);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002973 text_cursor_position_notifier_create(ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002974
Scott Moreauff1db4a2012-04-17 19:06:18 -06002975 ec->ping_handler = NULL;
2976
Kristian Høgsbergdade6492012-01-04 21:47:30 -05002977 wl_data_device_manager_init(ec->wl_display);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002978
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002979 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002980
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002981 if (weston_shader_init(&ec->texture_shader,
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002982 vertex_shader, texture_fragment_shader) < 0)
Kristian Høgsberga9468212010-06-14 21:03:11 -04002983 return -1;
Pekka Paalanenf55f5442012-02-02 16:49:05 +02002984 if (weston_shader_init(&ec->solid_shader,
2985 vertex_shader, solid_fragment_shader) < 0)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002986 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05002987
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002988 weston_compositor_xkb_init(ec, &xkb_names);
2989
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002990 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002991 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Pekka Paalanen7296e792011-12-07 16:22:00 +02002992 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002993
Kristian Høgsberg7ea10862012-03-05 17:49:30 -05002994 ec->input_loop = wl_event_loop_create();
2995
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002996 weston_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002997
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002998 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002999}
3000
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003001WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003002weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003003{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003004 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003005
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003006 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003007 if (ec->input_loop_source)
3008 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003009
Matt Roper361d2ad2011-08-29 13:52:23 -07003010 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003011 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003012 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003013
Daniel Stone325fc2d2012-05-30 16:31:58 +01003014 weston_binding_list_destroy_all(&ec->key_binding_list);
3015 weston_binding_list_destroy_all(&ec->button_binding_list);
3016 weston_binding_list_destroy_all(&ec->axis_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003017
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003018 wl_array_release(&ec->vertices);
3019 wl_array_release(&ec->indices);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003020
3021 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07003022}
3023
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003024static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003025{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003026 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003027
Kristian Høgsberg3cad4362011-07-15 21:09:24 -04003028 fprintf(stderr, "caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003029 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003030
3031 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003032}
3033
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003034static void
3035on_segv_signal(int s, siginfo_t *siginfo, void *context)
3036{
3037 void *buffer[32];
3038 int i, count;
3039 Dl_info info;
3040
3041 fprintf(stderr, "caught segv\n");
3042
3043 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3044 for (i = 0; i < count; i++) {
3045 dladdr(buffer[i], &info);
3046 fprintf(stderr, " [%016lx] %s (%s)\n",
3047 (long) buffer[i],
3048 info.dli_sname ? info.dli_sname : "--",
3049 info.dli_fname);
3050 }
3051
3052 longjmp(segv_jmp_buf, 1);
3053}
3054
3055
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003056static void *
3057load_module(const char *name, const char *entrypoint, void **handle)
3058{
3059 char path[PATH_MAX];
3060 void *module, *init;
3061
3062 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003063 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003064 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003065 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003066
3067 module = dlopen(path, RTLD_LAZY);
3068 if (!module) {
3069 fprintf(stderr,
Pekka Paalanenf31ad102012-04-26 10:31:36 +03003070 "failed to load module '%s': %s\n", path, dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003071 return NULL;
3072 }
3073
3074 init = dlsym(module, entrypoint);
3075 if (!init) {
3076 fprintf(stderr,
Pekka Paalanenf31ad102012-04-26 10:31:36 +03003077 "failed to lookup init function in '%s': %s\n",
3078 path, dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003079 return NULL;
3080 }
3081
3082 return init;
3083}
3084
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003085static const char xdg_error_message[] =
3086 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n"
3087 "Refer to your distribution on how to get it, or\n"
3088 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3089 "on how to implement it.\n";
3090
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003091int main(int argc, char *argv[])
3092{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003093 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003094 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003095 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003096 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003097 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003098 struct sigaction segv_action;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04003099 void *shell_module, *backend_module, *xserver_module;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003100 int (*module_init)(struct weston_compositor *ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003101 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003102 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003103 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003104 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003105 char *backend = NULL;
3106 char *shell = NULL;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003107 char *module = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003108 int32_t idle_time = 300;
Scott Moreaue17f6102012-04-25 10:03:06 -06003109 int32_t xserver = 0;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003110 char *socket_name = NULL;
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003111 char *config_file;
3112
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003113 const struct config_key shell_config_keys[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003114 { "type", CONFIG_KEY_STRING, &shell },
3115 };
3116
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003117 const struct config_section cs[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003118 { "shell",
3119 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
3120 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003121
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003122 const struct weston_option core_options[] = {
3123 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3124 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3125 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003126 { WESTON_OPTION_BOOLEAN, "xserver", 0, &xserver },
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003127 { WESTON_OPTION_STRING, "module", 0, &module },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003128 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003129
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003130 argc = parse_options(core_options,
3131 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003132
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003133 if (!getenv("XDG_RUNTIME_DIR")) {
3134 fprintf(stderr, xdg_error_message);
3135 exit(EXIT_FAILURE);
3136 }
3137
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003138 display = wl_display_create();
3139
Tiago Vignatti2116b892011-08-08 05:52:59 -07003140 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003141 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3142 display);
3143 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3144 display);
3145 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3146 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003147
3148 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003149 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3150 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003151
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003152 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3153 segv_action.sa_sigaction = on_segv_signal;
Pekka Paalanen8423a892012-01-19 16:48:37 +02003154 sigemptyset(&segv_action.sa_mask);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003155 sigaction(SIGSEGV, &segv_action, NULL);
3156
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003157 if (!backend) {
3158 if (getenv("WAYLAND_DISPLAY"))
3159 backend = "wayland-backend.so";
3160 else if (getenv("DISPLAY"))
3161 backend = "x11-backend.so";
3162 else if (getenv("OPENWFD"))
3163 backend = "openwfd-backend.so";
3164 else
3165 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003166 }
3167
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003168 config_file = config_file_path("weston.ini");
3169 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003170
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003171 backend_init = load_module(backend, "backend_init", &backend_module);
3172 if (!backend_init)
3173 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003174
Daniel Stonec1be8e52012-06-01 11:14:02 -04003175 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003176 if (ec == NULL) {
3177 fprintf(stderr, "failed to create compositor\n");
3178 exit(EXIT_FAILURE);
3179 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003180
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003181 for (i = 1; argv[i]; i++)
3182 fprintf(stderr, "unhandled option: %s\n", argv[i]);
3183 if (argv[1])
3184 exit(EXIT_FAILURE);
3185
Daniel Stonec1be8e52012-06-01 11:14:02 -04003186 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01003187
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003188 ec->option_idle_time = idle_time;
3189 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003190
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003191 module_init = NULL;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04003192 if (xserver)
Tiago Vignatti629ce232012-05-23 23:04:14 +03003193 module_init = load_module("xwayland.so",
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003194 "weston_xserver_init",
3195 &xserver_module);
3196 if (module_init && module_init(ec) < 0)
3197 exit(EXIT_FAILURE);
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04003198
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003199 if (!shell)
3200 shell = "desktop-shell.so";
3201 module_init = load_module(shell, "shell_init", &shell_module);
3202 if (!module_init || module_init(ec) < 0)
3203 exit(EXIT_FAILURE);
3204
3205
3206 module_init = NULL;
3207 if (module)
3208 module_init = load_module(module, "module_init", NULL);
3209 if (module_init && module_init(ec) < 0)
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003210 exit(EXIT_FAILURE);
3211
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003212 if (wl_display_add_socket(display, socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003213 fprintf(stderr, "failed to add socket: %m\n");
3214 exit(EXIT_FAILURE);
3215 }
3216
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003217 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003218 weston_compositor_wake(ec);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003219 if (setjmp(segv_jmp_buf) == 0)
3220 wl_display_run(display);
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003221 else
3222 ret = EXIT_FAILURE;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003223
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003224 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05003225 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003226
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003227 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003228
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003229 if (ec->has_bind_display)
3230 ec->unbind_display(ec->display, display);
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003231
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003232 for (i = ARRAY_LENGTH(signals); i;)
3233 wl_event_source_remove(signals[--i]);
3234
Daniel Stone855028f2012-05-01 20:37:10 +01003235 weston_compositor_xkb_destroy(ec);
3236
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003237 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003238 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003239
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003240 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003241}