blob: 9ce44d49b0dca3df7add61f551eb9c8301682b3b [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanend581a8f2012-01-27 16:25:16 +02004 * Copyright © 2012 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04006 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050015 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040016 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Kristian Høgsberg06bc2642010-12-01 09:50:16 -050025#define _GNU_SOURCE
26
Kristian Høgsberga9410222011-01-14 17:22:35 -050027#include "config.h"
28
Daniel Stoneb7452fe2012-06-01 12:14:06 +010029#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040030#include <stdio.h>
31#include <string.h>
32#include <stdlib.h>
33#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010034#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050035#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020036#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040037#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010038#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040039#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020040#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020041#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020042#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040043#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050044#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040045#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040046#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040047#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050048#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040049#include <sys/time.h>
50#include <time.h>
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -040051#include <ctype.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050052
Pekka Paalanen50719bc2011-11-22 14:18:50 +020053#include <wayland-server.h>
Kristian Høgsberg82863022010-06-04 21:52:02 -040054#include "compositor.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030055#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040056#include "git-version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050057
Pekka Paalanen07c91f82012-08-30 16:47:21 -050058#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
59
60
Kristian Høgsberg27da5382011-06-21 17:32:25 -040061static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040062static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040063
64static int
65sigchld_handler(int signal_number, void *data)
66{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050067 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040068 int status;
69 pid_t pid;
70
Bill Spitzak027b9622012-03-17 15:22:03 -070071 pid = waitpid(-1, &status, WNOHANG);
72 if (!pid)
73 return 1;
74
Kristian Høgsberg27da5382011-06-21 17:32:25 -040075 wl_list_for_each(p, &child_process_list, link) {
76 if (p->pid == pid)
77 break;
78 }
79
80 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020081 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040082 return 1;
83 }
84
85 wl_list_remove(&p->link);
86 p->cleanup(p, status);
87
88 return 1;
89}
90
Alex Wu2dda6042012-04-17 17:20:47 +080091WL_EXPORT int
92weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode)
93{
94 if (!output->switch_mode)
95 return -1;
96
97 return output->switch_mode(output, mode);
98}
99
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400100WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500101weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400102{
103 wl_list_insert(&child_process_list, &process->link);
104}
105
Benjamin Franzke06286262011-05-06 19:12:33 +0200106static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200107child_client_exec(int sockfd, const char *path)
108{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500109 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200110 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200111 sigset_t allsigs;
112
113 /* do not give our signal mask to the new process */
114 sigfillset(&allsigs);
115 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200116
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500117 /* Launch clients as the user. */
118 seteuid(getuid());
119
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500120 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
121 * non-CLOEXEC fd to pass through exec. */
122 clientfd = dup(sockfd);
123 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200124 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500125 return;
126 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200127
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500128 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200129 setenv("WAYLAND_SOCKET", s, 1);
130
131 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200132 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200133 path);
134}
135
136WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500137weston_client_launch(struct weston_compositor *compositor,
138 struct weston_process *proc,
139 const char *path,
140 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200141{
142 int sv[2];
143 pid_t pid;
144 struct wl_client *client;
145
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300146 weston_log("launching '%s'\n", path);
147
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300148 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200149 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200150 "socketpair failed while launching '%s': %m\n",
151 path);
152 return NULL;
153 }
154
155 pid = fork();
156 if (pid == -1) {
157 close(sv[0]);
158 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200159 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200160 "fork failed while launching '%s': %m\n", path);
161 return NULL;
162 }
163
164 if (pid == 0) {
165 child_client_exec(sv[1], path);
166 exit(-1);
167 }
168
169 close(sv[1]);
170
171 client = wl_client_create(compositor->wl_display, sv[0]);
172 if (!client) {
173 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200174 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200175 "wl_client_create failed while launching '%s'.\n",
176 path);
177 return NULL;
178 }
179
180 proc->pid = pid;
181 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500182 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200183
184 return client;
185}
186
187static void
Kristian Høgsberga85b4fb2012-06-20 09:24:35 -0400188update_shm_texture(struct weston_surface *surface);
189
190static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400191surface_handle_buffer_destroy(struct wl_listener *listener, void *data)
Benjamin Franzke06286262011-05-06 19:12:33 +0200192{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500193 struct weston_surface *es =
194 container_of(listener, struct weston_surface,
195 buffer_destroy_listener);
Benjamin Franzke06286262011-05-06 19:12:33 +0200196
Kristian Høgsberga85b4fb2012-06-20 09:24:35 -0400197 if (es->buffer && wl_buffer_is_shm(es->buffer))
198 update_shm_texture(es);
199
Kristian Høgsberg24596942011-10-24 17:51:02 -0400200 es->buffer = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200201}
202
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500203static const pixman_region32_data_t undef_region_data;
204
205static void
206undef_region(pixman_region32_t *region)
207{
Kristian Høgsberg66a099b2012-05-29 16:49:45 -0400208 if (region->data != &undef_region_data)
209 pixman_region32_fini(region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500210 region->data = (pixman_region32_data_t *) &undef_region_data;
211}
212
213static int
214region_is_undefined(pixman_region32_t *region)
215{
216 return region->data == &undef_region_data;
217}
218
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200219static void
220empty_region(pixman_region32_t *region)
221{
222 if (!region_is_undefined(region))
223 pixman_region32_fini(region);
224
225 pixman_region32_init(region);
226}
227
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500228WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500229weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500230{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500231 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400232
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200233 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400234 if (surface == NULL)
235 return NULL;
236
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400237 wl_signal_init(&surface->surface.resource.destroy_signal);
Kristian Høgsberg48891542012-02-23 17:38:33 -0500238
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500239 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500240 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500241
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400242 surface->surface.resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400243
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500244 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400245 surface->alpha = 1.0;
Kristian Høgsberg0b0c2bb2012-05-15 23:16:53 -0400246 surface->opaque_rect[0] = 0.0;
247 surface->opaque_rect[1] = 0.0;
248 surface->opaque_rect[2] = 0.0;
249 surface->opaque_rect[3] = 0.0;
Juan Zhao46436612012-03-20 01:48:46 +0800250 surface->pitch = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400251
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200252 surface->num_textures = 0;
253 surface->num_images = 0;
254
Benjamin Franzke06286262011-05-06 19:12:33 +0200255 surface->buffer = NULL;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400256 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400257 surface->plane = &compositor->primary_plane;
Benjamin Franzke06286262011-05-06 19:12:33 +0200258
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400259 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500260 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500261 pixman_region32_init(&surface->clip);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500262 undef_region(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200263 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500264 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400265
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400266 surface->buffer_destroy_listener.notify =
267 surface_handle_buffer_destroy;
Benjamin Franzke06286262011-05-06 19:12:33 +0200268
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200269 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200270 wl_list_insert(&surface->geometry.transformation_list,
271 &surface->transform.position.link);
272 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200273 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200274 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500275
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400276 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500277}
278
Alex Wu8811bf92012-02-28 18:07:54 +0800279WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500280weston_surface_set_color(struct weston_surface *surface,
281 GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
282{
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500283 surface->color[0] = red;
284 surface->color[1] = green;
285 surface->color[2] = blue;
286 surface->color[3] = alpha;
287 surface->shader = &surface->compositor->solid_shader;
288}
289
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400290WL_EXPORT void
291weston_surface_to_global_float(struct weston_surface *surface,
292 GLfloat sx, GLfloat sy, GLfloat *x, GLfloat *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200293{
294 if (surface->transform.enabled) {
295 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
296
297 weston_matrix_transform(&surface->transform.matrix, &v);
298
299 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200300 weston_log("warning: numerical instability in "
Pekka Paalanenece8a012012-02-08 15:23:15 +0200301 "weston_surface_to_global(), divisor = %g\n",
302 v.f[3]);
303 *x = 0;
304 *y = 0;
305 return;
306 }
307
308 *x = v.f[0] / v.f[3];
309 *y = v.f[1] / v.f[3];
310 } else {
311 *x = sx + surface->geometry.x;
312 *y = sy + surface->geometry.y;
313 }
314}
315
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500316WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400317weston_surface_move_to_plane(struct weston_surface *surface,
318 struct weston_plane *plane)
319{
320 if (surface->plane == plane)
321 return;
322
323 weston_surface_damage_below(surface);
324 surface->plane = plane;
325 weston_surface_damage(surface);
326}
327
328WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500329weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200330{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500331 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200332
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500333 pixman_region32_init(&damage);
334 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
335 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400336 pixman_region32_union(&surface->plane->damage,
337 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500338 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200339}
340
341static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200342surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
343 int32_t width, int32_t height,
344 pixman_region32_t *bbox)
345{
Pekka Paalanen219b9822012-02-08 15:38:37 +0200346 GLfloat min_x = HUGE_VALF, min_y = HUGE_VALF;
347 GLfloat max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200348 int32_t s[4][2] = {
349 { sx, sy },
350 { sx, sy + height },
351 { sx + width, sy },
352 { sx + width, sy + height }
353 };
Pekka Paalanen219b9822012-02-08 15:38:37 +0200354 GLfloat int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200355 int i;
356
357 for (i = 0; i < 4; ++i) {
Pekka Paalanen219b9822012-02-08 15:38:37 +0200358 GLfloat x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400359 weston_surface_to_global_float(surface,
360 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200361 if (x < min_x)
362 min_x = x;
363 if (x > max_x)
364 max_x = x;
365 if (y < min_y)
366 min_y = y;
367 if (y > max_y)
368 max_y = y;
369 }
370
Pekka Paalanen219b9822012-02-08 15:38:37 +0200371 int_x = floorf(min_x);
372 int_y = floorf(min_y);
373 pixman_region32_init_rect(bbox, int_x, int_y,
374 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200375}
376
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200377static void
378weston_surface_update_transform_disable(struct weston_surface *surface)
379{
380 surface->transform.enabled = 0;
381
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200382 /* round off fractions when not transformed */
383 surface->geometry.x = roundf(surface->geometry.x);
384 surface->geometry.y = roundf(surface->geometry.y);
385
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200386 pixman_region32_init_rect(&surface->transform.boundingbox,
387 surface->geometry.x,
388 surface->geometry.y,
389 surface->geometry.width,
390 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500391
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400392 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500393 pixman_region32_copy(&surface->transform.opaque,
394 &surface->opaque);
395 pixman_region32_translate(&surface->transform.opaque,
396 surface->geometry.x,
397 surface->geometry.y);
398 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200399}
400
401static int
402weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200403{
404 struct weston_matrix *matrix = &surface->transform.matrix;
405 struct weston_matrix *inverse = &surface->transform.inverse;
406 struct weston_transform *tform;
407
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200408 surface->transform.enabled = 1;
409
410 /* Otherwise identity matrix, but with x and y translation. */
411 surface->transform.position.matrix.d[12] = surface->geometry.x;
412 surface->transform.position.matrix.d[13] = surface->geometry.y;
413
414 weston_matrix_init(matrix);
415 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
416 weston_matrix_multiply(matrix, &tform->matrix);
417
418 if (weston_matrix_invert(inverse, matrix) < 0) {
419 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200420 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200421 " transformation not invertible.\n", surface);
422 return -1;
423 }
424
425 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
426 surface->geometry.height,
427 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500428
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200429 return 0;
430}
431
432WL_EXPORT void
433weston_surface_update_transform(struct weston_surface *surface)
434{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200435 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200436 return;
437
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200438 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200439
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500440 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200441
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200442 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500443 pixman_region32_fini(&surface->transform.opaque);
444 pixman_region32_init(&surface->transform.opaque);
445
446 if (region_is_undefined(&surface->input))
447 pixman_region32_init_rect(&surface->input, 0, 0,
448 surface->geometry.width,
449 surface->geometry.height);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200450
Pekka Paalanencd403622012-01-25 13:37:39 +0200451 /* transform.position is always in transformation_list */
452 if (surface->geometry.transformation_list.next ==
453 &surface->transform.position.link &&
454 surface->geometry.transformation_list.prev ==
455 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200456 weston_surface_update_transform_disable(surface);
457 } else {
458 if (weston_surface_update_transform_enable(surface) < 0)
459 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200460 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200461
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400462 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200463
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300464 if (weston_surface_is_mapped(surface))
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +0200465 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200466}
467
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200468WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100469weston_surface_to_global_fixed(struct weston_surface *surface,
470 wl_fixed_t sx, wl_fixed_t sy,
471 wl_fixed_t *x, wl_fixed_t *y)
472{
473 GLfloat xf, yf;
474
475 weston_surface_to_global_float(surface,
476 wl_fixed_to_double(sx),
477 wl_fixed_to_double(sy),
478 &xf, &yf);
479 *x = wl_fixed_from_double(xf);
480 *y = wl_fixed_from_double(yf);
481}
482
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200483static void
484surface_from_global_float(struct weston_surface *surface,
Daniel Stonebd3489b2012-05-08 17:17:53 +0100485 GLfloat x, GLfloat y, GLfloat *sx, GLfloat *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200486{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200487 if (surface->transform.enabled) {
488 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
489
490 weston_matrix_transform(&surface->transform.inverse, &v);
491
492 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200493 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200494 "weston_surface_from_global(), divisor = %g\n",
495 v.f[3]);
496 *sx = 0;
497 *sy = 0;
498 return;
499 }
500
Pekka Paalanencd403622012-01-25 13:37:39 +0200501 *sx = v.f[0] / v.f[3];
502 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200503 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200504 *sx = x - surface->geometry.x;
505 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200506 }
507}
508
509WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100510weston_surface_from_global_fixed(struct weston_surface *surface,
511 wl_fixed_t x, wl_fixed_t y,
512 wl_fixed_t *sx, wl_fixed_t *sy)
513{
514 GLfloat sxf, syf;
515
Daniel Stonebd3489b2012-05-08 17:17:53 +0100516 surface_from_global_float(surface,
517 wl_fixed_to_double(x),
518 wl_fixed_to_double(y),
519 &sxf, &syf);
520 *sx = wl_fixed_from_double(sxf);
521 *sy = wl_fixed_from_double(syf);
522}
523
524WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200525weston_surface_from_global(struct weston_surface *surface,
526 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
527{
528 GLfloat sxf, syf;
529
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200530 surface_from_global_float(surface, x, y, &sxf, &syf);
531 *sx = floorf(sxf);
532 *sy = floorf(syf);
533}
534
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400535WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400536weston_surface_schedule_repaint(struct weston_surface *surface)
537{
538 struct weston_output *output;
539
540 wl_list_for_each(output, &surface->compositor->output_list, link)
541 if (surface->output_mask & (1 << output->id))
542 weston_output_schedule_repaint(output);
543}
544
545WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500546weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500547{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400548 pixman_region32_union_rect(&surface->damage, &surface->damage,
549 0, 0, surface->geometry.width,
550 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200551
Kristian Høgsberg98238702012-08-03 16:29:12 -0400552 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500553}
554
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400555WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500556weston_surface_configure(struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200557 GLfloat x, GLfloat y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400558{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200559 surface->geometry.x = x;
560 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200561 surface->geometry.width = width;
562 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200563 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400564}
565
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200566WL_EXPORT void
567weston_surface_set_position(struct weston_surface *surface,
568 GLfloat x, GLfloat y)
569{
570 surface->geometry.x = x;
571 surface->geometry.y = y;
572 surface->geometry.dirty = 1;
573}
574
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300575WL_EXPORT int
576weston_surface_is_mapped(struct weston_surface *surface)
577{
578 if (surface->output)
579 return 1;
580 else
581 return 0;
582}
583
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400584WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500585weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500586{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400587 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500588
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400589 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500590
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400591 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500592}
593
Tiago Vignatti9d393522012-02-10 16:26:19 +0200594static struct weston_surface *
595weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100596 wl_fixed_t x, wl_fixed_t y,
597 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200598{
599 struct weston_surface *surface;
600
601 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100602 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500603 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100604 wl_fixed_to_int(*sx),
605 wl_fixed_to_int(*sy),
606 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200607 return surface;
608 }
609
610 return NULL;
611}
612
613static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400614weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500615{
Scott Moreau447013d2012-02-18 05:05:29 -0700616 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500617 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400618 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500619
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400620 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100621 return;
622
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400623 surface = weston_compositor_pick_surface(seat->compositor,
624 pointer->x,
625 pointer->y,
626 &pointer->current_x,
627 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500628
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400629 if (&surface->surface != pointer->current) {
630 interface = pointer->grab->interface;
631 pointer->current = &surface->surface;
632 interface->focus(pointer->grab, &surface->surface,
633 pointer->current_x,
634 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500635 }
636
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400637 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500638 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100639 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400640 pointer->x,
641 pointer->y,
642 &pointer->grab->x,
643 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500644}
645
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400646static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500647weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400648{
Daniel Stone37816df2012-05-16 18:45:18 +0100649 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400650
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500651 if (!compositor->focus)
652 return;
653
Daniel Stone37816df2012-05-16 18:45:18 +0100654 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400655 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400656}
657
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400658WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500659weston_surface_unmap(struct weston_surface *surface)
660{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100661 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500662
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500663 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500664 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500665 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500666
Daniel Stone4dab5db2012-05-30 16:31:53 +0100667 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100668 if (seat->seat.keyboard &&
669 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100670 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100671 if (seat->seat.pointer &&
672 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100673 wl_pointer_set_focus(seat->seat.pointer,
674 NULL,
675 wl_fixed_from_int(0),
676 wl_fixed_from_int(0));
677 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500678
Kristian Høgsberg98238702012-08-03 16:29:12 -0400679 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500680}
681
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400682struct weston_frame_callback {
683 struct wl_resource resource;
684 struct wl_list link;
685};
686
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500687static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400688destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500689{
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200690 int i;
691
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500692 struct weston_surface *surface =
693 container_of(resource,
694 struct weston_surface, surface.resource);
695 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400696 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400697
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300698 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500699 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400700
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200701 glDeleteTextures(surface->num_textures, surface->textures);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200702
Benjamin Franzke06286262011-05-06 19:12:33 +0200703 if (surface->buffer)
704 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400705
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200706 for (i = 0; i < surface->num_images; i++)
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400707 compositor->destroy_image(compositor->egl_display,
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200708 surface->images[i]);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200709
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200710 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200711 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500712 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500713 pixman_region32_fini(&surface->clip);
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500714 if (!region_is_undefined(&surface->input))
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500715 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200716
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400717 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
718 wl_resource_destroy(&cb->resource);
719
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400720 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500721}
722
Alex Wu8811bf92012-02-28 18:07:54 +0800723WL_EXPORT void
724weston_surface_destroy(struct weston_surface *surface)
725{
726 /* Not a valid way to destroy a client surface */
727 assert(surface->surface.resource.client == NULL);
728
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400729 wl_signal_emit(&surface->surface.resource.destroy_signal,
730 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800731 destroy_surface(&surface->surface.resource);
732}
733
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100734static void
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200735ensure_textures(struct weston_surface *es, int num_textures)
736{
737 int i;
738
739 if (num_textures <= es->num_textures)
740 return;
741
742 for (i = es->num_textures; i < num_textures; i++) {
743 glGenTextures(1, &es->textures[i]);
Rob Clarke3b95912012-08-31 16:42:18 -0500744 glBindTexture(es->target, es->textures[i]);
745 glTexParameteri(es->target,
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200746 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
Rob Clarke3b95912012-08-31 16:42:18 -0500747 glTexParameteri(es->target,
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200748 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
749 }
750 es->num_textures = num_textures;
Rob Clarke3b95912012-08-31 16:42:18 -0500751 glBindTexture(es->target, 0);
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200752}
753
754static void
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -0400755weston_surface_attach(struct wl_surface *surface, struct wl_buffer *buffer)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100756{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500757 struct weston_surface *es = (struct weston_surface *) surface;
758 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400759 EGLint attribs[3], format;
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200760 int i, num_planes;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100761
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300762 if (es->buffer) {
763 weston_buffer_post_release(es->buffer);
764 wl_list_remove(&es->buffer_destroy_listener.link);
765 }
766
767 es->buffer = buffer;
768
769 if (!buffer) {
770 if (weston_surface_is_mapped(es))
771 weston_surface_unmap(es);
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200772 for (i = 0; i < es->num_images; i++) {
773 ec->destroy_image(ec->egl_display, es->images[i]);
774 es->images[i] = NULL;
Kristian Høgsbergb8ceaaa2012-06-19 15:41:12 -0400775 }
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200776 es->num_images = 0;
777 glDeleteTextures(es->num_textures, es->textures);
778 es->num_textures = 0;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300779 return;
780 }
781
782 buffer->busy_count++;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400783 wl_signal_add(&es->buffer->resource.destroy_signal,
784 &es->buffer_destroy_listener);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300785
786 if (es->geometry.width != buffer->width ||
787 es->geometry.height != buffer->height) {
788 undef_region(&es->input);
789 pixman_region32_fini(&es->opaque);
790 pixman_region32_init(&es->opaque);
791 }
792
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100793 if (wl_buffer_is_shm(buffer)) {
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200794 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
Rob Clarke3b95912012-08-31 16:42:18 -0500795 es->target = GL_TEXTURE_2D;
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200796
797 ensure_textures(es, 1);
798 glBindTexture(GL_TEXTURE_2D, es->textures[0]);
Kristian Høgsbergfb6de222012-03-27 17:10:13 -0400799 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
800 es->pitch, es->buffer->height, 0,
801 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
Kristian Høgsbergd6ad1222012-05-17 11:11:15 -0400802 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
Pekka Paalanen6b5585b2012-08-30 16:47:19 -0500803 es->shader = &ec->texture_shader_rgbx;
Kristian Høgsbergd6ad1222012-05-17 11:11:15 -0400804 else
Pekka Paalanen6b5585b2012-08-30 16:47:19 -0500805 es->shader = &ec->texture_shader_rgba;
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200806 } else if (ec->query_buffer(ec->egl_display, buffer,
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400807 EGL_TEXTURE_FORMAT, &format)) {
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200808 for (i = 0; i < es->num_images; i++)
Kristian Høgsberg971cbc82012-07-17 14:21:25 -0400809 ec->destroy_image(ec->egl_display, es->images[i]);
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200810 es->num_images = 0;
Rob Clarke3b95912012-08-31 16:42:18 -0500811 es->target = GL_TEXTURE_2D;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400812 switch (format) {
813 case EGL_TEXTURE_RGB:
814 case EGL_TEXTURE_RGBA:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200815 default:
816 num_planes = 1;
817 es->shader = &ec->texture_shader_rgba;
818 break;
Rob Clarke3b95912012-08-31 16:42:18 -0500819 case EGL_TEXTURE_EXTERNAL_WL:
820 num_planes = 1;
821 es->target = GL_TEXTURE_EXTERNAL_OES;
822 es->shader = &ec->texture_shader_egl_external;
823 break;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400824 case EGL_TEXTURE_Y_UV_WL:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200825 num_planes = 2;
826 es->shader = &ec->texture_shader_y_uv;
827 break;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400828 case EGL_TEXTURE_Y_U_V_WL:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200829 num_planes = 3;
830 es->shader = &ec->texture_shader_y_u_v;
831 break;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400832 case EGL_TEXTURE_Y_XUXV_WL:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200833 num_planes = 2;
834 es->shader = &ec->texture_shader_y_xuxv;
835 break;
836 }
837
838 ensure_textures(es, num_planes);
839 for (i = 0; i < num_planes; i++) {
840 attribs[0] = EGL_WAYLAND_PLANE_WL;
841 attribs[1] = i;
842 attribs[2] = EGL_NONE;
843 es->images[i] = ec->create_image(ec->egl_display,
844 NULL,
845 EGL_WAYLAND_BUFFER_WL,
846 buffer, attribs);
Rob Clark48cd58b2012-08-13 17:39:17 -0500847 if (!es->images[i]) {
848 weston_log("failed to create img for plane %d\n", i);
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200849 continue;
Rob Clark48cd58b2012-08-13 17:39:17 -0500850 }
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200851 es->num_images++;
852
853 glActiveTexture(GL_TEXTURE0 + i);
Rob Clarke3b95912012-08-31 16:42:18 -0500854 glBindTexture(es->target, es->textures[i]);
855 ec->image_target_texture_2d(es->target,
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200856 es->images[i]);
857 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400858
Ander Conselvan de Oliveira9390ae32012-03-27 17:36:38 +0300859 es->pitch = buffer->width;
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200860 } else {
Rob Clark48cd58b2012-08-13 17:39:17 -0500861 weston_log("unhandled buffer type!\n");
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100862 }
863}
864
Rob Clark0e5a2d02012-08-30 16:47:18 -0500865
866#define max(a, b) (((a) > (b)) ? (a) : (b))
867#define min(a, b) (((a) > (b)) ? (b) : (a))
868#define clip(x, a, b) min(max(x, a), b)
869#define sign(x) ((x) >= 0)
870
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400871static int
Rob Clark0e5a2d02012-08-30 16:47:18 -0500872calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
873 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500874{
Rob Clark0e5a2d02012-08-30 16:47:18 -0500875 int i, n = 0;
876 GLfloat min_x, max_x, min_y, max_y;
877 GLfloat x[4] = {
878 surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1,
879 };
880 GLfloat y[4] = {
881 surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2,
882 };
883 GLfloat cx1 = rect->x1;
884 GLfloat cx2 = rect->x2;
885 GLfloat cy1 = rect->y1;
886 GLfloat cy2 = rect->y2;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500887
Rob Clark0e5a2d02012-08-30 16:47:18 -0500888 GLfloat dist_squared(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
889 {
890 GLfloat dx = (x1 - x2);
891 GLfloat dy = (y1 - y2);
892 return dx * dx + dy * dy;
893 }
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400894
Rob Clark0e5a2d02012-08-30 16:47:18 -0500895 void append_vertex(GLfloat x, GLfloat y)
896 {
897 /* don't emit duplicate vertices: */
898 if ((n > 0) && (ex[n-1] == x) && (ey[n-1] == y))
899 return;
900 ex[n] = x;
901 ey[n] = y;
902 n++;
903 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500904
Rob Clark0e5a2d02012-08-30 16:47:18 -0500905 /* transform surface to screen space: */
906 for (i = 0; i < 4; i++)
907 weston_surface_to_global_float(es, x[i], y[i], &x[i], &y[i]);
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500908
Rob Clark0e5a2d02012-08-30 16:47:18 -0500909 /* find bounding box: */
910 min_x = max_x = x[0];
911 min_y = max_y = y[0];
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500912
Rob Clark0e5a2d02012-08-30 16:47:18 -0500913 for (i = 1; i < 4; i++) {
914 min_x = min(min_x, x[i]);
915 max_x = max(max_x, x[i]);
916 min_y = min(min_y, y[i]);
917 max_y = max(max_y, y[i]);
918 }
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500919
Rob Clark0e5a2d02012-08-30 16:47:18 -0500920 /* First, simple bounding box check to discard early transformed
921 * surface rects that do not intersect with the clip region:
922 */
923 if ((min_x > cx2) || (max_x < cx1) ||
924 (min_y > cy2) || (max_y < cy1))
925 return 0;
926
927 /* Simple case, bounding box edges are parallel to surface edges,
928 * there will be only four edges. We just need to clip the surface
929 * vertices to the clip rect bounds:
930 */
931 if (!es->transform.enabled) {
932 for (i = 0; i < 4; i++) {
933 ex[n] = clip(x[i], cx1, cx2);
934 ey[n] = clip(y[i], cy1, cy2);
935 n++;
936 }
937 return 4;
938 }
939
940 /* Hard case, transformation applied. We need to find the vertices
941 * of the shape that is the intersection of the clip rect and
942 * transformed surface. This can be anything from 3 to 8 sides.
943 *
944 * Observation: all the resulting vertices will be the intersection
945 * points of the transformed surface and the clip rect, plus the
946 * vertices of the clip rect which are enclosed by the transformed
947 * surface and the vertices of the transformed surface which are
948 * enclosed by the clip rect.
949 *
950 * Observation: there will be zero, one, or two resulting vertices
951 * for each edge of the src rect.
952 *
953 * Loop over four edges of the transformed rect:
954 */
955 for (i = 0; i < 4; i++) {
956 GLfloat x1, y1, x2, y2;
957 int last_n = n;
958
959 x1 = x[i];
960 y1 = y[i];
961
962 /* if this vertex is contained in the clip rect, use it as-is: */
963 if ((cx1 <= x1) && (x1 <= cx2) &&
964 (cy1 <= y1) && (y1 <= cy2))
965 append_vertex(x1, y1);
966
967 /* for remaining, we consider the point as part of a line: */
968 x2 = x[(i+1) % 4];
969 y2 = y[(i+1) % 4];
970
971 if (x1 == x2) {
972 append_vertex(clip(x1, cx1, cx2), clip(y1, cy1, cy2));
973 append_vertex(clip(x2, cx1, cx2), clip(y2, cy1, cy2));
974 } else if (y1 == y2) {
975 append_vertex(clip(x1, cx1, cx2), clip(y1, cy1, cy2));
976 append_vertex(clip(x2, cx1, cx2), clip(y2, cy1, cy2));
977 } else {
978 GLfloat m, c, p;
979 GLfloat tx[2], ty[2];
980 int tn = 0;
981
982 int intersect_horiz(GLfloat y, GLfloat *p)
983 {
984 GLfloat x;
985
986 /* if y does not lie between y1 and y2, no
987 * intersection possible
988 */
989 if (sign(y-y1) == sign(y-y2))
990 return 0;
991
992 x = (y - c) / m;
993
994 /* if x does not lie between cx1 and cx2, no
995 * intersection:
996 */
997 if (sign(x-cx1) == sign(x-cx2))
998 return 0;
999
1000 *p = x;
1001 return 1;
1002 }
1003
1004 int intersect_vert(GLfloat x, GLfloat *p)
1005 {
1006 GLfloat y;
1007
1008 if (sign(x-x1) == sign(x-x2))
1009 return 0;
1010
1011 y = m * x + c;
1012
1013 if (sign(y-cy1) == sign(y-cy2))
1014 return 0;
1015
1016 *p = y;
1017 return 1;
1018 }
1019
1020 /* y = mx + c */
1021 m = (y2 - y1) / (x2 - x1);
1022 c = y1 - m * x1;
1023
1024 /* check for up to two intersections with the four edges
1025 * of the clip rect. Note that we don't know the orientation
1026 * of the transformed surface wrt. the clip rect. So if when
1027 * there are two intersection points, we need to put the one
1028 * closest to x1,y1 first:
1029 */
1030
1031 /* check top clip rect edge: */
1032 if (intersect_horiz(cy1, &p)) {
1033 ty[tn] = cy1;
1034 tx[tn] = p;
1035 tn++;
1036 }
1037
1038 /* check right clip rect edge: */
1039 if (intersect_vert(cx2, &p)) {
1040 ty[tn] = p;
1041 tx[tn] = cx2;
1042 tn++;
1043 if (tn == 2)
1044 goto edge_check_done;
1045 }
1046
1047 /* check bottom clip rect edge: */
1048 if (intersect_horiz(cy2, &p)) {
1049 ty[tn] = cy2;
1050 tx[tn] = p;
1051 tn++;
1052 if (tn == 2)
1053 goto edge_check_done;
1054 }
1055
1056 /* check left clip rect edge: */
1057 if (intersect_vert(cx1, &p)) {
1058 ty[tn] = p;
1059 tx[tn] = cx1;
1060 tn++;
1061 }
1062
1063edge_check_done:
1064 if (tn == 1) {
1065 append_vertex(tx[0], ty[0]);
1066 } else if (tn == 2) {
1067 if (dist_squared(x1, y1, tx[0], ty[0]) <
1068 dist_squared(x1, y1, tx[1], ty[1])) {
1069 append_vertex(tx[0], ty[0]);
1070 append_vertex(tx[1], ty[1]);
1071 } else {
1072 append_vertex(tx[1], ty[1]);
1073 append_vertex(tx[0], ty[0]);
1074 }
1075 }
1076
1077 if (n == last_n) {
1078 GLfloat best_x=0, best_y=0;
1079 uint32_t d, best_d = (unsigned int)-1; /* distance squared */
1080 uint32_t max_d = dist_squared(x2, y2,
1081 x[(i+2) % 4], y[(i+2) % 4]);
1082
1083 /* if there are no vertices on this line, it could be that
1084 * there is a vertex of the clip rect that is enclosed by
1085 * the transformed surface. Find the vertex of the clip
1086 * rect that is reached by the shortest line perpendicular
1087 * to the current edge, if any.
1088 *
1089 * slope of perpendicular is 1/m, so
1090 *
1091 * cy = -cx/m + c2
1092 * c2 = cy + cx/m
1093 *
1094 */
1095
1096 int perp_intersect(GLfloat cx, GLfloat cy, uint32_t *d)
1097 {
1098 GLfloat c2 = cy + cx/m;
1099 GLfloat x = (c2 - c) / (m + 1/m);
1100
1101 /* if the x position of the intersection of the
1102 * perpendicular with the transformed edge does
1103 * not lie within the bounds of the edge, then
1104 * no intersection:
1105 */
1106 if (sign(x-x1) == sign(x-x2))
1107 return 0;
1108
1109 *d = dist_squared(cx, cy, x, (m * x) + c);
1110
1111 /* if intersection distance is further away than
1112 * opposite edge of surface region, it is invalid:
1113 */
1114 if (*d > max_d)
1115 return 0;
1116
1117 return 1;
1118 }
1119
1120 if (perp_intersect(cx1, cy1, &d)) {
1121 best_x = cx1;
1122 best_y = cy1;
1123 best_d = d;
1124 }
1125
1126 if (perp_intersect(cx1, cy2, &d) && (d < best_d)) {
1127 best_x = cx1;
1128 best_y = cy2;
1129 best_d = d;
1130 }
1131
1132 if (perp_intersect(cx2, cy2, &d) && (d < best_d)) {
1133 best_x = cx2;
1134 best_y = cy2;
1135 best_d = d;
1136 }
1137
1138 if (perp_intersect(cx2, cy1, &d) && (d < best_d)) {
1139 best_x = cx2;
1140 best_y = cy1;
1141 best_d = d;
1142 }
1143
1144 if (best_d != (unsigned int)-1) // XXX can this happen?
1145 append_vertex(best_x, best_y);
1146 }
1147 }
1148
Kristian Høgsberg525e4c02011-02-14 10:39:54 -05001149 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001150
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001151 return n;
1152}
1153
Rob Clark0e5a2d02012-08-30 16:47:18 -05001154static int
1155texture_region(struct weston_surface *es, pixman_region32_t *region,
1156 pixman_region32_t *surf_region)
1157{
1158 struct weston_compositor *ec = es->compositor;
1159 GLfloat *v, inv_width, inv_height;
1160 unsigned int *vtxcnt, nvtx = 0;
1161 pixman_box32_t *rects, *surf_rects;
1162 int i, j, k, nrects, nsurf;
1163
1164 rects = pixman_region32_rectangles(region, &nrects);
1165 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
1166
1167 /* worst case we can have 10 vertices per rect (ie. clipped into
1168 * an octagon):
1169 */
1170 v = wl_array_add(&ec->vertices, nrects * nsurf * 10 * 4 * sizeof *v);
1171 vtxcnt = wl_array_add(&ec->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
1172
1173 inv_width = 1.0 / es->pitch;
1174 inv_height = 1.0 / es->geometry.height;
1175
1176 for (i = 0; i < nrects; i++) {
1177 pixman_box32_t *rect = &rects[i];
1178 for (j = 0; j < nsurf; j++) {
1179 pixman_box32_t *surf_rect = &surf_rects[j];
1180 GLfloat cx, cy;
1181 GLfloat ex[8], ey[8]; /* edge points in screen space */
1182 int n;
1183
1184 void emit_vertex(GLfloat gx, GLfloat gy)
1185 {
1186 GLfloat sx, sy;
1187 surface_from_global_float(es, gx, gy, &sx, &sy);
1188 /* In groups of 4 attributes, first two are 'position', 2nd two
1189 * are 'texcoord'.
1190 */
1191 *(v++) = gx;
1192 *(v++) = gy;
1193 *(v++) = sx * inv_width;
1194 *(v++) = sy * inv_height;
1195 }
1196
1197 /* The transformed surface, after clipping to the clip region,
1198 * can have as many as eight sides, emitted as a triangle-fan.
1199 * The first vertex is the center, followed by each corner.
1200 *
1201 * If a corner of the transformed surface falls outside of the
1202 * clip region, instead of emitting one vertex for the corner
1203 * of the surface, up to two are emitted for two corresponding
1204 * intersection point(s) between the surface and the clip region.
1205 *
1206 * To do this, we first calculate the (up to eight) points that
1207 * form the intersection of the clip rect and the transformed
1208 * surface. After that we calculate the average to determine
1209 * the center point, and emit the center and edge vertices of
1210 * the fan.
1211 */
1212 n = calculate_edges(es, rect, surf_rect, ex, ey);
1213 if (n < 3)
1214 continue;
1215
1216 /* calculate/emit center point: */
1217 cx = 0;
1218 cy = 0;
1219 for (k = 0; k < n; k++) {
1220 cx += ex[k];
1221 cy += ey[k];
1222 }
1223 cx /= n;
1224 cy /= n;
1225 emit_vertex(cx, cy);
1226
1227 /* then emit edge points: */
1228 for (k = 0; k < n; k++)
1229 emit_vertex(ex[k], ey[k]);
1230
1231 /* and close the fan: */
1232 emit_vertex(ex[0], ey[0]);
1233
1234 vtxcnt[nvtx++] = n + 2;
1235 }
1236 }
1237
1238 return nvtx;
1239}
1240
1241static void
Pekka Paalanen07c91f82012-08-30 16:47:21 -05001242triangle_fan_debug(struct weston_surface *surface, int first, int count)
1243{
1244 struct weston_compositor *compositor = surface->compositor;
1245 int i;
1246 GLushort *buffer;
1247 GLushort *index;
1248 int nelems;
1249 static int color_idx = 0;
1250 static const GLfloat color[][4] = {
1251 { 1.0, 0.0, 0.0, 1.0 },
1252 { 0.0, 1.0, 0.0, 1.0 },
1253 { 0.0, 0.0, 1.0, 1.0 },
1254 { 1.0, 1.0, 1.0, 1.0 },
1255 };
1256
1257 nelems = (count - 1 + count - 2) * 2;
1258
1259 buffer = malloc(sizeof(GLushort) * nelems);
1260 index = buffer;
1261
1262 for (i = 1; i < count; i++) {
1263 *index++ = first;
1264 *index++ = first + i;
1265 }
1266
1267 for (i = 2; i < count; i++) {
1268 *index++ = first + i - 1;
1269 *index++ = first + i;
1270 }
1271
1272 glUseProgram(compositor->solid_shader.program);
1273 glUniform4fv(compositor->solid_shader.color_uniform, 1,
1274 color[color_idx++ % ARRAY_SIZE(color)]);
1275 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
1276 glUseProgram(surface->shader->program);
1277 free(buffer);
1278}
1279
1280static void
Rob Clark0e5a2d02012-08-30 16:47:18 -05001281repaint_region(struct weston_surface *es, pixman_region32_t *region,
1282 pixman_region32_t *surf_region)
1283{
1284 struct weston_compositor *ec = es->compositor;
1285 GLfloat *v;
1286 unsigned int *vtxcnt;
1287 int i, first, nfans;
1288
1289 /* The final region to be painted is the intersection of
1290 * 'region' and 'surf_region'. However, 'region' is in the global
1291 * coordinates, and 'surf_region' is in the surface-local
1292 * corodinates. texture_region() will iterate over all pairs of
1293 * rectangles from both regions, compute the intersection
1294 * polygon for each pair, and store it as a triangle fan if
1295 * it has a non-zero area.
1296 */
1297 nfans = texture_region(es, region, surf_region);
1298
1299 v = ec->vertices.data;
1300 vtxcnt = ec->vtxcnt.data;
1301
1302 /* position: */
1303 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
1304 glEnableVertexAttribArray(0);
1305
1306 /* texcoord: */
1307 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
1308 glEnableVertexAttribArray(1);
1309
1310 for (i = 0, first = 0; i < nfans; i++) {
1311 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
Pekka Paalanen07c91f82012-08-30 16:47:21 -05001312 if (ec->fan_debug)
1313 triangle_fan_debug(es, first, vtxcnt[i]);
Rob Clark0e5a2d02012-08-30 16:47:18 -05001314 first += vtxcnt[i];
1315 }
1316
1317 glDisableVertexAttribArray(1);
1318 glDisableVertexAttribArray(0);
1319
1320 ec->vertices.size = 0;
1321 ec->vtxcnt.size = 0;
1322}
1323
1324
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05001325WL_EXPORT void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001326weston_surface_draw(struct weston_surface *es, struct weston_output *output,
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001327 pixman_region32_t *damage) /* in global coordinates */
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001328{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001329 struct weston_compositor *ec = es->compositor;
Rob Clark0e5a2d02012-08-30 16:47:18 -05001330 /* repaint bounding region in global coordinates: */
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001331 pixman_region32_t repaint;
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001332 /* non-opaque region in surface coordinates: */
1333 pixman_region32_t surface_blend;
Kristian Høgsberg40caded2011-06-20 19:48:16 -04001334 GLint filter;
Rob Clark0e5a2d02012-08-30 16:47:18 -05001335 int i;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001336
Pekka Paalanena9f8a212012-01-26 11:28:08 +02001337 pixman_region32_init(&repaint);
Rob Clark0e5a2d02012-08-30 16:47:18 -05001338 pixman_region32_init(&surface_blend);
1339
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001340 pixman_region32_intersect(&repaint,
1341 &es->transform.boundingbox, damage);
1342 pixman_region32_subtract(&repaint, &repaint, &es->clip);
Pekka Paalanenc7b45f62012-02-06 12:16:07 +02001343
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001344 if (!pixman_region32_not_empty(&repaint))
Pekka Paalanena9f8a212012-01-26 11:28:08 +02001345 goto out;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001346
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001347 pixman_region32_subtract(&ec->primary_plane.damage,
1348 &ec->primary_plane.damage, &repaint);
1349
Kristian Høgsberg101cb652012-02-17 10:45:16 -05001350 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001351
1352 /* blended region is whole surface minus opaque region: */
1353 pixman_region32_init_rect(&surface_blend, 0, 0,
1354 es->geometry.width, es->geometry.height);
1355 if (es->alpha >= 1.0)
Rob Clark0e5a2d02012-08-30 16:47:18 -05001356 pixman_region32_subtract(&surface_blend, &surface_blend,
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001357 &es->opaque);
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001358
Kristian Høgsberg07632622012-01-25 23:02:06 -05001359 if (ec->current_shader != es->shader) {
1360 glUseProgram(es->shader->program);
Kristian Høgsberg07632622012-01-25 23:02:06 -05001361 ec->current_shader = es->shader;
1362 }
1363
Kristian Høgsberg0452abc2012-01-31 15:38:36 -05001364 glUniformMatrix4fv(es->shader->proj_uniform,
1365 1, GL_FALSE, output->matrix.d);
Pekka Paalanenf55f5442012-02-02 16:49:05 +02001366 glUniform4fv(es->shader->color_uniform, 1, es->color);
Kristian Høgsberga416fa12012-05-21 14:06:52 -04001367 glUniform1f(es->shader->alpha_uniform, es->alpha);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001368
Scott Moreauccbf29d2012-02-22 14:21:41 -07001369 if (es->transform.enabled || output->zoom.active)
Kristian Høgsberg40caded2011-06-20 19:48:16 -04001370 filter = GL_LINEAR;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001371 else
Pekka Paalanenc61eca62012-01-06 14:10:06 +02001372 filter = GL_NEAREST;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001373
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02001374 for (i = 0; i < es->num_textures; i++) {
1375 glUniform1i(es->shader->tex_uniforms[i], i);
1376 glActiveTexture(GL_TEXTURE0 + i);
Rob Clarke3b95912012-08-31 16:42:18 -05001377 glBindTexture(es->target, es->textures[i]);
1378 glTexParameteri(es->target, GL_TEXTURE_MIN_FILTER, filter);
1379 glTexParameteri(es->target, GL_TEXTURE_MAG_FILTER, filter);
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02001380 }
Kristian Høgsberg40caded2011-06-20 19:48:16 -04001381
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001382 if (pixman_region32_not_empty(&es->opaque) && es->alpha >= 1.0) {
Rob Clark0e5a2d02012-08-30 16:47:18 -05001383 glDisable(GL_BLEND);
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001384 repaint_region(es, &repaint, &es->opaque);
Rob Clark0e5a2d02012-08-30 16:47:18 -05001385 }
Pekka Paalanen3df327f2012-01-24 15:53:35 +02001386
Rob Clark0e5a2d02012-08-30 16:47:18 -05001387 if (pixman_region32_not_empty(&surface_blend)) {
1388 glEnable(GL_BLEND);
1389 repaint_region(es, &repaint, &surface_blend);
1390 }
Pekka Paalanena9f8a212012-01-26 11:28:08 +02001391
1392out:
Kristian Høgsberg525e4c02011-02-14 10:39:54 -05001393 pixman_region32_fini(&repaint);
Rob Clark0e5a2d02012-08-30 16:47:18 -05001394 pixman_region32_fini(&surface_blend);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001395}
1396
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001397WL_EXPORT void
1398weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001399{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001400 wl_list_remove(&surface->layer_link);
1401 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001402 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001403 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001404}
1405
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001406WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001407weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001408{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001409 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001410
1411 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001412 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001413}
1414
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001415WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001416weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +02001417{
Kristian Høgsberg24596942011-10-24 17:51:02 -04001418 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +02001419 return;
Benjamin Franzke06286262011-05-06 19:12:33 +02001420
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001421 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -05001422 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +02001423}
1424
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001425WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001426weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001427{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001428 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001429
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001430 pixman_region32_union(&compositor->primary_plane.damage,
1431 &compositor->primary_plane.damage,
1432 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001433 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001434}
1435
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001436static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001437fade_frame(struct weston_animation *animation,
1438 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001439{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001440 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001441 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001442 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001443 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001444
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001445 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -06001446 compositor->fade.spring.timestamp = msecs;
1447
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001448 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001449 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001450 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
1451 compositor->fade.spring.current);
1452 weston_surface_damage(surface);
1453
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001454 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001455 compositor->fade.spring.current =
1456 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001457 wl_list_remove(&animation->link);
1458 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001459
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001460 if (compositor->fade.spring.current < 0.001) {
1461 destroy_surface(&surface->surface.resource);
1462 compositor->fade.surface = NULL;
1463 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001464 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001465 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001466 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001467 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001468}
1469
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001470static void
1471update_shm_texture(struct weston_surface *surface)
1472{
1473#ifdef GL_UNPACK_ROW_LENGTH
1474 pixman_box32_t *rectangles;
1475 void *data;
1476 int i, n;
1477#endif
1478
Gwenole Beauchesne023f8552012-04-20 11:07:06 +02001479 glBindTexture(GL_TEXTURE_2D, surface->textures[0]);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001480
1481 if (!surface->compositor->has_unpack_subimage) {
1482 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1483 surface->pitch, surface->buffer->height, 0,
1484 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1485 wl_shm_buffer_get_data(surface->buffer));
1486
1487 return;
1488 }
1489
1490#ifdef GL_UNPACK_ROW_LENGTH
1491 /* Mesa does not define GL_EXT_unpack_subimage */
1492 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch);
1493 data = wl_shm_buffer_get_data(surface->buffer);
1494 rectangles = pixman_region32_rectangles(&surface->damage, &n);
1495 for (i = 0; i < n; i++) {
1496 glPixelStorei(GL_UNPACK_SKIP_PIXELS, rectangles[i].x1);
1497 glPixelStorei(GL_UNPACK_SKIP_ROWS, rectangles[i].y1);
1498 glTexSubImage2D(GL_TEXTURE_2D, 0,
1499 rectangles[i].x1, rectangles[i].y1,
Rob Bradford8f241562012-07-02 17:33:40 +01001500 rectangles[i].x2 - rectangles[i].x1,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001501 rectangles[i].y2 - rectangles[i].y1,
1502 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
1503 }
1504#endif
1505}
1506
1507static void
1508surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001509 pixman_region32_t *opaque)
1510{
1511 if (surface->buffer && wl_buffer_is_shm(surface->buffer))
1512 update_shm_texture(surface);
1513
1514 if (surface->transform.enabled) {
1515 pixman_box32_t *extents;
1516
1517 extents = pixman_region32_extents(&surface->damage);
1518 surface_compute_bbox(surface, extents->x1, extents->y1,
1519 extents->x2 - extents->x1,
1520 extents->y2 - extents->y1,
1521 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001522 pixman_region32_translate(&surface->damage,
1523 -surface->plane->x,
1524 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001525 } else {
1526 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001527 surface->geometry.x - surface->plane->x,
1528 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001529 }
1530
1531 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001532 pixman_region32_union(&surface->plane->damage,
1533 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001534 empty_region(&surface->damage);
1535 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001536 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001537}
1538
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001539static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001540weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001541{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001542 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001543 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001544 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001545 struct weston_animation *animation, *next;
1546 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001547 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001548 pixman_region32_t opaque, output_damage, new_damage;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001549 int32_t width, height;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001550
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001551 weston_compositor_update_drag_surfaces(ec);
1552
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001553 width = output->current->width +
1554 output->border.left + output->border.right;
1555 height = output->current->height +
1556 output->border.top + output->border.bottom;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001557
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001558 glViewport(0, 0, width, height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -05001559
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001560 /* Rebuild the surface list and update surface transforms up front. */
1561 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +02001562 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001563 wl_list_for_each(layer, &ec->layer_list, link) {
1564 wl_list_for_each(es, &layer->surface_list, layer_link) {
1565 weston_surface_update_transform(es);
1566 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +02001567 if (es->output == output) {
1568 wl_list_insert_list(&frame_callback_list,
1569 &es->frame_callback_list);
1570 wl_list_init(&es->frame_callback_list);
1571 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001572 }
1573 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001574
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001575 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001576 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001577 else
1578 wl_list_for_each(es, &ec->surface_list, link)
1579 weston_surface_move_to_plane(es, &ec->primary_plane);
1580
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001581 pixman_region32_init(&opaque);
1582
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001583 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001584 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001585
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -04001586 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001587
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001588 pixman_region32_init(&output_damage);
1589
1590 pixman_region32_init(&new_damage);
1591 pixman_region32_copy(&new_damage, &ec->primary_plane.damage);
1592
1593 pixman_region32_union(&ec->primary_plane.damage,
1594 &ec->primary_plane.damage,
1595 &output->previous_damage);
1596
1597 pixman_region32_intersect(&output->previous_damage,
1598 &new_damage, &output->region);
1599
1600 pixman_region32_intersect(&output_damage,
1601 &ec->primary_plane.damage, &output->region);
1602
1603 pixman_region32_fini(&new_damage);
1604
Scott Moreauccbf29d2012-02-22 14:21:41 -07001605 if (output->dirty)
1606 weston_output_update_matrix(output);
1607
Pekka Paalanen07c91f82012-08-30 16:47:21 -05001608 /* if debugging, redraw everything outside the damage to clean up
1609 * debug lines from the previous draw on this buffer:
1610 */
1611 if (ec->fan_debug) {
1612 pixman_region32_t undamaged;
1613 pixman_region32_init(&undamaged);
1614 pixman_region32_subtract(&undamaged, &output->region,
1615 &output_damage);
1616 ec->fan_debug = 0;
1617 output->repaint(output, &undamaged, 0);
1618 ec->fan_debug = 1;
1619 pixman_region32_fini(&undamaged);
1620 }
1621
1622 output->repaint(output, &output_damage, 1);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001623
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001624 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001625
Kristian Høgsbergef044142011-06-21 15:02:12 -04001626 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001627
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001628 weston_compositor_repick(ec);
1629 wl_event_loop_dispatch(ec->input_loop, 0);
1630
Jonas Ådahldb773762012-06-13 00:01:21 +02001631 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001632 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001633 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001634 }
Jonas Ådahldb773762012-06-13 00:01:21 +02001635 wl_list_init(&frame_callback_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001636
Scott Moreaud64cf212012-06-08 19:40:54 -06001637 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001638 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001639 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001640 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001641}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001642
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001643static int
1644weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001645{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001646 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001647
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001648 wl_event_loop_dispatch(compositor->input_loop, 0);
1649
1650 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001651}
1652
1653WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001654weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001655{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001656 struct weston_compositor *compositor = output->compositor;
1657 struct wl_event_loop *loop =
1658 wl_display_get_event_loop(compositor->wl_display);
1659 int fd;
1660
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001661 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001662 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001663 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001664 return;
1665 }
1666
1667 output->repaint_scheduled = 0;
1668 if (compositor->input_loop_source)
1669 return;
1670
1671 fd = wl_event_loop_get_fd(compositor->input_loop);
1672 compositor->input_loop_source =
1673 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1674 weston_compositor_read_input, compositor);
1675}
1676
1677static void
1678idle_repaint(void *data)
1679{
1680 struct weston_output *output = data;
1681
1682 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001683}
1684
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001685WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001686weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1687{
1688 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001689 if (below != NULL)
1690 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001691}
1692
1693WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001694weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001695{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001696 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001697 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001698
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001699 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001700 return;
1701
Kristian Høgsbergef044142011-06-21 15:02:12 -04001702 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001703 output->repaint_needed = 1;
1704 if (output->repaint_scheduled)
1705 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001706
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001707 wl_event_loop_add_idle(loop, idle_repaint, output);
1708 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001709
1710 if (compositor->input_loop_source) {
1711 wl_event_source_remove(compositor->input_loop_source);
1712 compositor->input_loop_source = NULL;
1713 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001714}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001715
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001716WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001717weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1718{
1719 struct weston_output *output;
1720
1721 wl_list_for_each(output, &compositor->output_list, link)
1722 weston_output_schedule_repaint(output);
1723}
1724
1725WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001726weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001727{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001728 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001729 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001730
Scott Moreau9d1b1122012-06-08 19:40:53 -06001731 output = container_of(compositor->output_list.next,
1732 struct weston_output, link);
1733
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001734 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001735 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001736 return;
1737
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001738 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001739 surface = weston_surface_create(compositor);
1740 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001741 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001742 wl_list_insert(&compositor->fade_layer.surface_list,
1743 &surface->layer_link);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001744 weston_surface_assign_output(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001745 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001746 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001747 }
1748
1749 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001750 if (wl_list_empty(&compositor->fade.animation.link)) {
1751 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001752 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001753 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001754 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001755}
1756
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001757static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001758surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001759{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001760 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001761}
1762
Casey Dahlin96d8a752012-04-19 22:50:07 -04001763static struct wl_resource *
1764find_resource_for_client(struct wl_list *list, struct wl_client *client)
1765{
1766 struct wl_resource *r;
1767
1768 wl_list_for_each(r, list, link) {
1769 if (r->client == client)
1770 return r;
1771 }
1772
1773 return NULL;
1774}
1775
Casey Dahlin9074db52012-04-19 22:50:09 -04001776static void
1777weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1778{
1779 uint32_t different = es->output_mask ^ mask;
1780 uint32_t entered = mask & different;
1781 uint32_t left = es->output_mask & different;
1782 struct weston_output *output;
Rob Bradford8667e772012-05-18 14:13:03 +01001783 struct wl_resource *resource = NULL;
Casey Dahlin9074db52012-04-19 22:50:09 -04001784 struct wl_client *client = es->surface.resource.client;
1785
Scott Moreaua5021522012-08-03 17:11:51 -06001786 es->output_mask = mask;
Casey Dahlin9074db52012-04-19 22:50:09 -04001787 if (es->surface.resource.client == NULL)
1788 return;
1789 if (different == 0)
1790 return;
1791
Casey Dahlin9074db52012-04-19 22:50:09 -04001792 wl_list_for_each(output, &es->compositor->output_list, link) {
1793 if (1 << output->id & different)
1794 resource =
1795 find_resource_for_client(&output->resource_list,
1796 client);
Kristian Høgsbergc7814d22012-07-12 12:34:43 -04001797 if (resource == NULL)
1798 continue;
Casey Dahlin9074db52012-04-19 22:50:09 -04001799 if (1 << output->id & entered)
1800 wl_surface_send_enter(&es->surface.resource, resource);
1801 if (1 << output->id & left)
1802 wl_surface_send_leave(&es->surface.resource, resource);
1803 }
1804}
1805
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001806WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001807weston_surface_assign_output(struct weston_surface *es)
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001808{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001809 struct weston_compositor *ec = es->compositor;
1810 struct weston_output *output, *new_output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001811 pixman_region32_t region;
Casey Dahlin9074db52012-04-19 22:50:09 -04001812 uint32_t max, area, mask;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001813 pixman_box32_t *e;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001814
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001815 new_output = NULL;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001816 max = 0;
Casey Dahlin9074db52012-04-19 22:50:09 -04001817 mask = 0;
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001818 pixman_region32_init(&region);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001819 wl_list_for_each(output, &ec->output_list, link) {
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001820 pixman_region32_intersect(&region, &es->transform.boundingbox,
1821 &output->region);
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001822
1823 e = pixman_region32_extents(&region);
1824 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1825
Casey Dahlin9074db52012-04-19 22:50:09 -04001826 if (area > 0)
1827 mask |= 1 << output->id;
1828
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001829 if (area >= max) {
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001830 new_output = output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001831 max = area;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001832 }
1833 }
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001834 pixman_region32_fini(&region);
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001835
1836 es->output = new_output;
Casey Dahlin9074db52012-04-19 22:50:09 -04001837 weston_surface_update_output_mask(es, mask);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001838}
1839
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001840static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001841surface_attach(struct wl_client *client,
1842 struct wl_resource *resource,
1843 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1844{
1845 struct weston_surface *es = resource->data;
1846 struct wl_buffer *buffer = NULL;
1847
1848 if (buffer_resource)
1849 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001850
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -04001851 weston_surface_attach(&es->surface, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001852
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001853 if (buffer && es->configure)
1854 es->configure(es, sx, sy);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001855}
1856
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001857static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001858surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001859 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001860 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001861{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001862 struct weston_surface *es = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001863
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001864 pixman_region32_union_rect(&es->damage, &es->damage,
1865 x, y, width, height);
Kristian Høgsberg98238702012-08-03 16:29:12 -04001866 weston_surface_schedule_repaint(es);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001867}
1868
Kristian Høgsberg33418202011-08-16 23:01:28 -04001869static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001870destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001871{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001872 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001873
1874 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001875 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001876}
1877
1878static void
1879surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001880 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001881{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001882 struct weston_frame_callback *cb;
1883 struct weston_surface *es = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001884
1885 cb = malloc(sizeof *cb);
1886 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001887 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001888 return;
1889 }
1890
1891 cb->resource.object.interface = &wl_callback_interface;
1892 cb->resource.object.id = callback;
1893 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001894 cb->resource.client = client;
1895 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001896
1897 wl_client_add_resource(client, &cb->resource);
Jonas Ådahldb773762012-06-13 00:01:21 +02001898 wl_list_insert(es->frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001899}
1900
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001901static void
1902surface_set_opaque_region(struct wl_client *client,
1903 struct wl_resource *resource,
1904 struct wl_resource *region_resource)
1905{
1906 struct weston_surface *surface = resource->data;
1907 struct weston_region *region;
1908
1909 pixman_region32_fini(&surface->opaque);
1910
1911 if (region_resource) {
1912 region = region_resource->data;
1913 pixman_region32_init_rect(&surface->opaque, 0, 0,
1914 surface->geometry.width,
1915 surface->geometry.height);
1916 pixman_region32_intersect(&surface->opaque,
1917 &surface->opaque, &region->region);
1918 } else {
1919 pixman_region32_init(&surface->opaque);
1920 }
1921
1922 surface->geometry.dirty = 1;
1923}
1924
1925static void
1926surface_set_input_region(struct wl_client *client,
1927 struct wl_resource *resource,
1928 struct wl_resource *region_resource)
1929{
1930 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001931 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001932
1933 if (region_resource) {
1934 region = region_resource->data;
1935 pixman_region32_init_rect(&surface->input, 0, 0,
1936 surface->geometry.width,
1937 surface->geometry.height);
1938 pixman_region32_intersect(&surface->input,
1939 &surface->input, &region->region);
1940 } else {
1941 pixman_region32_init_rect(&surface->input, 0, 0,
1942 surface->geometry.width,
1943 surface->geometry.height);
1944 }
1945
Kristian Høgsberg98238702012-08-03 16:29:12 -04001946 weston_surface_schedule_repaint(surface);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001947}
1948
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001949static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001950 surface_destroy,
1951 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001952 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001953 surface_frame,
1954 surface_set_opaque_region,
1955 surface_set_input_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001956};
1957
1958static void
1959compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001960 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001961{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001962 struct weston_compositor *ec = resource->data;
1963 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001964
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001965 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001966 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001967 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001968 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001969 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001970
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001971 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001972
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001973 surface->surface.resource.object.id = id;
1974 surface->surface.resource.object.interface = &wl_surface_interface;
1975 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001976 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001977 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001978
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001979 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001980}
1981
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001982static void
1983destroy_region(struct wl_resource *resource)
1984{
1985 struct weston_region *region =
1986 container_of(resource, struct weston_region, resource);
1987
1988 pixman_region32_fini(&region->region);
1989 free(region);
1990}
1991
1992static void
1993region_destroy(struct wl_client *client, struct wl_resource *resource)
1994{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001995 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001996}
1997
1998static void
1999region_add(struct wl_client *client, struct wl_resource *resource,
2000 int32_t x, int32_t y, int32_t width, int32_t height)
2001{
2002 struct weston_region *region = resource->data;
2003
2004 pixman_region32_union_rect(&region->region, &region->region,
2005 x, y, width, height);
2006}
2007
2008static void
2009region_subtract(struct wl_client *client, struct wl_resource *resource,
2010 int32_t x, int32_t y, int32_t width, int32_t height)
2011{
2012 struct weston_region *region = resource->data;
2013 pixman_region32_t rect;
2014
2015 pixman_region32_init_rect(&rect, x, y, width, height);
2016 pixman_region32_subtract(&region->region, &region->region, &rect);
2017 pixman_region32_fini(&rect);
2018}
2019
2020static const struct wl_region_interface region_interface = {
2021 region_destroy,
2022 region_add,
2023 region_subtract
2024};
2025
2026static void
2027compositor_create_region(struct wl_client *client,
2028 struct wl_resource *resource, uint32_t id)
2029{
2030 struct weston_region *region;
2031
2032 region = malloc(sizeof *region);
2033 if (region == NULL) {
2034 wl_resource_post_no_memory(resource);
2035 return;
2036 }
2037
2038 region->resource.destroy = destroy_region;
2039
2040 region->resource.object.id = id;
2041 region->resource.object.interface = &wl_region_interface;
2042 region->resource.object.implementation =
2043 (void (**)(void)) &region_interface;
2044 region->resource.data = region;
2045
2046 pixman_region32_init(&region->region);
2047
2048 wl_client_add_resource(client, &region->resource);
2049}
2050
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002051static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002052 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002053 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002054};
2055
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002056WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002057weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002058{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002059 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2060 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002061
2062 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02002063 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002064}
2065
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002066static void
2067weston_compositor_dpms_on(struct weston_compositor *compositor)
2068{
2069 struct weston_output *output;
2070
2071 wl_list_for_each(output, &compositor->output_list, link)
2072 if (output->set_dpms)
2073 output->set_dpms(output, WESTON_DPMS_ON);
2074}
2075
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002076WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002077weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002078{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002079 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
2080 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002081 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002082 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002083 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002084 }
2085}
2086
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002087static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002088weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002089{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002090 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002091 compositor->idle_inhibit++;
2092}
2093
2094static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002095weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002096{
2097 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002098 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002099}
2100
2101static int
2102idle_handler(void *data)
2103{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002104 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002105
2106 if (compositor->idle_inhibit)
2107 return 1;
2108
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002109 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002110
2111 return 1;
2112}
2113
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002114WL_EXPORT void
2115weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2116{
2117 pixman_region32_init(&plane->damage);
2118 plane->x = x;
2119 plane->y = y;
2120}
2121
2122WL_EXPORT void
2123weston_plane_release(struct weston_plane *plane)
2124{
2125 pixman_region32_fini(&plane->damage);
2126}
2127
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002128static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002129weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002130
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002131static void
Daniel Stone37816df2012-05-16 18:45:18 +01002132clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002133{
Daniel Stone37816df2012-05-16 18:45:18 +01002134 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002135 struct weston_output *output, *prev = NULL;
2136 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002137
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002138 x = wl_fixed_to_int(*fx);
2139 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01002140 old_x = wl_fixed_to_int(seat->seat.pointer->x);
2141 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002142
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002143 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002144 if (pixman_region32_contains_point(&output->region,
2145 x, y, NULL))
2146 valid = 1;
2147 if (pixman_region32_contains_point(&output->region,
2148 old_x, old_y, NULL))
2149 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002150 }
Daniel Stone37816df2012-05-16 18:45:18 +01002151
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002152 if (!valid) {
2153 if (x < prev->x)
2154 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06002155 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002156 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06002157 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002158 if (y < prev->y)
2159 *fy = wl_fixed_from_int(prev->y);
2160 else if (y >= prev->y + prev->current->height)
2161 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06002162 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002163 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002164}
2165
2166WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002167notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002168{
2169 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002170 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002171 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002172 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002173 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002174
2175 weston_compositor_activity(ec);
2176
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002177 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002178
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002179 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002180
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002181 pointer->x = x;
2182 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002183
2184 ix = wl_fixed_to_int(x);
2185 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002186
Scott Moreauccbf29d2012-02-22 14:21:41 -07002187 wl_list_for_each(output, &ec->output_list, link)
2188 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002189 pixman_region32_contains_point(&output->region,
2190 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06002191 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002192
Daniel Stone37816df2012-05-16 18:45:18 +01002193 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002194 interface = pointer->grab->interface;
2195 interface->motion(pointer->grab, time,
2196 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002197
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002198 if (seat->sprite) {
2199 weston_surface_set_position(seat->sprite,
2200 ix - seat->hotspot_x,
2201 iy - seat->hotspot_y);
2202 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002203 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002204}
2205
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002206WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002207weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002208 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05002209{
Daniel Stone37816df2012-05-16 18:45:18 +01002210 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002211
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03002212 if (seat->seat.keyboard) {
2213 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
2214 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04002215 }
2216
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002217 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05002218}
2219
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002220WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002221notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002222 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05002223{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002224 struct weston_compositor *compositor = seat->compositor;
2225 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01002226 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002227 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002228 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05002229
Daniel Stone4dbadb12012-05-30 16:31:51 +01002230 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002231 if (compositor->ping_handler && focus)
2232 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002233 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002234 if (pointer->button_count == 0) {
2235 pointer->grab_button = button;
2236 pointer->grab_time = time;
2237 pointer->grab_x = pointer->x;
2238 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002239 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002240 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002241 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002242 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002243 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002244 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002245
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002246 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002247 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05002248
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002249 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05002250
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002251 if (pointer->button_count == 1)
2252 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002253 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05002254}
2255
Scott Moreau210d0792012-03-22 10:47:01 -06002256WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002257notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01002258 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002259{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002260 struct weston_compositor *compositor = seat->compositor;
2261 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01002262 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002263 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002264 uint32_t serial = wl_display_next_serial(compositor->wl_display);
2265
2266 if (compositor->ping_handler && focus)
2267 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06002268
2269 weston_compositor_activity(compositor);
2270
Scott Moreau6a3633d2012-03-20 08:47:59 -06002271 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002272 weston_compositor_run_axis_binding(compositor, seat,
2273 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06002274 else
2275 return;
2276
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002277 if (pointer->focus_resource)
2278 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01002279 value);
Scott Moreau210d0792012-03-22 10:47:01 -06002280}
2281
Daniel Stone05d58682012-06-22 13:21:38 +01002282WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002283notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002284{
Daniel Stone05d58682012-06-22 13:21:38 +01002285 struct wl_keyboard *keyboard = &seat->keyboard;
2286 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01002287 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002288 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01002289 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01002290 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01002291
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002292 /* Serialize and update our internal state, checking to see if it's
2293 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01002294 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
2295 XKB_STATE_DEPRESSED);
2296 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
2297 XKB_STATE_LATCHED);
2298 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
2299 XKB_STATE_LOCKED);
2300 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01002301 XKB_STATE_EFFECTIVE);
2302
Daniel Stone71c38772012-06-22 13:21:30 +01002303 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
2304 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
2305 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
2306 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01002307 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01002308
Daniel Stone71c38772012-06-22 13:21:30 +01002309 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
2310 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
2311 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
2312 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05002313
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002314 /* And update the modifier_state for bindings. */
2315 mods_lookup = mods_depressed | mods_latched;
2316 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01002317 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002318 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01002319 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002320 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01002321 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002322 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04002323 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
2324 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01002325
Daniel Stone8c8164f2012-05-30 16:31:45 +01002326 /* Finally, notify the compositor that LEDs have changed. */
2327 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01002328 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01002329 leds |= LED_NUM_LOCK;
2330 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01002331 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01002332 leds |= LED_CAPS_LOCK;
2333 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01002334 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01002335 leds |= LED_SCROLL_LOCK;
2336 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01002337 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01002338 seat->xkb_state.leds = leds;
2339
Daniel Stone05d58682012-06-22 13:21:38 +01002340 if (changed) {
2341 grab->interface->modifiers(grab,
2342 serial,
2343 keyboard->modifiers.mods_depressed,
2344 keyboard->modifiers.mods_latched,
2345 keyboard->modifiers.mods_locked,
2346 keyboard->modifiers.group);
2347 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002348}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04002349
Daniel Stone05d58682012-06-22 13:21:38 +01002350static void
2351update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002352 enum wl_keyboard_key_state state)
2353{
2354 enum xkb_key_direction direction;
2355
2356 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2357 direction = XKB_KEY_DOWN;
2358 else
2359 direction = XKB_KEY_UP;
2360
2361 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
2362 * broken keycode system, which starts at 8. */
2363 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
2364
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002365 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002366}
2367
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002368WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002369notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01002370 enum wl_keyboard_key_state state,
2371 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002372{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002373 struct weston_compositor *compositor = seat->compositor;
2374 struct wl_keyboard *keyboard = seat->seat.keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01002375 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002376 (struct weston_surface *) keyboard->focus;
2377 struct wl_keyboard_grab *grab = keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002378 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002379 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002380
Daniel Stonec9785ea2012-05-30 16:31:52 +01002381 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002382 if (compositor->ping_handler && focus)
2383 compositor->ping_handler(focus, serial);
2384
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002385 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002386 keyboard->grab_key = key;
2387 keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07002388 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002389 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07002390 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002391
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002392 end = keyboard->keys.data + keyboard->keys.size;
2393 for (k = keyboard->keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01002394 if (*k == key) {
2395 /* Ignore server-generated repeats. */
2396 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2397 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002398 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01002399 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002400 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002401 keyboard->keys.size = (void *) end - keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002402 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002403 k = wl_array_add(&keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002404 *k = key;
2405 }
Ray Strodee96dcb82008-12-20 02:00:49 -05002406
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002407 if (grab == &keyboard->default_grab) {
2408 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002409 state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002410 grab = keyboard->grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06002411 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05002412
Daniel Stone7ace3902012-05-30 16:31:40 +01002413 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01002414
2415 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002416 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01002417 wl_display_get_serial(compositor->wl_display),
2418 key,
2419 state);
2420 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002421}
2422
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002423WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002424notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01002425 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002426{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002427 struct weston_compositor *compositor = seat->compositor;
2428 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002429
2430 if (output) {
Daniel Stone37816df2012-05-16 18:45:18 +01002431 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002432 x - pointer->x,
2433 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002434
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002435 pointer->x = x;
2436 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002437 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002438 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002439 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002440 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03002441 /* FIXME: We should call wl_pointer_set_focus(seat,
2442 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002443 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002444}
2445
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002446static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002447destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002448{
Daniel Stone37816df2012-05-16 18:45:18 +01002449 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002450
Daniel Stone37816df2012-05-16 18:45:18 +01002451 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002452 saved_kbd_focus_listener);
2453
Daniel Stone37816df2012-05-16 18:45:18 +01002454 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002455}
2456
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002457WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002458notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002459 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002460{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002461 struct weston_compositor *compositor = seat->compositor;
2462 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04002463 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002464 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05002465
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002466 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002467 wl_array_copy(&keyboard->keys, keys);
2468 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002469 weston_compositor_idle_inhibit(compositor);
2470 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002471 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002472 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002473 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002474
Daniel Stoneef172672012-06-22 13:21:32 +01002475 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002476 wl_array_for_each(k, &keyboard->keys) {
2477 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01002478 WL_KEYBOARD_KEY_STATE_PRESSED);
2479 }
2480
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002481 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002482
2483 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002484 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2485 wl_keyboard_set_focus(keyboard, surface);
2486 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002487 }
2488}
2489
2490WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002491notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01002492{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002493 struct weston_compositor *compositor = seat->compositor;
2494 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002495 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002496
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002497 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002498 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002499 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002500 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002501 WL_KEYBOARD_KEY_STATE_RELEASED);
2502 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002503
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002504 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002505
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002506 if (keyboard->focus) {
2507 seat->saved_kbd_focus = keyboard->focus;
2508 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01002509 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002510 wl_signal_add(&keyboard->focus->resource.destroy_signal,
2511 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002512 }
2513
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002514 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002515 /* FIXME: We really need keyboard grab cancel here to
2516 * let the grab shut down properly. As it is we leak
2517 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002518 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002519}
2520
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002521static void
Daniel Stone37816df2012-05-16 18:45:18 +01002522touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002523{
Daniel Stone37816df2012-05-16 18:45:18 +01002524 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002525 struct wl_resource *resource;
2526
Pekka Paalanen56464252012-07-31 13:21:08 +03002527 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002528 return;
2529
Pekka Paalanen56464252012-07-31 13:21:08 +03002530 if (seat->touch->focus_resource)
2531 wl_list_remove(&seat->touch->focus_listener.link);
2532 seat->touch->focus = NULL;
2533 seat->touch->focus_resource = NULL;
2534
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002535 if (surface) {
2536 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01002537 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04002538 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002539 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02002540 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002541 return;
2542 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002543
Daniel Stone37816df2012-05-16 18:45:18 +01002544 seat->touch->focus = surface;
2545 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03002546 wl_signal_add(&resource->destroy_signal,
2547 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002548 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002549}
2550
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002551/**
2552 * notify_touch - emulates button touches and notifies surfaces accordingly.
2553 *
2554 * It assumes always the correct cycle sequence until it gets here: touch_down
2555 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2556 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002557 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002558 */
2559WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002560notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002561 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002562{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002563 struct weston_compositor *ec = seat->compositor;
2564 struct wl_touch *touch = seat->seat.touch;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002565 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002566 wl_fixed_t sx, sy;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002567 uint32_t serial = 0;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002568
2569 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01002570 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002571 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002572
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002573 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002574
2575 /* the first finger down picks the surface, and all further go
2576 * to that surface for the remainder of the touch session i.e.
2577 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002578 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002579 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002580 touch_set_focus(seat, &es->surface);
2581 } else if (touch->focus) {
2582 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002583 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002584 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002585
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002586 if (touch->focus_resource && touch->focus)
2587 wl_touch_send_down(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002588 serial, time,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002589 &touch->focus->resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002590 touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002591 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002592 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002593 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002594 if (!es)
2595 break;
2596
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002597 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002598 if (touch->focus_resource)
2599 wl_touch_send_motion(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002600 time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002601 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002602 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002603 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002604 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002605
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002606 if (touch->focus_resource)
2607 wl_touch_send_up(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002608 serial, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002609 if (seat->num_tp == 0)
2610 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002611 break;
2612 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002613}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002614
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04002615static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002616pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
2617{
2618 struct weston_seat *seat = container_of(listener, struct weston_seat,
2619 sprite_destroy_listener);
2620
2621 seat->sprite = NULL;
2622}
2623
2624static void
2625pointer_cursor_surface_configure(struct weston_surface *es,
2626 int32_t dx, int32_t dy)
2627{
2628 struct weston_seat *seat = es->private;
2629 int x, y;
2630
2631 assert(es == seat->sprite);
2632
2633 seat->hotspot_x -= dx;
2634 seat->hotspot_y -= dy;
2635
2636 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
2637 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
2638
2639 weston_surface_configure(seat->sprite, x, y,
2640 es->buffer->width, es->buffer->height);
2641
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03002642 empty_region(&es->input);
2643
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002644 if (!weston_surface_is_mapped(es)) {
2645 wl_list_insert(&es->compositor->cursor_layer.surface_list,
2646 &es->layer_link);
2647 weston_surface_assign_output(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002648 }
2649}
2650
2651static void
2652pointer_unmap_sprite(struct weston_seat *seat)
2653{
2654 if (weston_surface_is_mapped(seat->sprite))
2655 weston_surface_unmap(seat->sprite);
2656
2657 wl_list_remove(&seat->sprite_destroy_listener.link);
2658 seat->sprite->configure = NULL;
2659 seat->sprite->private = NULL;
2660 seat->sprite = NULL;
2661}
2662
2663static void
2664pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2665 uint32_t serial, struct wl_resource *surface_resource,
2666 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002667{
Daniel Stone37816df2012-05-16 18:45:18 +01002668 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002669 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002670
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002671 if (surface_resource)
2672 surface = container_of(surface_resource->data,
2673 struct weston_surface, surface);
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002674
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04002675 if (seat->seat.pointer->focus == NULL)
2676 return;
2677 if (seat->seat.pointer->focus->resource.client != client)
2678 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002679 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002680 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002681
2682 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002683 if (surface->configure) {
2684 wl_resource_post_error(&surface->surface.resource,
2685 WL_DISPLAY_ERROR_INVALID_OBJECT,
2686 "surface->configure already "
2687 "set");
2688 return;
2689 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002690 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002691
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002692 if (seat->sprite)
2693 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002694
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002695 if (!surface)
2696 return;
2697
2698 wl_signal_add(&surface->surface.resource.destroy_signal,
2699 &seat->sprite_destroy_listener);
2700
2701 surface->configure = pointer_cursor_surface_configure;
2702 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002703 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002704 seat->hotspot_x = x;
2705 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002706
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04002707 if (surface->buffer)
2708 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002709}
2710
Daniel Stone37816df2012-05-16 18:45:18 +01002711static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002712 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002713};
2714
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002715static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002716handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002717{
Daniel Stone37816df2012-05-16 18:45:18 +01002718 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002719
Daniel Stone37816df2012-05-16 18:45:18 +01002720 seat = container_of(listener, struct weston_seat,
2721 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002722
Daniel Stone37816df2012-05-16 18:45:18 +01002723 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002724}
2725
Casey Dahlin9074db52012-04-19 22:50:09 -04002726static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002727{
2728 wl_list_remove(&resource->link);
2729 free(resource);
2730}
2731
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002732static void
Daniel Stone37816df2012-05-16 18:45:18 +01002733seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2734 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002735{
Daniel Stone37816df2012-05-16 18:45:18 +01002736 struct weston_seat *seat = resource->data;
2737 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002738
Daniel Stone37816df2012-05-16 18:45:18 +01002739 if (!seat->seat.pointer)
2740 return;
2741
2742 cr = wl_client_add_object(client, &wl_pointer_interface,
2743 &pointer_interface, id, seat);
2744 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002745 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002746
2747 if (seat->seat.pointer->focus &&
2748 seat->seat.pointer->focus->resource.client == client) {
2749 struct weston_surface *surface;
2750 wl_fixed_t sx, sy;
2751
2752 surface = (struct weston_surface *) seat->seat.pointer->focus;
2753 weston_surface_from_global_fixed(surface,
2754 seat->seat.pointer->x,
2755 seat->seat.pointer->y,
2756 &sx,
2757 &sy);
2758 wl_pointer_set_focus(seat->seat.pointer,
2759 seat->seat.pointer->focus,
2760 sx,
2761 sy);
2762 }
Daniel Stone37816df2012-05-16 18:45:18 +01002763}
2764
2765static void
2766seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2767 uint32_t id)
2768{
2769 struct weston_seat *seat = resource->data;
2770 struct wl_resource *cr;
2771
2772 if (!seat->seat.keyboard)
2773 return;
2774
2775 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2776 seat);
2777 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002778 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002779
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002780 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2781 seat->xkb_info.keymap_fd,
2782 seat->xkb_info.keymap_size);
2783
Daniel Stone17b13bd2012-05-30 16:31:39 +01002784 if (seat->seat.keyboard->focus &&
2785 seat->seat.keyboard->focus->resource.client == client) {
2786 wl_keyboard_set_focus(seat->seat.keyboard,
2787 seat->seat.keyboard->focus);
2788 }
Daniel Stone37816df2012-05-16 18:45:18 +01002789}
2790
2791static void
2792seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2793 uint32_t id)
2794{
2795 struct weston_seat *seat = resource->data;
2796 struct wl_resource *cr;
2797
2798 if (!seat->seat.touch)
2799 return;
2800
2801 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2802 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002803 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002804}
2805
2806static const struct wl_seat_interface seat_interface = {
2807 seat_get_pointer,
2808 seat_get_keyboard,
2809 seat_get_touch,
2810};
2811
2812static void
2813bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2814{
2815 struct wl_seat *seat = data;
2816 struct wl_resource *resource;
2817 enum wl_seat_capability caps = 0;
2818
2819 resource = wl_client_add_object(client, &wl_seat_interface,
2820 &seat_interface, id, data);
2821 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002822 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002823
2824 if (seat->pointer)
2825 caps |= WL_SEAT_CAPABILITY_POINTER;
2826 if (seat->keyboard)
2827 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2828 if (seat->touch)
2829 caps |= WL_SEAT_CAPABILITY_TOUCH;
2830
2831 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002832}
2833
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002834static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002835device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002836{
Daniel Stone37816df2012-05-16 18:45:18 +01002837 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002838
Daniel Stone37816df2012-05-16 18:45:18 +01002839 seat = container_of(listener, struct weston_seat,
2840 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002841
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002842 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002843}
2844
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002845static void weston_compositor_xkb_init(struct weston_compositor *ec,
2846 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002847{
Daniel Stonee379da92012-05-30 16:32:04 +01002848 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002849 ec->xkb_context = xkb_context_new(0);
2850 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002851 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002852 exit(1);
2853 }
Daniel Stone994679a2012-05-30 16:31:43 +01002854 }
2855
2856 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002857 ec->xkb_names = *names;
2858 if (!ec->xkb_names.rules)
2859 ec->xkb_names.rules = strdup("evdev");
2860 if (!ec->xkb_names.model)
2861 ec->xkb_names.model = strdup("pc105");
2862 if (!ec->xkb_names.layout)
2863 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002864}
2865
Daniel Stonee379da92012-05-30 16:32:04 +01002866static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2867{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002868 if (xkb_info->keymap)
2869 xkb_map_unref(xkb_info->keymap);
2870
2871 if (xkb_info->keymap_area)
2872 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2873 if (xkb_info->keymap_fd >= 0)
2874 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002875}
2876
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002877static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2878{
Daniel Stonee379da92012-05-30 16:32:04 +01002879 free((char *) ec->xkb_names.rules);
2880 free((char *) ec->xkb_names.model);
2881 free((char *) ec->xkb_names.layout);
2882 free((char *) ec->xkb_names.variant);
2883 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002884
Daniel Stonee379da92012-05-30 16:32:04 +01002885 xkb_info_destroy(&ec->xkb_info);
2886 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002887}
2888
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002889static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002890weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002891{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002892 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002893
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002894 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2895 XKB_MOD_NAME_SHIFT);
2896 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2897 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002898 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2899 XKB_MOD_NAME_CTRL);
2900 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2901 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002902 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2903 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002904 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2905 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002906 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002907
2908 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2909 XKB_LED_NAME_NUM);
2910 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2911 XKB_LED_NAME_CAPS);
2912 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2913 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002914
2915 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2916 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002917 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002918 exit(EXIT_FAILURE);
2919 }
2920 xkb_info->keymap_size = strlen(keymap_str) + 1;
2921
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002922 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002923 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002924 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002925 (unsigned long) xkb_info->keymap_size);
2926 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002927 }
2928
2929 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2930 PROT_READ | PROT_WRITE,
2931 MAP_SHARED, xkb_info->keymap_fd, 0);
2932 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002933 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002934 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002935 goto err_dev_zero;
2936 }
2937 strcpy(xkb_info->keymap_area, keymap_str);
2938 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002939
2940 return;
2941
2942err_dev_zero:
2943 close(xkb_info->keymap_fd);
2944 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002945err_keymap_str:
2946 free(keymap_str);
2947 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002948}
2949
2950static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002951weston_compositor_build_global_keymap(struct weston_compositor *ec)
2952{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002953 if (ec->xkb_info.keymap != NULL)
2954 return;
2955
Daniel Stonee379da92012-05-30 16:32:04 +01002956 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002957 &ec->xkb_names,
2958 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002959 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002960 weston_log("failed to compile global XKB keymap\n");
2961 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002962 "options %s",
2963 ec->xkb_names.rules, ec->xkb_names.model,
2964 ec->xkb_names.layout, ec->xkb_names.variant,
2965 ec->xkb_names.options);
2966 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002967 }
2968
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002969 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002970}
2971
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002972WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002973weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002974{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002975 if (seat->has_keyboard)
2976 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002977
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002978 if (keymap != NULL) {
2979 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002980 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002981 }
2982 else {
2983 weston_compositor_build_global_keymap(seat->compositor);
2984 seat->xkb_info = seat->compositor->xkb_info;
2985 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2986 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002987
2988 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2989 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002990 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01002991 exit(1);
2992 }
2993
Daniel Stone71c38772012-06-22 13:21:30 +01002994 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002995
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002996 wl_keyboard_init(&seat->keyboard);
2997 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2998
2999 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01003000}
3001
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003002WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01003003weston_seat_init_pointer(struct weston_seat *seat)
3004{
3005 if (seat->has_pointer)
3006 return;
3007
3008 wl_pointer_init(&seat->pointer);
3009 wl_seat_set_pointer(&seat->seat, &seat->pointer);
3010
3011 seat->has_pointer = 1;
3012}
3013
3014WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01003015weston_seat_init_touch(struct weston_seat *seat)
3016{
3017 if (seat->has_touch)
3018 return;
3019
3020 wl_touch_init(&seat->touch);
3021 wl_seat_set_touch(&seat->seat, &seat->touch);
3022
3023 seat->has_touch = 1;
3024}
3025
3026WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01003027weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05003028{
Daniel Stone37816df2012-05-16 18:45:18 +01003029 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01003030 seat->has_pointer = 0;
3031 seat->has_keyboard = 0;
3032 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05003033
Daniel Stone37816df2012-05-16 18:45:18 +01003034 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
3035 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003036
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003037 seat->sprite = NULL;
3038 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04003039
Daniel Stone37816df2012-05-16 18:45:18 +01003040 seat->compositor = ec;
3041 seat->hotspot_x = 16;
3042 seat->hotspot_y = 16;
3043 seat->modifier_state = 0;
3044 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05003045
Daniel Stone37816df2012-05-16 18:45:18 +01003046 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003047 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02003048
Daniel Stone37816df2012-05-16 18:45:18 +01003049 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03003050
Daniel Stone37816df2012-05-16 18:45:18 +01003051 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
3052 wl_signal_add(&seat->seat.drag_icon_signal,
3053 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04003054
3055 clipboard_create(seat);
Jan Arne Petersene829adc2012-08-10 16:47:22 +02003056 input_method_create(ec, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05003057}
3058
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003059WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01003060weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003061{
Daniel Stone37816df2012-05-16 18:45:18 +01003062 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003063 /* The global object is destroyed at wl_display_destroy() time. */
3064
Daniel Stone37816df2012-05-16 18:45:18 +01003065 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003066 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003067
Daniel Stone74419a22012-05-30 16:32:02 +01003068 if (seat->xkb_state.state != NULL)
3069 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01003070 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01003071
Daniel Stone37816df2012-05-16 18:45:18 +01003072 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003073}
3074
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003075static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003076drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
3077{
3078 weston_surface_configure(es,
3079 es->geometry.x + sx, es->geometry.y + sy,
3080 es->buffer->width, es->buffer->height);
3081}
3082
3083static int
Daniel Stone37816df2012-05-16 18:45:18 +01003084device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003085 struct weston_surface *surface)
3086{
Daniel Stone37816df2012-05-16 18:45:18 +01003087 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003088
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003089 if (surface->configure) {
3090 wl_resource_post_error(&surface->surface.resource,
3091 WL_DISPLAY_ERROR_INVALID_OBJECT,
3092 "surface->configure already set");
3093 return 0;
3094 }
3095
Daniel Stone37816df2012-05-16 18:45:18 +01003096 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003097
Daniel Stone37816df2012-05-16 18:45:18 +01003098 weston_surface_set_position(ws->drag_surface,
3099 wl_fixed_to_double(seat->pointer->x),
3100 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003101
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003102 surface->configure = drag_surface_configure;
3103
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003104 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01003105 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003106
3107 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003108}
3109
3110static void
Daniel Stone37816df2012-05-16 18:45:18 +01003111device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003112{
Daniel Stone37816df2012-05-16 18:45:18 +01003113 seat->drag_surface->configure = NULL;
3114 undef_region(&seat->drag_surface->input);
3115 wl_list_remove(&seat->drag_surface_destroy_listener.link);
3116 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003117}
3118
3119static void
Daniel Stone37816df2012-05-16 18:45:18 +01003120device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003121{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003122 struct wl_list *list;
3123
Daniel Stone37816df2012-05-16 18:45:18 +01003124 if (weston_surface_is_mapped(seat->drag_surface) ||
3125 !seat->drag_surface->buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003126 return;
3127
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003128 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
3129 list = &seat->sprite->layer_link;
3130 else
3131 list = &seat->compositor->cursor_layer.surface_list;
3132
3133 wl_list_insert(list, &seat->drag_surface->layer_link);
Daniel Stone37816df2012-05-16 18:45:18 +01003134 weston_surface_assign_output(seat->drag_surface);
3135 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003136}
3137
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003138static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003139weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01003140 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003141{
3142 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003143
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003144 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003145 return;
3146
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003147 if (seat->drag_surface && seat->seat.drag_surface &&
3148 (&seat->drag_surface->surface.resource !=
3149 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003150 /* between calls to this funcion we got a new drag_surface */
3151 surface_changed = 1;
3152
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003153 if (!seat->seat.drag_surface || surface_changed) {
3154 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003155 if (!surface_changed)
3156 return;
3157 }
3158
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003159 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003160 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003161 seat->seat.drag_surface;
3162 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003163 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003164 }
3165
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003166 /* the client may not have attached a buffer to the drag surface
3167 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003168 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003169
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02003170 /* the client may have attached a buffer with a different size to
3171 * the drag surface, causing the input region to be reset */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003172 if (region_is_undefined(&seat->drag_surface->input))
3173 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02003174
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003175 if (!dx && !dy)
3176 return;
3177
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003178 weston_surface_set_position(seat->drag_surface,
3179 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
3180 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003181}
3182
3183WL_EXPORT void
3184weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
3185{
Daniel Stone4dab5db2012-05-30 16:31:53 +01003186 struct weston_seat *seat;
3187
3188 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003189 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003190}
3191
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003192static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003193bind_output(struct wl_client *client,
3194 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003195{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003196 struct weston_output *output = data;
3197 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003198 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003199
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003200 resource = wl_client_add_object(client,
3201 &wl_output_interface, NULL, id, data);
3202
Casey Dahlin9074db52012-04-19 22:50:09 -04003203 wl_list_insert(&output->resource_list, &resource->link);
3204 resource->destroy = unbind_resource;
3205
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003206 wl_output_send_geometry(resource,
3207 output->x,
3208 output->y,
3209 output->mm_width,
3210 output->mm_height,
3211 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003212 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003213 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003214
3215 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003216 wl_output_send_mode(resource,
3217 mode->flags,
3218 mode->width,
3219 mode->height,
3220 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003221 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003222}
3223
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003224static const char vertex_shader[] =
3225 "uniform mat4 proj;\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05003226 "attribute vec2 position;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003227 "attribute vec2 texcoord;\n"
3228 "varying vec2 v_texcoord;\n"
3229 "void main()\n"
3230 "{\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05003231 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003232 " v_texcoord = texcoord;\n"
3233 "}\n";
3234
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02003235/* Declare common fragment shader uniforms */
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003236#define FRAGMENT_CONVERT_YUV \
Rob Clark0e5a2d02012-08-30 16:47:18 -05003237 " y *= alpha;\n" \
3238 " u *= alpha;\n" \
3239 " v *= alpha;\n" \
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003240 " gl_FragColor.r = y + 1.59602678 * v;\n" \
3241 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
3242 " gl_FragColor.b = y + 2.01723214 * u;\n" \
Rob Clark0e5a2d02012-08-30 16:47:18 -05003243 " gl_FragColor.a = alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003244
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02003245static const char texture_fragment_shader_rgba[] =
Kristian Høgsbergdfce71d2010-12-07 20:19:10 -05003246 "precision mediump float;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003247 "varying vec2 v_texcoord;\n"
3248 "uniform sampler2D tex;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003249 "uniform float alpha;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003250 "void main()\n"
3251 "{\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003252 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003253 "}\n";
3254
Pekka Paalanen6b5585b2012-08-30 16:47:19 -05003255static const char texture_fragment_shader_rgbx[] =
3256 "precision mediump float;\n"
3257 "varying vec2 v_texcoord;\n"
3258 "uniform sampler2D tex;\n"
3259 "uniform float alpha;\n"
3260 "void main()\n"
3261 "{\n"
3262 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
3263 " gl_FragColor.a = alpha;\n"
3264 "}\n";
3265
Rob Clarke3b95912012-08-31 16:42:18 -05003266static const char texture_fragment_shader_egl_external[] =
3267 "#extension GL_OES_EGL_image_external : require\n"
3268 "precision mediump float;\n"
3269 "varying vec2 v_texcoord;\n"
3270 "uniform samplerExternalOES tex;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003271 "uniform float alpha;\n"
Rob Clarke3b95912012-08-31 16:42:18 -05003272 "void main()\n"
3273 "{\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003274 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Rob Clarke3b95912012-08-31 16:42:18 -05003275 "}\n";
3276
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003277static const char texture_fragment_shader_y_uv[] =
3278 "precision mediump float;\n"
3279 "uniform sampler2D tex;\n"
3280 "uniform sampler2D tex1;\n"
3281 "varying vec2 v_texcoord;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003282 "uniform float alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003283 "void main() {\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003284 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
3285 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
3286 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
3287 FRAGMENT_CONVERT_YUV
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003288 "}\n";
3289
3290static const char texture_fragment_shader_y_u_v[] =
3291 "precision mediump float;\n"
3292 "uniform sampler2D tex;\n"
3293 "uniform sampler2D tex1;\n"
3294 "uniform sampler2D tex2;\n"
3295 "varying vec2 v_texcoord;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003296 "uniform float alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003297 "void main() {\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003298 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
3299 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
3300 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
3301 FRAGMENT_CONVERT_YUV
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003302 "}\n";
3303
3304static const char texture_fragment_shader_y_xuxv[] =
3305 "precision mediump float;\n"
3306 "uniform sampler2D tex;\n"
3307 "uniform sampler2D tex1;\n"
3308 "varying vec2 v_texcoord;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003309 "uniform float alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003310 "void main() {\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003311 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
3312 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
3313 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
3314 FRAGMENT_CONVERT_YUV
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003315 "}\n";
3316
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003317static const char solid_fragment_shader[] =
3318 "precision mediump float;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003319 "uniform vec4 color;\n"
Kristian Høgsbergdbae80e2012-03-29 11:34:39 -04003320 "uniform float alpha;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003321 "void main()\n"
3322 "{\n"
Kristian Høgsbergdbae80e2012-03-29 11:34:39 -04003323 " gl_FragColor = alpha * color\n;"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003324 "}\n";
3325
Kristian Høgsberga9468212010-06-14 21:03:11 -04003326static int
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003327compile_shader(GLenum type, const char *source)
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003328{
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003329 GLuint s;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003330 char msg[512];
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003331 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003332
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003333 s = glCreateShader(type);
3334 glShaderSource(s, 1, &source, NULL);
3335 glCompileShader(s);
3336 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003337 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003338 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
Martin Minarik6d118362012-06-07 18:01:59 +02003339 weston_log("shader info: %s\n", msg);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003340 return GL_NONE;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003341 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003342
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003343 return s;
3344}
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003345
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003346static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003347weston_shader_init(struct weston_shader *shader,
3348 const char *vertex_source, const char *fragment_source)
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003349{
3350 char msg[512];
3351 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003352
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003353 shader->vertex_shader =
3354 compile_shader(GL_VERTEX_SHADER, vertex_source);
3355 shader->fragment_shader =
3356 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
3357
3358 shader->program = glCreateProgram();
3359 glAttachShader(shader->program, shader->vertex_shader);
3360 glAttachShader(shader->program, shader->fragment_shader);
3361 glBindAttribLocation(shader->program, 0, "position");
3362 glBindAttribLocation(shader->program, 1, "texcoord");
3363
3364 glLinkProgram(shader->program);
3365 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003366 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003367 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
Martin Minarik6d118362012-06-07 18:01:59 +02003368 weston_log("link info: %s\n", msg);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003369 return -1;
3370 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003371
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003372 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02003373 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
3374 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
3375 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
Kristian Høgsberg541e5552011-12-14 09:24:11 -05003376 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
Pekka Paalanenf55f5442012-02-02 16:49:05 +02003377 shader->color_uniform = glGetUniformLocation(shader->program, "color");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003378
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003379 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003380}
3381
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003382WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003383weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003384{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003385 struct weston_compositor *c = output->compositor;
3386
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003387 pixman_region32_fini(&output->region);
Kristian Høgsberg8b72f602011-06-23 20:46:34 -04003388 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003389 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003390
3391 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003392}
3393
Scott Moreau1bad5db2012-08-18 01:04:05 -06003394static void
3395weston_output_compute_transform(struct weston_output *output)
3396{
3397 struct weston_matrix transform;
3398 int flip;
3399
3400 weston_matrix_init(&transform);
3401
3402 switch(output->transform) {
3403 case WL_OUTPUT_TRANSFORM_FLIPPED:
3404 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3405 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3406 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3407 flip = -1;
3408 break;
3409 default:
3410 flip = 1;
3411 break;
3412 }
3413
3414 switch(output->transform) {
3415 case WL_OUTPUT_TRANSFORM_NORMAL:
3416 case WL_OUTPUT_TRANSFORM_FLIPPED:
3417 transform.d[0] = flip;
3418 transform.d[1] = 0;
3419 transform.d[4] = 0;
3420 transform.d[5] = 1;
3421 break;
3422 case WL_OUTPUT_TRANSFORM_90:
3423 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3424 transform.d[0] = 0;
3425 transform.d[1] = -flip;
3426 transform.d[4] = 1;
3427 transform.d[5] = 0;
3428 break;
3429 case WL_OUTPUT_TRANSFORM_180:
3430 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3431 transform.d[0] = -flip;
3432 transform.d[1] = 0;
3433 transform.d[4] = 0;
3434 transform.d[5] = -1;
3435 break;
3436 case WL_OUTPUT_TRANSFORM_270:
3437 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3438 transform.d[0] = 0;
3439 transform.d[1] = flip;
3440 transform.d[4] = -1;
3441 transform.d[5] = 0;
3442 break;
3443 default:
3444 break;
3445 }
3446
3447 weston_matrix_multiply(&output->matrix, &transform);
3448}
3449
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003450WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003451weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003452{
Scott Moreau850ca422012-05-21 15:21:25 -06003453 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003454 struct weston_matrix camera;
3455 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003456
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003457 weston_matrix_init(&output->matrix);
3458 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003459 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
3460 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003461
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003462 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003463 2.0 / (output->width + output->border.left + output->border.right),
3464 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
3465
3466 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003467
Scott Moreauccbf29d2012-02-22 14:21:41 -07003468 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003469 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003470 weston_matrix_init(&camera);
3471 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06003472 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06003473 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003474 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003475 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003476 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003477 weston_matrix_multiply(&output->matrix, &modelview);
3478 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003479
Scott Moreauccbf29d2012-02-22 14:21:41 -07003480 output->dirty = 0;
3481}
3482
Scott Moreau1bad5db2012-08-18 01:04:05 -06003483static void
3484weston_output_transform_init(struct weston_output *output, uint32_t transform)
3485{
3486 output->transform = transform;
3487
3488 switch (transform) {
3489 case WL_OUTPUT_TRANSFORM_90:
3490 case WL_OUTPUT_TRANSFORM_270:
3491 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3492 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3493 /* Swap width and height */
3494 output->width = output->current->height;
3495 output->height = output->current->width;
3496 break;
3497 case WL_OUTPUT_TRANSFORM_NORMAL:
3498 case WL_OUTPUT_TRANSFORM_180:
3499 case WL_OUTPUT_TRANSFORM_FLIPPED:
3500 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3501 output->width = output->current->width;
3502 output->height = output->current->height;
3503 break;
3504 default:
3505 break;
3506 }
3507}
3508
Scott Moreauccbf29d2012-02-22 14:21:41 -07003509WL_EXPORT void
3510weston_output_move(struct weston_output *output, int x, int y)
3511{
3512 output->x = x;
3513 output->y = y;
3514
3515 pixman_region32_init(&output->previous_damage);
3516 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003517 output->width,
3518 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003519}
3520
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003521WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003522weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003523 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003524{
3525 output->compositor = c;
3526 output->x = x;
3527 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05003528 output->border.top = 0;
3529 output->border.bottom = 0;
3530 output->border.left = 0;
3531 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003532 output->mm_width = width;
3533 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003534 output->dirty = 1;
3535
Scott Moreau1bad5db2012-08-18 01:04:05 -06003536 if (transform != WL_OUTPUT_TRANSFORM_NORMAL)
3537 output->disable_planes++;
3538
3539 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06003540 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003541
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003542 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003543 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003544
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003545 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003546 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003547 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003548
Casey Dahlin58ba1372012-04-19 22:50:08 -04003549 output->id = ffs(~output->compositor->output_id_pool) - 1;
3550 output->compositor->output_id_pool |= 1 << output->id;
3551
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003552 output->global =
3553 wl_display_add_global(c->wl_display, &wl_output_interface,
3554 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003555}
3556
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003557static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003558compositor_bind(struct wl_client *client,
3559 void *data, uint32_t version, uint32_t id)
3560{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003561 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003562
3563 wl_client_add_object(client, &wl_compositor_interface,
3564 &compositor_interface, id, compositor);
3565}
3566
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003567static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003568log_uname(void)
3569{
3570 struct utsname usys;
3571
3572 uname(&usys);
3573
3574 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3575 usys.version, usys.machine);
3576}
3577
3578static void
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003579log_extensions(const char *name, const char *extensions)
3580{
3581 const char *p, *end;
3582 int l;
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003583 int len;
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003584
3585 l = weston_log("%s:", name);
3586 p = extensions;
3587 while (*p) {
3588 end = strchrnul(p, ' ');
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003589 len = end - p;
3590 if (l + len > 78)
Pekka Paalanen4e4167d2012-06-11 14:06:05 +03003591 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003592 len, p);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003593 else
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003594 l += weston_log_continue(" %.*s", len, p);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003595 for (p = end; isspace(*p); p++)
3596 ;
3597 }
3598 weston_log_continue("\n");
3599}
3600
Pekka Paalanen4e4167d2012-06-11 14:06:05 +03003601static void
3602log_egl_gl_info(EGLDisplay egldpy)
3603{
3604 const char *str;
3605
3606 str = eglQueryString(egldpy, EGL_VERSION);
3607 weston_log("EGL version: %s\n", str ? str : "(null)");
3608
3609 str = eglQueryString(egldpy, EGL_VENDOR);
3610 weston_log("EGL vendor: %s\n", str ? str : "(null)");
3611
3612 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
3613 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
3614
3615 str = eglQueryString(egldpy, EGL_EXTENSIONS);
3616 log_extensions("EGL extensions", str ? str : "(null)");
3617
3618 str = (char *)glGetString(GL_VERSION);
3619 weston_log("GL version: %s\n", str ? str : "(null)");
3620
3621 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
3622 weston_log("GLSL version: %s\n", str ? str : "(null)");
3623
3624 str = (char *)glGetString(GL_VENDOR);
3625 weston_log("GL vendor: %s\n", str ? str : "(null)");
3626
3627 str = (char *)glGetString(GL_RENDERER);
3628 weston_log("GL renderer: %s\n", str ? str : "(null)");
3629
3630 str = (char *)glGetString(GL_EXTENSIONS);
3631 log_extensions("GL extensions", str ? str : "(null)");
3632}
3633
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003634WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003635weston_compositor_init(struct weston_compositor *ec,
3636 struct wl_display *display,
3637 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003638 char *argv[],
3639 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003640{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003641 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003642 struct xkb_rule_names xkb_names;
3643 const struct config_key keyboard_config_keys[] = {
3644 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
3645 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
3646 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
3647 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
3648 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
3649 };
3650 const struct config_section cs[] = {
3651 { "keyboard",
3652 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
3653 };
3654
3655 memset(&xkb_names, 0, sizeof(xkb_names));
3656 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003657
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003658 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003659 wl_signal_init(&ec->destroy_signal);
3660 wl_signal_init(&ec->activate_signal);
3661 wl_signal_init(&ec->lock_signal);
3662 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003663 wl_signal_init(&ec->show_input_panel_signal);
3664 wl_signal_init(&ec->hide_input_panel_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01003665 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003666
Casey Dahlin58ba1372012-04-19 22:50:08 -04003667 ec->output_id_pool = 0;
3668
Kristian Høgsberga8873122011-11-23 10:39:34 -05003669 if (!wl_display_add_global(display, &wl_compositor_interface,
3670 ec, compositor_bind))
3671 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003672
Daniel Stone725c2c32012-06-22 14:04:36 +01003673 wl_list_init(&ec->surface_list);
3674 wl_list_init(&ec->layer_list);
3675 wl_list_init(&ec->seat_list);
3676 wl_list_init(&ec->output_list);
3677 wl_list_init(&ec->key_binding_list);
3678 wl_list_init(&ec->button_binding_list);
3679 wl_list_init(&ec->axis_binding_list);
3680 wl_list_init(&ec->fade.animation.link);
3681
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003682 weston_plane_init(&ec->primary_plane, 0, 0);
3683
Daniel Stone725c2c32012-06-22 14:04:36 +01003684 weston_compositor_xkb_init(ec, &xkb_names);
3685
3686 ec->ping_handler = NULL;
3687
3688 screenshooter_create(ec);
3689 text_cursor_position_notifier_create(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003690
3691 wl_data_device_manager_init(ec->wl_display);
3692
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003693 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003694
Daniel Stone725c2c32012-06-22 14:04:36 +01003695 loop = wl_display_get_event_loop(ec->wl_display);
3696 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3697 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3698
3699 ec->input_loop = wl_event_loop_create();
3700
3701 return 0;
3702}
3703
3704WL_EXPORT int
3705weston_compositor_init_gl(struct weston_compositor *ec)
3706{
3707 const char *extensions;
Rob Clarke3b95912012-08-31 16:42:18 -05003708 int has_egl_image_external = 0;
Daniel Stone725c2c32012-06-22 14:04:36 +01003709
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003710 log_egl_gl_info(ec->egl_display);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003711
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003712 ec->image_target_texture_2d =
3713 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3714 ec->image_target_renderbuffer_storage = (void *)
3715 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
3716 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3717 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3718 ec->bind_display =
3719 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
3720 ec->unbind_display =
3721 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02003722 ec->query_buffer =
3723 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003724
3725 extensions = (const char *) glGetString(GL_EXTENSIONS);
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003726 if (!extensions) {
Martin Minarik6d118362012-06-07 18:01:59 +02003727 weston_log("Retrieving GL extension string failed.\n");
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003728 return -1;
3729 }
3730
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003731 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
Martin Minarik6d118362012-06-07 18:01:59 +02003732 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003733 return -1;
3734 }
3735
Pekka Paalanena1d57db2012-04-17 15:02:08 +03003736 if (strstr(extensions, "GL_EXT_read_format_bgra"))
3737 ec->read_format = GL_BGRA_EXT;
3738 else
3739 ec->read_format = GL_RGBA;
Kristian Høgsbergf9247dd2012-03-27 15:25:46 -04003740
Pekka Paalanen52bfbaa2012-04-11 16:19:37 +03003741 if (strstr(extensions, "GL_EXT_unpack_subimage"))
3742 ec->has_unpack_subimage = 1;
Kristian Høgsbergfb6de222012-03-27 17:10:13 -04003743
Rob Clarke3b95912012-08-31 16:42:18 -05003744 if (strstr(extensions, "GL_OES_EGL_image_external"))
3745 has_egl_image_external = 1;
3746
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003747 extensions =
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003748 (const char *) eglQueryString(ec->egl_display, EGL_EXTENSIONS);
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003749 if (!extensions) {
Martin Minarik6d118362012-06-07 18:01:59 +02003750 weston_log("Retrieving EGL extension string failed.\n");
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003751 return -1;
3752 }
3753
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003754 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
3755 ec->has_bind_display = 1;
3756 if (ec->has_bind_display)
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003757 ec->bind_display(ec->egl_display, ec->wl_display);
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04003758
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003759 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003760 ec->fade.animation.frame = fade_frame;
Kristian Høgsberg3555d092011-04-11 13:58:13 -04003761
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003762 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3763 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3764
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003765 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003766
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02003767 if (weston_shader_init(&ec->texture_shader_rgba,
3768 vertex_shader, texture_fragment_shader_rgba) < 0)
Kristian Høgsberga9468212010-06-14 21:03:11 -04003769 return -1;
Pekka Paalanen6b5585b2012-08-30 16:47:19 -05003770 if (weston_shader_init(&ec->texture_shader_rgbx,
3771 vertex_shader, texture_fragment_shader_rgbx) < 0)
3772 return -1;
Rob Clarke3b95912012-08-31 16:42:18 -05003773 if (has_egl_image_external &&
3774 weston_shader_init(&ec->texture_shader_egl_external,
3775 vertex_shader, texture_fragment_shader_egl_external) < 0)
3776 return -1;
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003777 if (weston_shader_init(&ec->texture_shader_y_uv,
3778 vertex_shader, texture_fragment_shader_y_uv) < 0)
3779 return -1;
3780 if (weston_shader_init(&ec->texture_shader_y_u_v,
3781 vertex_shader, texture_fragment_shader_y_u_v) < 0)
3782 return -1;
3783 if (weston_shader_init(&ec->texture_shader_y_xuxv,
3784 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
3785 return -1;
Pekka Paalanenf55f5442012-02-02 16:49:05 +02003786 if (weston_shader_init(&ec->solid_shader,
3787 vertex_shader, solid_fragment_shader) < 0)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003788 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05003789
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003790 weston_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003791
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003792 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003793}
3794
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003795WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003796weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003797{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003798 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003799
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003800 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003801 if (ec->input_loop_source)
3802 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003803
Matt Roper361d2ad2011-08-29 13:52:23 -07003804 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003805 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003806 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003807
Daniel Stone325fc2d2012-05-30 16:31:58 +01003808 weston_binding_list_destroy_all(&ec->key_binding_list);
3809 weston_binding_list_destroy_all(&ec->button_binding_list);
3810 weston_binding_list_destroy_all(&ec->axis_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003811
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003812 weston_plane_release(&ec->primary_plane);
3813
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003814 wl_array_release(&ec->vertices);
3815 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05003816 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003817
3818 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07003819}
3820
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003821static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003822{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003823 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003824
Martin Minarik6d118362012-06-07 18:01:59 +02003825 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003826 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003827
3828 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003829}
3830
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003831static void
3832on_segv_signal(int s, siginfo_t *siginfo, void *context)
3833{
3834 void *buffer[32];
3835 int i, count;
3836 Dl_info info;
3837
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003838 /* This SIGSEGV handler will do a best-effort backtrace, and
3839 * then call the backend restore function, which will switch
3840 * back to the vt we launched from or ungrab X etc and then
3841 * raise SIGTRAP. If we run weston under gdb from X or a
3842 * different vt, and tell gdb "handle SIGSEGV nostop", this
3843 * will allow weston to switch back to gdb on crash and then
3844 * gdb will catch the crash with SIGTRAP. */
3845
Martin Minarik6d118362012-06-07 18:01:59 +02003846 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003847
3848 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3849 for (i = 0; i < count; i++) {
3850 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02003851 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003852 (long) buffer[i],
3853 info.dli_sname ? info.dli_sname : "--",
3854 info.dli_fname);
3855 }
3856
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003857 segv_compositor->restore(segv_compositor);
3858
3859 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003860}
3861
3862
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003863static void *
3864load_module(const char *name, const char *entrypoint, void **handle)
3865{
3866 char path[PATH_MAX];
3867 void *module, *init;
3868
3869 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003870 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003871 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003872 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003873
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003874 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003875 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003876 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003877 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003878 return NULL;
3879 }
3880
3881 init = dlsym(module, entrypoint);
3882 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003883 weston_log("Failed to lookup init function: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003884 return NULL;
3885 }
3886
3887 return init;
3888}
3889
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003890static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003891 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3892
3893static const char xdg_wrong_message[] =
3894 "fatal: environment variable XDG_RUNTIME_DIR\n"
3895 "is set to \"%s\", which is not a directory.\n";
3896
3897static const char xdg_wrong_mode_message[] =
3898 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3899 "correctly. Unix access mode must be 0700 but is %o,\n"
3900 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003901 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003902
3903static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003904 "Refer to your distribution on how to get it, or\n"
3905 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3906 "on how to implement it.\n";
3907
Martin Minarik37032f82012-06-18 20:15:18 +02003908static void
3909verify_xdg_runtime_dir(void)
3910{
3911 char *dir = getenv("XDG_RUNTIME_DIR");
3912 struct stat s;
3913
3914 if (!dir) {
3915 weston_log(xdg_error_message);
3916 weston_log_continue(xdg_detail_message);
3917 exit(EXIT_FAILURE);
3918 }
3919
3920 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3921 weston_log(xdg_wrong_message, dir);
3922 weston_log_continue(xdg_detail_message);
3923 exit(EXIT_FAILURE);
3924 }
3925
3926 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3927 weston_log(xdg_wrong_mode_message,
3928 dir, s.st_mode & 0777, s.st_uid);
3929 weston_log_continue(xdg_detail_message);
3930 }
3931}
3932
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003933static int
3934usage(int error_code)
3935{
3936 fprintf(stderr,
3937 "Usage: weston [OPTIONS]\n\n"
3938 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3939 "Weston supports multiple backends, and depending on which backend is in use\n"
3940 "different options will be accepted.\n\n"
3941
3942
3943 "Core options:\n\n"
3944 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3945 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3946 " -S, --socket=NAME\tName of socket to listen on\n"
3947 " -i, --idle-time=SECS\tIdle time in seconds\n"
3948 " --xserver\t\tEnable X server integration\n"
3949 " --module\t\tLoad the specified module\n"
3950 " --log==FILE\t\tLog to the given file\n"
3951 " -h, --help\t\tThis help message\n\n");
3952
3953 fprintf(stderr,
3954 "Options for drm-backend.so:\n\n"
3955 " --connector=ID\tBring up only this connector\n"
3956 " --seat=SEAT\t\tThe seat that weston should run on\n"
3957 " --tty=TTY\t\tThe tty to use\n"
3958 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3959
3960 fprintf(stderr,
3961 "Options for x11-backend.so:\n\n"
3962 " --width=WIDTH\t\tWidth of X window\n"
3963 " --height=HEIGHT\tHeight of X window\n"
3964 " --fullscreen\t\tRun in fullscreen mode\n"
3965 " --output-count=COUNT\tCreate multiple outputs\n"
3966 " --no-input\t\tDont create input devices\n\n");
3967
3968 fprintf(stderr,
3969 "Options for wayland-backend.so:\n\n"
3970 " --width=WIDTH\t\tWidth of Wayland surface\n"
3971 " --height=HEIGHT\tHeight of Wayland surface\n"
3972 " --display=DISPLAY\tWayland display to connect to\n\n");
3973
3974 exit(error_code);
3975}
3976
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003977int main(int argc, char *argv[])
3978{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003979 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003980 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003981 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003982 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003983 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003984 struct sigaction segv_action;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04003985 void *shell_module, *backend_module, *xserver_module;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003986 int (*module_init)(struct weston_compositor *ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003987 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003988 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003989 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003990 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003991 char *backend = NULL;
3992 char *shell = NULL;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003993 char *module = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003994 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003995 int32_t idle_time = 300;
Scott Moreaue17f6102012-04-25 10:03:06 -06003996 int32_t xserver = 0;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003997 int32_t help = 0;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003998 char *socket_name = NULL;
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003999 char *config_file;
4000
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004001 const struct config_key shell_config_keys[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004002 { "type", CONFIG_KEY_STRING, &shell },
4003 };
4004
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004005 const struct config_section cs[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004006 { "shell",
4007 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
4008 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004009
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004010 const struct weston_option core_options[] = {
4011 { WESTON_OPTION_STRING, "backend", 'B', &backend },
4012 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4013 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004014 { WESTON_OPTION_BOOLEAN, "xserver", 0, &xserver },
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004015 { WESTON_OPTION_STRING, "module", 0, &module },
Martin Minarik19e6f262012-06-07 13:08:46 +02004016 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004017 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004018 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004019
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004020 argc = parse_options(core_options,
4021 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004022
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004023 if (help)
4024 usage(EXIT_SUCCESS);
4025
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004026 weston_log_file_open(log);
4027
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004028 weston_log("%s\n"
4029 STAMP_SPACE "%s\n"
4030 STAMP_SPACE "Bug reports to: %s\n"
4031 STAMP_SPACE "Build: %s\n",
4032 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004033 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004034 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004035
Martin Minarik37032f82012-06-18 20:15:18 +02004036 verify_xdg_runtime_dir();
4037
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004038 display = wl_display_create();
4039
Tiago Vignatti2116b892011-08-08 05:52:59 -07004040 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004041 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4042 display);
4043 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4044 display);
4045 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4046 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004047
4048 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004049 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4050 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004051
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004052 if (!backend) {
4053 if (getenv("WAYLAND_DISPLAY"))
4054 backend = "wayland-backend.so";
4055 else if (getenv("DISPLAY"))
4056 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004057 else
4058 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004059 }
4060
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004061 config_file = config_file_path("weston.ini");
4062 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004063
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004064 backend_init = load_module(backend, "backend_init", &backend_module);
4065 if (!backend_init)
4066 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004067
Daniel Stonec1be8e52012-06-01 11:14:02 -04004068 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004069 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004070 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004071 exit(EXIT_FAILURE);
4072 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004073
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004074 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4075 segv_action.sa_sigaction = on_segv_signal;
4076 sigemptyset(&segv_action.sa_mask);
4077 sigaction(SIGSEGV, &segv_action, NULL);
4078 segv_compositor = ec;
4079
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004080 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03004081 weston_log("fatal: unhandled option: %s\n", argv[i]);
4082 if (argv[1]) {
4083 ret = EXIT_FAILURE;
4084 goto out;
4085 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004086
Daniel Stonec1be8e52012-06-01 11:14:02 -04004087 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01004088
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004089 ec->option_idle_time = idle_time;
4090 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004091
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004092 module_init = NULL;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04004093 if (xserver)
Tiago Vignatti629ce232012-05-23 23:04:14 +03004094 module_init = load_module("xwayland.so",
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004095 "weston_xserver_init",
4096 &xserver_module);
Pekka Paalanen33616392012-07-30 16:56:57 +03004097 if (module_init && module_init(ec) < 0) {
4098 ret = EXIT_FAILURE;
4099 goto out;
4100 }
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04004101
Kristian Høgsberg33d75092012-08-13 13:56:03 -04004102 if (socket_name)
4103 setenv("WAYLAND_DISPLAY", socket_name, 1);
4104
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004105 if (!shell)
4106 shell = "desktop-shell.so";
4107 module_init = load_module(shell, "shell_init", &shell_module);
Pekka Paalanen33616392012-07-30 16:56:57 +03004108 if (!module_init || module_init(ec) < 0) {
4109 ret = EXIT_FAILURE;
4110 goto out;
4111 }
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004112
4113
4114 module_init = NULL;
4115 if (module)
4116 module_init = load_module(module, "module_init", NULL);
Pekka Paalanen33616392012-07-30 16:56:57 +03004117 if (module_init && module_init(ec) < 0) {
4118 ret = EXIT_FAILURE;
4119 goto out;
4120 }
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004121
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004122 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004123 weston_log("fatal: failed to add socket: %m\n");
4124 ret = EXIT_FAILURE;
4125 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004126 }
4127
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004128 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004129 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004130
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004131 wl_display_run(display);
4132
4133 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004134 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05004135 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004136
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004137 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004138
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004139 if (ec->has_bind_display)
Kristian Høgsberg362b6722012-06-18 15:13:51 -04004140 ec->unbind_display(ec->egl_display, display);
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004141
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004142 for (i = ARRAY_LENGTH(signals); i;)
4143 wl_event_source_remove(signals[--i]);
4144
Daniel Stone855028f2012-05-01 20:37:10 +01004145 weston_compositor_xkb_destroy(ec);
4146
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004147 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004148 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004149
Martin Minarik19e6f262012-06-07 13:08:46 +02004150 weston_log_file_close();
4151
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004152 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004153}