blob: 70445ecba1bf4e550df3d64436e68bb0b4bd7d99 [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øgsberg890bc052008-12-30 14:31:33 -050051
Pekka Paalanen50719bc2011-11-22 14:18:50 +020052#include <wayland-server.h>
Kristian Høgsberg82863022010-06-04 21:52:02 -040053#include "compositor.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030054#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040055#include "git-version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050056
Kristian Høgsberg27da5382011-06-21 17:32:25 -040057static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040058static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040059
60static int
61sigchld_handler(int signal_number, void *data)
62{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050063 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040064 int status;
65 pid_t pid;
66
Bill Spitzak027b9622012-03-17 15:22:03 -070067 pid = waitpid(-1, &status, WNOHANG);
68 if (!pid)
69 return 1;
70
Kristian Høgsberg27da5382011-06-21 17:32:25 -040071 wl_list_for_each(p, &child_process_list, link) {
72 if (p->pid == pid)
73 break;
74 }
75
76 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020077 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040078 return 1;
79 }
80
81 wl_list_remove(&p->link);
82 p->cleanup(p, status);
83
84 return 1;
85}
86
Alex Wu2dda6042012-04-17 17:20:47 +080087WL_EXPORT int
88weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode)
89{
90 if (!output->switch_mode)
91 return -1;
92
93 return output->switch_mode(output, mode);
94}
95
Kristian Høgsberg27da5382011-06-21 17:32:25 -040096WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050097weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -040098{
99 wl_list_insert(&child_process_list, &process->link);
100}
101
Benjamin Franzke06286262011-05-06 19:12:33 +0200102static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200103child_client_exec(int sockfd, const char *path)
104{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500105 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200106 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200107 sigset_t allsigs;
108
109 /* do not give our signal mask to the new process */
110 sigfillset(&allsigs);
111 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200112
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500113 /* Launch clients as the user. */
114 seteuid(getuid());
115
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500116 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
117 * non-CLOEXEC fd to pass through exec. */
118 clientfd = dup(sockfd);
119 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200120 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500121 return;
122 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200123
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500124 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200125 setenv("WAYLAND_SOCKET", s, 1);
126
127 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200128 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200129 path);
130}
131
132WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500133weston_client_launch(struct weston_compositor *compositor,
134 struct weston_process *proc,
135 const char *path,
136 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200137{
138 int sv[2];
139 pid_t pid;
140 struct wl_client *client;
141
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300142 weston_log("launching '%s'\n", path);
143
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300144 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200145 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200146 "socketpair failed while launching '%s': %m\n",
147 path);
148 return NULL;
149 }
150
151 pid = fork();
152 if (pid == -1) {
153 close(sv[0]);
154 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200155 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200156 "fork failed while launching '%s': %m\n", path);
157 return NULL;
158 }
159
160 if (pid == 0) {
161 child_client_exec(sv[1], path);
162 exit(-1);
163 }
164
165 close(sv[1]);
166
167 client = wl_client_create(compositor->wl_display, sv[0]);
168 if (!client) {
169 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200170 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200171 "wl_client_create failed while launching '%s'.\n",
172 path);
173 return NULL;
174 }
175
176 proc->pid = pid;
177 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500178 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200179
180 return client;
181}
182
183static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400184surface_handle_buffer_destroy(struct wl_listener *listener, void *data)
Benjamin Franzke06286262011-05-06 19:12:33 +0200185{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500186 struct weston_surface *es =
187 container_of(listener, struct weston_surface,
188 buffer_destroy_listener);
Benjamin Franzke06286262011-05-06 19:12:33 +0200189
Kristian Høgsberga85b4fb2012-06-20 09:24:35 -0400190 if (es->buffer && wl_buffer_is_shm(es->buffer))
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400191 gles2_renderer_flush_damage(es);
Kristian Høgsberga85b4fb2012-06-20 09:24:35 -0400192
Kristian Høgsberg24596942011-10-24 17:51:02 -0400193 es->buffer = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200194}
195
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500196static const pixman_region32_data_t undef_region_data;
197
198static void
199undef_region(pixman_region32_t *region)
200{
Kristian Høgsberg66a099b2012-05-29 16:49:45 -0400201 if (region->data != &undef_region_data)
202 pixman_region32_fini(region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500203 region->data = (pixman_region32_data_t *) &undef_region_data;
204}
205
206static int
207region_is_undefined(pixman_region32_t *region)
208{
209 return region->data == &undef_region_data;
210}
211
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200212static void
213empty_region(pixman_region32_t *region)
214{
215 if (!region_is_undefined(region))
216 pixman_region32_fini(region);
217
218 pixman_region32_init(region);
219}
220
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500221WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500222weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500223{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500224 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400225
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200226 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400227 if (surface == NULL)
228 return NULL;
229
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400230 wl_signal_init(&surface->surface.resource.destroy_signal);
Kristian Høgsberg48891542012-02-23 17:38:33 -0500231
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500232 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500233 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500234
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400235 surface->surface.resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400236
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500237 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400238 surface->alpha = 1.0;
Juan Zhao46436612012-03-20 01:48:46 +0800239 surface->pitch = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400240
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200241 surface->num_textures = 0;
242 surface->num_images = 0;
243
Benjamin Franzke06286262011-05-06 19:12:33 +0200244 surface->buffer = NULL;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400245 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400246 surface->plane = &compositor->primary_plane;
Benjamin Franzke06286262011-05-06 19:12:33 +0200247
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400248 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500249 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500250 pixman_region32_init(&surface->clip);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500251 undef_region(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200252 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500253 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400254
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400255 surface->buffer_destroy_listener.notify =
256 surface_handle_buffer_destroy;
Benjamin Franzke06286262011-05-06 19:12:33 +0200257
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200258 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200259 wl_list_insert(&surface->geometry.transformation_list,
260 &surface->transform.position.link);
261 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200262 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200263 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500264
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400265 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500266}
267
Alex Wu8811bf92012-02-28 18:07:54 +0800268WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500269weston_surface_set_color(struct weston_surface *surface,
270 GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
271{
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500272 surface->color[0] = red;
273 surface->color[1] = green;
274 surface->color[2] = blue;
275 surface->color[3] = alpha;
276 surface->shader = &surface->compositor->solid_shader;
277}
278
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400279WL_EXPORT void
280weston_surface_to_global_float(struct weston_surface *surface,
281 GLfloat sx, GLfloat sy, GLfloat *x, GLfloat *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200282{
283 if (surface->transform.enabled) {
284 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
285
286 weston_matrix_transform(&surface->transform.matrix, &v);
287
288 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200289 weston_log("warning: numerical instability in "
Pekka Paalanenece8a012012-02-08 15:23:15 +0200290 "weston_surface_to_global(), divisor = %g\n",
291 v.f[3]);
292 *x = 0;
293 *y = 0;
294 return;
295 }
296
297 *x = v.f[0] / v.f[3];
298 *y = v.f[1] / v.f[3];
299 } else {
300 *x = sx + surface->geometry.x;
301 *y = sy + surface->geometry.y;
302 }
303}
304
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500305WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400306weston_surface_move_to_plane(struct weston_surface *surface,
307 struct weston_plane *plane)
308{
309 if (surface->plane == plane)
310 return;
311
312 weston_surface_damage_below(surface);
313 surface->plane = plane;
314 weston_surface_damage(surface);
315}
316
317WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500318weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200319{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500320 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200321
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500322 pixman_region32_init(&damage);
323 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
324 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400325 pixman_region32_union(&surface->plane->damage,
326 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500327 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200328}
329
330static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200331surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
332 int32_t width, int32_t height,
333 pixman_region32_t *bbox)
334{
Pekka Paalanen219b9822012-02-08 15:38:37 +0200335 GLfloat min_x = HUGE_VALF, min_y = HUGE_VALF;
336 GLfloat max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200337 int32_t s[4][2] = {
338 { sx, sy },
339 { sx, sy + height },
340 { sx + width, sy },
341 { sx + width, sy + height }
342 };
Pekka Paalanen219b9822012-02-08 15:38:37 +0200343 GLfloat int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200344 int i;
345
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300346 if (width == 0 || height == 0) {
347 /* avoid rounding empty bbox to 1x1 */
348 pixman_region32_init(bbox);
349 return;
350 }
351
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200352 for (i = 0; i < 4; ++i) {
Pekka Paalanen219b9822012-02-08 15:38:37 +0200353 GLfloat x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400354 weston_surface_to_global_float(surface,
355 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200356 if (x < min_x)
357 min_x = x;
358 if (x > max_x)
359 max_x = x;
360 if (y < min_y)
361 min_y = y;
362 if (y > max_y)
363 max_y = y;
364 }
365
Pekka Paalanen219b9822012-02-08 15:38:37 +0200366 int_x = floorf(min_x);
367 int_y = floorf(min_y);
368 pixman_region32_init_rect(bbox, int_x, int_y,
369 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200370}
371
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200372static void
373weston_surface_update_transform_disable(struct weston_surface *surface)
374{
375 surface->transform.enabled = 0;
376
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200377 /* round off fractions when not transformed */
378 surface->geometry.x = roundf(surface->geometry.x);
379 surface->geometry.y = roundf(surface->geometry.y);
380
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200381 pixman_region32_init_rect(&surface->transform.boundingbox,
382 surface->geometry.x,
383 surface->geometry.y,
384 surface->geometry.width,
385 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500386
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400387 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500388 pixman_region32_copy(&surface->transform.opaque,
389 &surface->opaque);
390 pixman_region32_translate(&surface->transform.opaque,
391 surface->geometry.x,
392 surface->geometry.y);
393 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200394}
395
396static int
397weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200398{
399 struct weston_matrix *matrix = &surface->transform.matrix;
400 struct weston_matrix *inverse = &surface->transform.inverse;
401 struct weston_transform *tform;
402
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200403 surface->transform.enabled = 1;
404
405 /* Otherwise identity matrix, but with x and y translation. */
406 surface->transform.position.matrix.d[12] = surface->geometry.x;
407 surface->transform.position.matrix.d[13] = surface->geometry.y;
408
409 weston_matrix_init(matrix);
410 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
411 weston_matrix_multiply(matrix, &tform->matrix);
412
413 if (weston_matrix_invert(inverse, matrix) < 0) {
414 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200415 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200416 " transformation not invertible.\n", surface);
417 return -1;
418 }
419
420 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
421 surface->geometry.height,
422 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500423
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200424 return 0;
425}
426
427WL_EXPORT void
428weston_surface_update_transform(struct weston_surface *surface)
429{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200430 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200431 return;
432
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200433 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200434
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500435 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200436
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200437 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500438 pixman_region32_fini(&surface->transform.opaque);
439 pixman_region32_init(&surface->transform.opaque);
440
441 if (region_is_undefined(&surface->input))
442 pixman_region32_init_rect(&surface->input, 0, 0,
443 surface->geometry.width,
444 surface->geometry.height);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200445
Pekka Paalanencd403622012-01-25 13:37:39 +0200446 /* transform.position is always in transformation_list */
447 if (surface->geometry.transformation_list.next ==
448 &surface->transform.position.link &&
449 surface->geometry.transformation_list.prev ==
450 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200451 weston_surface_update_transform_disable(surface);
452 } else {
453 if (weston_surface_update_transform_enable(surface) < 0)
454 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200455 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200456
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400457 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200458
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300459 if (weston_surface_is_mapped(surface))
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +0200460 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200461}
462
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200463WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100464weston_surface_to_global_fixed(struct weston_surface *surface,
465 wl_fixed_t sx, wl_fixed_t sy,
466 wl_fixed_t *x, wl_fixed_t *y)
467{
468 GLfloat xf, yf;
469
470 weston_surface_to_global_float(surface,
471 wl_fixed_to_double(sx),
472 wl_fixed_to_double(sy),
473 &xf, &yf);
474 *x = wl_fixed_from_double(xf);
475 *y = wl_fixed_from_double(yf);
476}
477
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400478WL_EXPORT void
479weston_surface_from_global_float(struct weston_surface *surface,
480 GLfloat x, GLfloat y, GLfloat *sx, GLfloat *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200481{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200482 if (surface->transform.enabled) {
483 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
484
485 weston_matrix_transform(&surface->transform.inverse, &v);
486
487 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200488 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200489 "weston_surface_from_global(), divisor = %g\n",
490 v.f[3]);
491 *sx = 0;
492 *sy = 0;
493 return;
494 }
495
Pekka Paalanencd403622012-01-25 13:37:39 +0200496 *sx = v.f[0] / v.f[3];
497 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200498 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200499 *sx = x - surface->geometry.x;
500 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200501 }
502}
503
504WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100505weston_surface_from_global_fixed(struct weston_surface *surface,
506 wl_fixed_t x, wl_fixed_t y,
507 wl_fixed_t *sx, wl_fixed_t *sy)
508{
509 GLfloat sxf, syf;
510
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400511 weston_surface_from_global_float(surface,
512 wl_fixed_to_double(x),
513 wl_fixed_to_double(y),
514 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100515 *sx = wl_fixed_from_double(sxf);
516 *sy = wl_fixed_from_double(syf);
517}
518
519WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200520weston_surface_from_global(struct weston_surface *surface,
521 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
522{
523 GLfloat sxf, syf;
524
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400525 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200526 *sx = floorf(sxf);
527 *sy = floorf(syf);
528}
529
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400530WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400531weston_surface_schedule_repaint(struct weston_surface *surface)
532{
533 struct weston_output *output;
534
535 wl_list_for_each(output, &surface->compositor->output_list, link)
536 if (surface->output_mask & (1 << output->id))
537 weston_output_schedule_repaint(output);
538}
539
540WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500541weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500542{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400543 pixman_region32_union_rect(&surface->damage, &surface->damage,
544 0, 0, surface->geometry.width,
545 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200546
Kristian Høgsberg98238702012-08-03 16:29:12 -0400547 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500548}
549
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400550WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500551weston_surface_configure(struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200552 GLfloat x, GLfloat y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400553{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200554 surface->geometry.x = x;
555 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200556 surface->geometry.width = width;
557 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200558 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400559}
560
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200561WL_EXPORT void
562weston_surface_set_position(struct weston_surface *surface,
563 GLfloat x, GLfloat y)
564{
565 surface->geometry.x = x;
566 surface->geometry.y = y;
567 surface->geometry.dirty = 1;
568}
569
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300570WL_EXPORT int
571weston_surface_is_mapped(struct weston_surface *surface)
572{
573 if (surface->output)
574 return 1;
575 else
576 return 0;
577}
578
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400579WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500580weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500581{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400582 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500583
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400584 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500585
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400586 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500587}
588
Tiago Vignatti9d393522012-02-10 16:26:19 +0200589static struct weston_surface *
590weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100591 wl_fixed_t x, wl_fixed_t y,
592 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200593{
594 struct weston_surface *surface;
595
596 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100597 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500598 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100599 wl_fixed_to_int(*sx),
600 wl_fixed_to_int(*sy),
601 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200602 return surface;
603 }
604
605 return NULL;
606}
607
608static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400609weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500610{
Scott Moreau447013d2012-02-18 05:05:29 -0700611 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500612 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400613 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500614
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400615 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100616 return;
617
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400618 surface = weston_compositor_pick_surface(seat->compositor,
619 pointer->x,
620 pointer->y,
621 &pointer->current_x,
622 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500623
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400624 if (&surface->surface != pointer->current) {
625 interface = pointer->grab->interface;
626 pointer->current = &surface->surface;
627 interface->focus(pointer->grab, &surface->surface,
628 pointer->current_x,
629 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500630 }
631
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400632 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500633 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100634 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400635 pointer->x,
636 pointer->y,
637 &pointer->grab->x,
638 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500639}
640
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400641static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500642weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400643{
Daniel Stone37816df2012-05-16 18:45:18 +0100644 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400645
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500646 if (!compositor->focus)
647 return;
648
Daniel Stone37816df2012-05-16 18:45:18 +0100649 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400650 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400651}
652
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400653WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500654weston_surface_unmap(struct weston_surface *surface)
655{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100656 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500657
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500658 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500659 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500660 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500661
Daniel Stone4dab5db2012-05-30 16:31:53 +0100662 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100663 if (seat->seat.keyboard &&
664 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100665 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100666 if (seat->seat.pointer &&
667 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100668 wl_pointer_set_focus(seat->seat.pointer,
669 NULL,
670 wl_fixed_from_int(0),
671 wl_fixed_from_int(0));
672 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500673
Kristian Høgsberg98238702012-08-03 16:29:12 -0400674 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500675}
676
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400677struct weston_frame_callback {
678 struct wl_resource resource;
679 struct wl_list link;
680};
681
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500682static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400683destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500684{
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200685 int i;
686
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500687 struct weston_surface *surface =
688 container_of(resource,
689 struct weston_surface, surface.resource);
690 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400691 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400692
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300693 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500694 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400695
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200696 glDeleteTextures(surface->num_textures, surface->textures);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200697
Benjamin Franzke06286262011-05-06 19:12:33 +0200698 if (surface->buffer)
699 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400700
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200701 for (i = 0; i < surface->num_images; i++)
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400702 compositor->destroy_image(compositor->egl_display,
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200703 surface->images[i]);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200704
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200705 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200706 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500707 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500708 pixman_region32_fini(&surface->clip);
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500709 if (!region_is_undefined(&surface->input))
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500710 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200711
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400712 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
713 wl_resource_destroy(&cb->resource);
714
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400715 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500716}
717
Alex Wu8811bf92012-02-28 18:07:54 +0800718WL_EXPORT void
719weston_surface_destroy(struct weston_surface *surface)
720{
721 /* Not a valid way to destroy a client surface */
722 assert(surface->surface.resource.client == NULL);
723
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400724 wl_signal_emit(&surface->surface.resource.destroy_signal,
725 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800726 destroy_surface(&surface->surface.resource);
727}
728
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100729static void
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200730ensure_textures(struct weston_surface *es, int num_textures)
731{
732 int i;
733
734 if (num_textures <= es->num_textures)
735 return;
736
737 for (i = es->num_textures; i < num_textures; i++) {
738 glGenTextures(1, &es->textures[i]);
Rob Clarke3b95912012-08-31 16:42:18 -0500739 glBindTexture(es->target, es->textures[i]);
740 glTexParameteri(es->target,
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200741 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
Rob Clarke3b95912012-08-31 16:42:18 -0500742 glTexParameteri(es->target,
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200743 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
744 }
745 es->num_textures = num_textures;
Rob Clarke3b95912012-08-31 16:42:18 -0500746 glBindTexture(es->target, 0);
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200747}
748
749static void
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -0400750weston_surface_attach(struct wl_surface *surface, struct wl_buffer *buffer)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100751{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500752 struct weston_surface *es = (struct weston_surface *) surface;
753 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400754 EGLint attribs[3], format;
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200755 int i, num_planes;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100756
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300757 if (es->buffer) {
758 weston_buffer_post_release(es->buffer);
759 wl_list_remove(&es->buffer_destroy_listener.link);
760 }
761
762 es->buffer = buffer;
763
764 if (!buffer) {
765 if (weston_surface_is_mapped(es))
766 weston_surface_unmap(es);
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200767 for (i = 0; i < es->num_images; i++) {
768 ec->destroy_image(ec->egl_display, es->images[i]);
769 es->images[i] = NULL;
Kristian Høgsbergb8ceaaa2012-06-19 15:41:12 -0400770 }
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200771 es->num_images = 0;
772 glDeleteTextures(es->num_textures, es->textures);
773 es->num_textures = 0;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300774 return;
775 }
776
777 buffer->busy_count++;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400778 wl_signal_add(&es->buffer->resource.destroy_signal,
779 &es->buffer_destroy_listener);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300780
781 if (es->geometry.width != buffer->width ||
782 es->geometry.height != buffer->height) {
783 undef_region(&es->input);
784 pixman_region32_fini(&es->opaque);
785 pixman_region32_init(&es->opaque);
786 }
787
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100788 if (wl_buffer_is_shm(buffer)) {
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200789 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
Rob Clarke3b95912012-08-31 16:42:18 -0500790 es->target = GL_TEXTURE_2D;
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200791
792 ensure_textures(es, 1);
793 glBindTexture(GL_TEXTURE_2D, es->textures[0]);
Kristian Høgsbergfb6de222012-03-27 17:10:13 -0400794 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
795 es->pitch, es->buffer->height, 0,
796 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
Kristian Høgsbergd6ad1222012-05-17 11:11:15 -0400797 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
Pekka Paalanen6b5585b2012-08-30 16:47:19 -0500798 es->shader = &ec->texture_shader_rgbx;
Kristian Høgsbergd6ad1222012-05-17 11:11:15 -0400799 else
Pekka Paalanen6b5585b2012-08-30 16:47:19 -0500800 es->shader = &ec->texture_shader_rgba;
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200801 } else if (ec->query_buffer(ec->egl_display, buffer,
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400802 EGL_TEXTURE_FORMAT, &format)) {
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200803 for (i = 0; i < es->num_images; i++)
Kristian Høgsberg971cbc82012-07-17 14:21:25 -0400804 ec->destroy_image(ec->egl_display, es->images[i]);
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200805 es->num_images = 0;
Rob Clarke3b95912012-08-31 16:42:18 -0500806 es->target = GL_TEXTURE_2D;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400807 switch (format) {
808 case EGL_TEXTURE_RGB:
809 case EGL_TEXTURE_RGBA:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200810 default:
811 num_planes = 1;
812 es->shader = &ec->texture_shader_rgba;
813 break;
Rob Clarke3b95912012-08-31 16:42:18 -0500814 case EGL_TEXTURE_EXTERNAL_WL:
815 num_planes = 1;
816 es->target = GL_TEXTURE_EXTERNAL_OES;
817 es->shader = &ec->texture_shader_egl_external;
818 break;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400819 case EGL_TEXTURE_Y_UV_WL:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200820 num_planes = 2;
821 es->shader = &ec->texture_shader_y_uv;
822 break;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400823 case EGL_TEXTURE_Y_U_V_WL:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200824 num_planes = 3;
825 es->shader = &ec->texture_shader_y_u_v;
826 break;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400827 case EGL_TEXTURE_Y_XUXV_WL:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200828 num_planes = 2;
829 es->shader = &ec->texture_shader_y_xuxv;
830 break;
831 }
832
833 ensure_textures(es, num_planes);
834 for (i = 0; i < num_planes; i++) {
835 attribs[0] = EGL_WAYLAND_PLANE_WL;
836 attribs[1] = i;
837 attribs[2] = EGL_NONE;
838 es->images[i] = ec->create_image(ec->egl_display,
839 NULL,
840 EGL_WAYLAND_BUFFER_WL,
841 buffer, attribs);
Rob Clark48cd58b2012-08-13 17:39:17 -0500842 if (!es->images[i]) {
843 weston_log("failed to create img for plane %d\n", i);
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200844 continue;
Rob Clark48cd58b2012-08-13 17:39:17 -0500845 }
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200846 es->num_images++;
847
848 glActiveTexture(GL_TEXTURE0 + i);
Rob Clarke3b95912012-08-31 16:42:18 -0500849 glBindTexture(es->target, es->textures[i]);
850 ec->image_target_texture_2d(es->target,
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200851 es->images[i]);
852 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400853
Ander Conselvan de Oliveira9390ae32012-03-27 17:36:38 +0300854 es->pitch = buffer->width;
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200855 } else {
Rob Clark48cd58b2012-08-13 17:39:17 -0500856 weston_log("unhandled buffer type!\n");
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100857 }
858}
859
Rob Clark0e5a2d02012-08-30 16:47:18 -0500860
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500861WL_EXPORT void
862weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400863{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500864 wl_list_remove(&surface->layer_link);
865 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500866 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500867 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400868}
869
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400870WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500871weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400872{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500873 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400874
875 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500876 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400877}
878
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500879WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500880weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +0200881{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400882 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200883 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200884
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400885 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -0500886 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200887}
888
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400889WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500890weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400891{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500892 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400893
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400894 pixman_region32_union(&compositor->primary_plane.damage,
895 &compositor->primary_plane.damage,
896 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400897 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400898}
899
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400900static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500901fade_frame(struct weston_animation *animation,
902 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400903{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500904 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400905 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500906 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500907 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400908
Scott Moreau94b0b0c2012-06-14 01:01:56 -0600909 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -0600910 compositor->fade.spring.timestamp = msecs;
911
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500912 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500913 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500914 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
915 compositor->fade.spring.current);
916 weston_surface_damage(surface);
917
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500918 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400919 compositor->fade.spring.current =
920 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400921 wl_list_remove(&animation->link);
922 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200923
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500924 if (compositor->fade.spring.current < 0.001) {
925 destroy_surface(&surface->surface.resource);
926 compositor->fade.surface = NULL;
927 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500928 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400929 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200930 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400931 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400932}
933
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400934static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400935surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400936 pixman_region32_t *opaque)
937{
938 if (surface->buffer && wl_buffer_is_shm(surface->buffer))
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400939 gles2_renderer_flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400940
941 if (surface->transform.enabled) {
942 pixman_box32_t *extents;
943
944 extents = pixman_region32_extents(&surface->damage);
945 surface_compute_bbox(surface, extents->x1, extents->y1,
946 extents->x2 - extents->x1,
947 extents->y2 - extents->y1,
948 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400949 pixman_region32_translate(&surface->damage,
950 -surface->plane->x,
951 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400952 } else {
953 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400954 surface->geometry.x - surface->plane->x,
955 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400956 }
957
958 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400959 pixman_region32_union(&surface->plane->damage,
960 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400961 empty_region(&surface->damage);
962 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400963 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400964}
965
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400966static void
Rob Bradford0fb79752012-08-02 15:36:57 +0100967weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400968{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500969 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500970 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500971 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -0500972 struct weston_animation *animation, *next;
973 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +0200974 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400975 pixman_region32_t opaque, output_damage, new_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500976
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +0200977 weston_compositor_update_drag_surfaces(ec);
978
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500979 /* Rebuild the surface list and update surface transforms up front. */
980 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +0200981 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500982 wl_list_for_each(layer, &ec->layer_list, link) {
983 wl_list_for_each(es, &layer->surface_list, layer_link) {
984 weston_surface_update_transform(es);
985 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +0200986 if (es->output == output) {
987 wl_list_insert_list(&frame_callback_list,
988 &es->frame_callback_list);
989 wl_list_init(&es->frame_callback_list);
990 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500991 }
992 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +0200993
Kristian Høgsberg79af73e2012-08-03 15:45:23 -0400994 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800995 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -0400996 else
997 wl_list_for_each(es, &ec->surface_list, link)
998 weston_surface_move_to_plane(es, &ec->primary_plane);
999
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001000 pixman_region32_init(&opaque);
1001
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001002 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001003 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001004
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -04001005 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001006
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001007 pixman_region32_init(&output_damage);
1008
1009 pixman_region32_init(&new_damage);
1010 pixman_region32_copy(&new_damage, &ec->primary_plane.damage);
1011
1012 pixman_region32_union(&ec->primary_plane.damage,
1013 &ec->primary_plane.damage,
1014 &output->previous_damage);
1015
1016 pixman_region32_intersect(&output->previous_damage,
1017 &new_damage, &output->region);
1018
1019 pixman_region32_intersect(&output_damage,
1020 &ec->primary_plane.damage, &output->region);
1021
1022 pixman_region32_fini(&new_damage);
1023
Scott Moreauccbf29d2012-02-22 14:21:41 -07001024 if (output->dirty)
1025 weston_output_update_matrix(output);
1026
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001027 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001028
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001029 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001030
Kristian Høgsbergef044142011-06-21 15:02:12 -04001031 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001032
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001033 weston_compositor_repick(ec);
1034 wl_event_loop_dispatch(ec->input_loop, 0);
1035
Jonas Ådahldb773762012-06-13 00:01:21 +02001036 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001037 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001038 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001039 }
Jonas Ådahldb773762012-06-13 00:01:21 +02001040 wl_list_init(&frame_callback_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001041
Scott Moreaud64cf212012-06-08 19:40:54 -06001042 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001043 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001044 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001045 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001046}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001047
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001048static int
1049weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001050{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001051 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001052
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001053 wl_event_loop_dispatch(compositor->input_loop, 0);
1054
1055 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001056}
1057
1058WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001059weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001060{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001061 struct weston_compositor *compositor = output->compositor;
1062 struct wl_event_loop *loop =
1063 wl_display_get_event_loop(compositor->wl_display);
1064 int fd;
1065
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001066 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001067 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001068 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001069 return;
1070 }
1071
1072 output->repaint_scheduled = 0;
1073 if (compositor->input_loop_source)
1074 return;
1075
1076 fd = wl_event_loop_get_fd(compositor->input_loop);
1077 compositor->input_loop_source =
1078 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1079 weston_compositor_read_input, compositor);
1080}
1081
1082static void
1083idle_repaint(void *data)
1084{
1085 struct weston_output *output = data;
1086
1087 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001088}
1089
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001090WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001091weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1092{
1093 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001094 if (below != NULL)
1095 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001096}
1097
1098WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001099weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001100{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001101 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001102 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001103
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001104 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001105 return;
1106
Kristian Høgsbergef044142011-06-21 15:02:12 -04001107 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001108 output->repaint_needed = 1;
1109 if (output->repaint_scheduled)
1110 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001111
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001112 wl_event_loop_add_idle(loop, idle_repaint, output);
1113 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001114
1115 if (compositor->input_loop_source) {
1116 wl_event_source_remove(compositor->input_loop_source);
1117 compositor->input_loop_source = NULL;
1118 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001119}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001120
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001121WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001122weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1123{
1124 struct weston_output *output;
1125
1126 wl_list_for_each(output, &compositor->output_list, link)
1127 weston_output_schedule_repaint(output);
1128}
1129
1130WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001131weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001132{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001133 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001134 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001135
Scott Moreau9d1b1122012-06-08 19:40:53 -06001136 output = container_of(compositor->output_list.next,
1137 struct weston_output, link);
1138
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001139 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001140 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001141 return;
1142
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001143 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001144 surface = weston_surface_create(compositor);
1145 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001146 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001147 wl_list_insert(&compositor->fade_layer.surface_list,
1148 &surface->layer_link);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001149 weston_surface_assign_output(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001150 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001151 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001152 }
1153
1154 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001155 if (wl_list_empty(&compositor->fade.animation.link)) {
1156 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001157 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001158 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001159 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001160}
1161
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001162static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001163surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001164{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001165 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001166}
1167
Casey Dahlin96d8a752012-04-19 22:50:07 -04001168static struct wl_resource *
1169find_resource_for_client(struct wl_list *list, struct wl_client *client)
1170{
1171 struct wl_resource *r;
1172
1173 wl_list_for_each(r, list, link) {
1174 if (r->client == client)
1175 return r;
1176 }
1177
1178 return NULL;
1179}
1180
Casey Dahlin9074db52012-04-19 22:50:09 -04001181static void
1182weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1183{
1184 uint32_t different = es->output_mask ^ mask;
1185 uint32_t entered = mask & different;
1186 uint32_t left = es->output_mask & different;
1187 struct weston_output *output;
Rob Bradford8667e772012-05-18 14:13:03 +01001188 struct wl_resource *resource = NULL;
Casey Dahlin9074db52012-04-19 22:50:09 -04001189 struct wl_client *client = es->surface.resource.client;
1190
Scott Moreaua5021522012-08-03 17:11:51 -06001191 es->output_mask = mask;
Casey Dahlin9074db52012-04-19 22:50:09 -04001192 if (es->surface.resource.client == NULL)
1193 return;
1194 if (different == 0)
1195 return;
1196
Casey Dahlin9074db52012-04-19 22:50:09 -04001197 wl_list_for_each(output, &es->compositor->output_list, link) {
1198 if (1 << output->id & different)
1199 resource =
1200 find_resource_for_client(&output->resource_list,
1201 client);
Kristian Høgsbergc7814d22012-07-12 12:34:43 -04001202 if (resource == NULL)
1203 continue;
Casey Dahlin9074db52012-04-19 22:50:09 -04001204 if (1 << output->id & entered)
1205 wl_surface_send_enter(&es->surface.resource, resource);
1206 if (1 << output->id & left)
1207 wl_surface_send_leave(&es->surface.resource, resource);
1208 }
1209}
1210
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001211WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001212weston_surface_assign_output(struct weston_surface *es)
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001213{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001214 struct weston_compositor *ec = es->compositor;
1215 struct weston_output *output, *new_output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001216 pixman_region32_t region;
Casey Dahlin9074db52012-04-19 22:50:09 -04001217 uint32_t max, area, mask;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001218 pixman_box32_t *e;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001219
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001220 new_output = NULL;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001221 max = 0;
Casey Dahlin9074db52012-04-19 22:50:09 -04001222 mask = 0;
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001223 pixman_region32_init(&region);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001224 wl_list_for_each(output, &ec->output_list, link) {
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001225 pixman_region32_intersect(&region, &es->transform.boundingbox,
1226 &output->region);
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001227
1228 e = pixman_region32_extents(&region);
1229 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1230
Casey Dahlin9074db52012-04-19 22:50:09 -04001231 if (area > 0)
1232 mask |= 1 << output->id;
1233
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001234 if (area >= max) {
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001235 new_output = output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001236 max = area;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001237 }
1238 }
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001239 pixman_region32_fini(&region);
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001240
1241 es->output = new_output;
Casey Dahlin9074db52012-04-19 22:50:09 -04001242 weston_surface_update_output_mask(es, mask);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001243}
1244
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001245static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001246surface_attach(struct wl_client *client,
1247 struct wl_resource *resource,
1248 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1249{
1250 struct weston_surface *es = resource->data;
1251 struct wl_buffer *buffer = NULL;
1252
1253 if (buffer_resource)
1254 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001255
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -04001256 weston_surface_attach(&es->surface, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001257
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001258 if (buffer && es->configure)
1259 es->configure(es, sx, sy);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001260}
1261
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001262static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001263surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001264 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001265 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001266{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001267 struct weston_surface *es = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001268
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001269 pixman_region32_union_rect(&es->damage, &es->damage,
1270 x, y, width, height);
Kristian Høgsberg98238702012-08-03 16:29:12 -04001271 weston_surface_schedule_repaint(es);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001272}
1273
Kristian Høgsberg33418202011-08-16 23:01:28 -04001274static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001275destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001276{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001277 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001278
1279 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001280 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001281}
1282
1283static void
1284surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001285 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001286{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001287 struct weston_frame_callback *cb;
1288 struct weston_surface *es = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001289
1290 cb = malloc(sizeof *cb);
1291 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001292 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001293 return;
1294 }
1295
1296 cb->resource.object.interface = &wl_callback_interface;
1297 cb->resource.object.id = callback;
1298 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001299 cb->resource.client = client;
1300 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001301
1302 wl_client_add_resource(client, &cb->resource);
Jonas Ådahldb773762012-06-13 00:01:21 +02001303 wl_list_insert(es->frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001304}
1305
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001306static void
1307surface_set_opaque_region(struct wl_client *client,
1308 struct wl_resource *resource,
1309 struct wl_resource *region_resource)
1310{
1311 struct weston_surface *surface = resource->data;
1312 struct weston_region *region;
1313
1314 pixman_region32_fini(&surface->opaque);
1315
1316 if (region_resource) {
1317 region = region_resource->data;
1318 pixman_region32_init_rect(&surface->opaque, 0, 0,
1319 surface->geometry.width,
1320 surface->geometry.height);
1321 pixman_region32_intersect(&surface->opaque,
1322 &surface->opaque, &region->region);
1323 } else {
1324 pixman_region32_init(&surface->opaque);
1325 }
1326
1327 surface->geometry.dirty = 1;
1328}
1329
1330static void
1331surface_set_input_region(struct wl_client *client,
1332 struct wl_resource *resource,
1333 struct wl_resource *region_resource)
1334{
1335 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001336 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001337
1338 if (region_resource) {
1339 region = region_resource->data;
1340 pixman_region32_init_rect(&surface->input, 0, 0,
1341 surface->geometry.width,
1342 surface->geometry.height);
1343 pixman_region32_intersect(&surface->input,
1344 &surface->input, &region->region);
1345 } else {
1346 pixman_region32_init_rect(&surface->input, 0, 0,
1347 surface->geometry.width,
1348 surface->geometry.height);
1349 }
1350
Kristian Høgsberg98238702012-08-03 16:29:12 -04001351 weston_surface_schedule_repaint(surface);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001352}
1353
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001354static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001355 surface_destroy,
1356 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001357 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001358 surface_frame,
1359 surface_set_opaque_region,
1360 surface_set_input_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001361};
1362
1363static void
1364compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001365 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001366{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001367 struct weston_compositor *ec = resource->data;
1368 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001369
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001370 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001371 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001372 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001373 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001374 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001375
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001376 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001377
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001378 surface->surface.resource.object.id = id;
1379 surface->surface.resource.object.interface = &wl_surface_interface;
1380 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001381 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001382 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001383
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001384 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001385}
1386
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001387static void
1388destroy_region(struct wl_resource *resource)
1389{
1390 struct weston_region *region =
1391 container_of(resource, struct weston_region, resource);
1392
1393 pixman_region32_fini(&region->region);
1394 free(region);
1395}
1396
1397static void
1398region_destroy(struct wl_client *client, struct wl_resource *resource)
1399{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001400 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001401}
1402
1403static void
1404region_add(struct wl_client *client, struct wl_resource *resource,
1405 int32_t x, int32_t y, int32_t width, int32_t height)
1406{
1407 struct weston_region *region = resource->data;
1408
1409 pixman_region32_union_rect(&region->region, &region->region,
1410 x, y, width, height);
1411}
1412
1413static void
1414region_subtract(struct wl_client *client, struct wl_resource *resource,
1415 int32_t x, int32_t y, int32_t width, int32_t height)
1416{
1417 struct weston_region *region = resource->data;
1418 pixman_region32_t rect;
1419
1420 pixman_region32_init_rect(&rect, x, y, width, height);
1421 pixman_region32_subtract(&region->region, &region->region, &rect);
1422 pixman_region32_fini(&rect);
1423}
1424
1425static const struct wl_region_interface region_interface = {
1426 region_destroy,
1427 region_add,
1428 region_subtract
1429};
1430
1431static void
1432compositor_create_region(struct wl_client *client,
1433 struct wl_resource *resource, uint32_t id)
1434{
1435 struct weston_region *region;
1436
1437 region = malloc(sizeof *region);
1438 if (region == NULL) {
1439 wl_resource_post_no_memory(resource);
1440 return;
1441 }
1442
1443 region->resource.destroy = destroy_region;
1444
1445 region->resource.object.id = id;
1446 region->resource.object.interface = &wl_region_interface;
1447 region->resource.object.implementation =
1448 (void (**)(void)) &region_interface;
1449 region->resource.data = region;
1450
1451 pixman_region32_init(&region->region);
1452
1453 wl_client_add_resource(client, &region->resource);
1454}
1455
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001456static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001457 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001458 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001459};
1460
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001461WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001462weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001463{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001464 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1465 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001466
1467 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001468 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001469}
1470
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001471static void
1472weston_compositor_dpms_on(struct weston_compositor *compositor)
1473{
1474 struct weston_output *output;
1475
1476 wl_list_for_each(output, &compositor->output_list, link)
1477 if (output->set_dpms)
1478 output->set_dpms(output, WESTON_DPMS_ON);
1479}
1480
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001481WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001482weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001483{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001484 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1485 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001486 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001487 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001488 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001489 }
1490}
1491
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001492static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001493weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001494{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001495 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001496 compositor->idle_inhibit++;
1497}
1498
1499static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001500weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001501{
1502 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001503 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001504}
1505
1506static int
1507idle_handler(void *data)
1508{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001509 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001510
1511 if (compositor->idle_inhibit)
1512 return 1;
1513
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001514 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001515
1516 return 1;
1517}
1518
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001519WL_EXPORT void
1520weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1521{
1522 pixman_region32_init(&plane->damage);
1523 plane->x = x;
1524 plane->y = y;
1525}
1526
1527WL_EXPORT void
1528weston_plane_release(struct weston_plane *plane)
1529{
1530 pixman_region32_fini(&plane->damage);
1531}
1532
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001533static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001534weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001535
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001536static void
Daniel Stone37816df2012-05-16 18:45:18 +01001537clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001538{
Daniel Stone37816df2012-05-16 18:45:18 +01001539 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001540 struct weston_output *output, *prev = NULL;
1541 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001542
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001543 x = wl_fixed_to_int(*fx);
1544 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001545 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1546 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001547
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001548 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001549 if (pixman_region32_contains_point(&output->region,
1550 x, y, NULL))
1551 valid = 1;
1552 if (pixman_region32_contains_point(&output->region,
1553 old_x, old_y, NULL))
1554 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001555 }
Daniel Stone37816df2012-05-16 18:45:18 +01001556
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001557 if (!valid) {
1558 if (x < prev->x)
1559 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001560 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001561 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001562 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001563 if (y < prev->y)
1564 *fy = wl_fixed_from_int(prev->y);
1565 else if (y >= prev->y + prev->current->height)
1566 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001567 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001568 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001569}
1570
1571WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001572notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001573{
1574 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001575 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001576 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001577 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001578 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001579
1580 weston_compositor_activity(ec);
1581
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001582 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001583
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001584 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001585
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001586 pointer->x = x;
1587 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001588
1589 ix = wl_fixed_to_int(x);
1590 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001591
Scott Moreauccbf29d2012-02-22 14:21:41 -07001592 wl_list_for_each(output, &ec->output_list, link)
1593 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001594 pixman_region32_contains_point(&output->region,
1595 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06001596 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001597
Daniel Stone37816df2012-05-16 18:45:18 +01001598 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001599 interface = pointer->grab->interface;
1600 interface->motion(pointer->grab, time,
1601 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001602
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001603 if (seat->sprite) {
1604 weston_surface_set_position(seat->sprite,
1605 ix - seat->hotspot_x,
1606 iy - seat->hotspot_y);
1607 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001608 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001609}
1610
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001611WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001612weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001613 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001614{
Daniel Stone37816df2012-05-16 18:45:18 +01001615 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001616
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001617 if (seat->seat.keyboard) {
1618 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1619 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04001620 }
1621
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001622 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001623}
1624
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001625WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001626notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001627 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001628{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001629 struct weston_compositor *compositor = seat->compositor;
1630 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001631 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001632 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001633 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001634
Daniel Stone4dbadb12012-05-30 16:31:51 +01001635 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001636 if (compositor->ping_handler && focus)
1637 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001638 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001639 if (pointer->button_count == 0) {
1640 pointer->grab_button = button;
1641 pointer->grab_time = time;
1642 pointer->grab_x = pointer->x;
1643 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001644 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001645 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001646 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001647 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001648 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001649 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001650
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001651 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001652 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001653
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001654 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001655
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001656 if (pointer->button_count == 1)
1657 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001658 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001659}
1660
Scott Moreau210d0792012-03-22 10:47:01 -06001661WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001662notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01001663 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001664{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001665 struct weston_compositor *compositor = seat->compositor;
1666 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001667 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001668 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001669 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1670
1671 if (compositor->ping_handler && focus)
1672 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001673
1674 weston_compositor_activity(compositor);
1675
Scott Moreau6a3633d2012-03-20 08:47:59 -06001676 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001677 weston_compositor_run_axis_binding(compositor, seat,
1678 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06001679 else
1680 return;
1681
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001682 if (pointer->focus_resource)
1683 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01001684 value);
Scott Moreau210d0792012-03-22 10:47:01 -06001685}
1686
Daniel Stone05d58682012-06-22 13:21:38 +01001687WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001688notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001689{
Daniel Stone05d58682012-06-22 13:21:38 +01001690 struct wl_keyboard *keyboard = &seat->keyboard;
1691 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01001692 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001693 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01001694 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01001695 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01001696
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001697 /* Serialize and update our internal state, checking to see if it's
1698 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01001699 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1700 XKB_STATE_DEPRESSED);
1701 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1702 XKB_STATE_LATCHED);
1703 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1704 XKB_STATE_LOCKED);
1705 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01001706 XKB_STATE_EFFECTIVE);
1707
Daniel Stone71c38772012-06-22 13:21:30 +01001708 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
1709 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
1710 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
1711 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01001712 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01001713
Daniel Stone71c38772012-06-22 13:21:30 +01001714 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
1715 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
1716 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
1717 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001718
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001719 /* And update the modifier_state for bindings. */
1720 mods_lookup = mods_depressed | mods_latched;
1721 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01001722 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001723 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01001724 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001725 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01001726 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001727 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04001728 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
1729 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01001730
Daniel Stone8c8164f2012-05-30 16:31:45 +01001731 /* Finally, notify the compositor that LEDs have changed. */
1732 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001733 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001734 leds |= LED_NUM_LOCK;
1735 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001736 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001737 leds |= LED_CAPS_LOCK;
1738 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001739 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001740 leds |= LED_SCROLL_LOCK;
1741 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01001742 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01001743 seat->xkb_state.leds = leds;
1744
Daniel Stone05d58682012-06-22 13:21:38 +01001745 if (changed) {
1746 grab->interface->modifiers(grab,
1747 serial,
1748 keyboard->modifiers.mods_depressed,
1749 keyboard->modifiers.mods_latched,
1750 keyboard->modifiers.mods_locked,
1751 keyboard->modifiers.group);
1752 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001753}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001754
Daniel Stone05d58682012-06-22 13:21:38 +01001755static void
1756update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001757 enum wl_keyboard_key_state state)
1758{
1759 enum xkb_key_direction direction;
1760
1761 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1762 direction = XKB_KEY_DOWN;
1763 else
1764 direction = XKB_KEY_UP;
1765
1766 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1767 * broken keycode system, which starts at 8. */
1768 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1769
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001770 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001771}
1772
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001773WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001774notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001775 enum wl_keyboard_key_state state,
1776 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001777{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001778 struct weston_compositor *compositor = seat->compositor;
1779 struct wl_keyboard *keyboard = seat->seat.keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01001780 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001781 (struct weston_surface *) keyboard->focus;
1782 struct wl_keyboard_grab *grab = keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001783 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001784 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001785
Daniel Stonec9785ea2012-05-30 16:31:52 +01001786 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001787 if (compositor->ping_handler && focus)
1788 compositor->ping_handler(focus, serial);
1789
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001790 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001791 keyboard->grab_key = key;
1792 keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07001793 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001794 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001795 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001796
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001797 end = keyboard->keys.data + keyboard->keys.size;
1798 for (k = keyboard->keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01001799 if (*k == key) {
1800 /* Ignore server-generated repeats. */
1801 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1802 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001803 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01001804 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001805 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001806 keyboard->keys.size = (void *) end - keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001807 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001808 k = wl_array_add(&keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001809 *k = key;
1810 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001811
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001812 if (grab == &keyboard->default_grab) {
1813 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001814 state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001815 grab = keyboard->grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06001816 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001817
Daniel Stone7ace3902012-05-30 16:31:40 +01001818 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01001819
1820 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001821 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01001822 wl_display_get_serial(compositor->wl_display),
1823 key,
1824 state);
1825 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001826}
1827
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001828WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001829notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01001830 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001831{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001832 struct weston_compositor *compositor = seat->compositor;
1833 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001834
1835 if (output) {
Daniel Stone37816df2012-05-16 18:45:18 +01001836 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001837 x - pointer->x,
1838 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001839
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001840 pointer->x = x;
1841 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001842 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001843 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001844 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001845 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03001846 /* FIXME: We should call wl_pointer_set_focus(seat,
1847 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001848 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001849}
1850
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001851static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001852destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001853{
Daniel Stone37816df2012-05-16 18:45:18 +01001854 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001855
Daniel Stone37816df2012-05-16 18:45:18 +01001856 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001857 saved_kbd_focus_listener);
1858
Daniel Stone37816df2012-05-16 18:45:18 +01001859 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001860}
1861
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001862WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001863notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001864 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001865{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001866 struct weston_compositor *compositor = seat->compositor;
1867 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001868 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001869 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001870
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001871 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001872 wl_array_copy(&keyboard->keys, keys);
1873 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001874 weston_compositor_idle_inhibit(compositor);
1875 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001876 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001877 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001878 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001879
Daniel Stoneef172672012-06-22 13:21:32 +01001880 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001881 wl_array_for_each(k, &keyboard->keys) {
1882 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01001883 WL_KEYBOARD_KEY_STATE_PRESSED);
1884 }
1885
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001886 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001887
1888 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001889 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1890 wl_keyboard_set_focus(keyboard, surface);
1891 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001892 }
1893}
1894
1895WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001896notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01001897{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001898 struct weston_compositor *compositor = seat->compositor;
1899 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001900 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001901
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001902 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001903 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001904 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001905 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001906 WL_KEYBOARD_KEY_STATE_RELEASED);
1907 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001908
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001909 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001910
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001911 if (keyboard->focus) {
1912 seat->saved_kbd_focus = keyboard->focus;
1913 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01001914 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001915 wl_signal_add(&keyboard->focus->resource.destroy_signal,
1916 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001917 }
1918
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001919 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001920 /* FIXME: We really need keyboard grab cancel here to
1921 * let the grab shut down properly. As it is we leak
1922 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001923 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001924}
1925
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001926static void
Daniel Stone37816df2012-05-16 18:45:18 +01001927touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001928{
Daniel Stone37816df2012-05-16 18:45:18 +01001929 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001930 struct wl_resource *resource;
1931
Pekka Paalanen56464252012-07-31 13:21:08 +03001932 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001933 return;
1934
Pekka Paalanen56464252012-07-31 13:21:08 +03001935 if (seat->touch->focus_resource)
1936 wl_list_remove(&seat->touch->focus_listener.link);
1937 seat->touch->focus = NULL;
1938 seat->touch->focus_resource = NULL;
1939
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001940 if (surface) {
1941 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01001942 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04001943 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001944 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02001945 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001946 return;
1947 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001948
Daniel Stone37816df2012-05-16 18:45:18 +01001949 seat->touch->focus = surface;
1950 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03001951 wl_signal_add(&resource->destroy_signal,
1952 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001953 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001954}
1955
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001956/**
1957 * notify_touch - emulates button touches and notifies surfaces accordingly.
1958 *
1959 * It assumes always the correct cycle sequence until it gets here: touch_down
1960 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1961 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001962 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001963 */
1964WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001965notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001966 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001967{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001968 struct weston_compositor *ec = seat->compositor;
1969 struct wl_touch *touch = seat->seat.touch;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001970 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001971 wl_fixed_t sx, sy;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001972 uint32_t serial = 0;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001973
1974 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01001975 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001976 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001977
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001978 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001979
1980 /* the first finger down picks the surface, and all further go
1981 * to that surface for the remainder of the touch session i.e.
1982 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001983 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001984 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001985 touch_set_focus(seat, &es->surface);
1986 } else if (touch->focus) {
1987 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001988 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001989 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001990
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001991 if (touch->focus_resource && touch->focus)
1992 wl_touch_send_down(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001993 serial, time,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001994 &touch->focus->resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001995 touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001996 break;
Daniel Stone37816df2012-05-16 18:45:18 +01001997 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001998 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001999 if (!es)
2000 break;
2001
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002002 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002003 if (touch->focus_resource)
2004 wl_touch_send_motion(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002005 time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002006 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002007 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002008 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002009 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002010
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002011 if (touch->focus_resource)
2012 wl_touch_send_up(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002013 serial, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002014 if (seat->num_tp == 0)
2015 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002016 break;
2017 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002018}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002019
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04002020static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002021pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
2022{
2023 struct weston_seat *seat = container_of(listener, struct weston_seat,
2024 sprite_destroy_listener);
2025
2026 seat->sprite = NULL;
2027}
2028
2029static void
2030pointer_cursor_surface_configure(struct weston_surface *es,
2031 int32_t dx, int32_t dy)
2032{
2033 struct weston_seat *seat = es->private;
2034 int x, y;
2035
2036 assert(es == seat->sprite);
2037
2038 seat->hotspot_x -= dx;
2039 seat->hotspot_y -= dy;
2040
2041 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
2042 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
2043
2044 weston_surface_configure(seat->sprite, x, y,
2045 es->buffer->width, es->buffer->height);
2046
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03002047 empty_region(&es->input);
2048
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002049 if (!weston_surface_is_mapped(es)) {
2050 wl_list_insert(&es->compositor->cursor_layer.surface_list,
2051 &es->layer_link);
2052 weston_surface_assign_output(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002053 }
2054}
2055
2056static void
2057pointer_unmap_sprite(struct weston_seat *seat)
2058{
2059 if (weston_surface_is_mapped(seat->sprite))
2060 weston_surface_unmap(seat->sprite);
2061
2062 wl_list_remove(&seat->sprite_destroy_listener.link);
2063 seat->sprite->configure = NULL;
2064 seat->sprite->private = NULL;
2065 seat->sprite = NULL;
2066}
2067
2068static void
2069pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2070 uint32_t serial, struct wl_resource *surface_resource,
2071 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002072{
Daniel Stone37816df2012-05-16 18:45:18 +01002073 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002074 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002075
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002076 if (surface_resource)
2077 surface = container_of(surface_resource->data,
2078 struct weston_surface, surface);
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002079
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04002080 if (seat->seat.pointer->focus == NULL)
2081 return;
2082 if (seat->seat.pointer->focus->resource.client != client)
2083 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002084 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002085 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002086
2087 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002088 if (surface->configure) {
2089 wl_resource_post_error(&surface->surface.resource,
2090 WL_DISPLAY_ERROR_INVALID_OBJECT,
2091 "surface->configure already "
2092 "set");
2093 return;
2094 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002095 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002096
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002097 if (seat->sprite)
2098 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002099
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002100 if (!surface)
2101 return;
2102
2103 wl_signal_add(&surface->surface.resource.destroy_signal,
2104 &seat->sprite_destroy_listener);
2105
2106 surface->configure = pointer_cursor_surface_configure;
2107 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002108 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002109 seat->hotspot_x = x;
2110 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002111
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04002112 if (surface->buffer)
2113 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002114}
2115
Daniel Stone37816df2012-05-16 18:45:18 +01002116static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002117 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002118};
2119
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002120static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002121handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002122{
Daniel Stone37816df2012-05-16 18:45:18 +01002123 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002124
Daniel Stone37816df2012-05-16 18:45:18 +01002125 seat = container_of(listener, struct weston_seat,
2126 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002127
Daniel Stone37816df2012-05-16 18:45:18 +01002128 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002129}
2130
Casey Dahlin9074db52012-04-19 22:50:09 -04002131static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002132{
2133 wl_list_remove(&resource->link);
2134 free(resource);
2135}
2136
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002137static void
Daniel Stone37816df2012-05-16 18:45:18 +01002138seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2139 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002140{
Daniel Stone37816df2012-05-16 18:45:18 +01002141 struct weston_seat *seat = resource->data;
2142 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002143
Daniel Stone37816df2012-05-16 18:45:18 +01002144 if (!seat->seat.pointer)
2145 return;
2146
2147 cr = wl_client_add_object(client, &wl_pointer_interface,
2148 &pointer_interface, id, seat);
2149 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002150 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002151
2152 if (seat->seat.pointer->focus &&
2153 seat->seat.pointer->focus->resource.client == client) {
2154 struct weston_surface *surface;
2155 wl_fixed_t sx, sy;
2156
2157 surface = (struct weston_surface *) seat->seat.pointer->focus;
2158 weston_surface_from_global_fixed(surface,
2159 seat->seat.pointer->x,
2160 seat->seat.pointer->y,
2161 &sx,
2162 &sy);
2163 wl_pointer_set_focus(seat->seat.pointer,
2164 seat->seat.pointer->focus,
2165 sx,
2166 sy);
2167 }
Daniel Stone37816df2012-05-16 18:45:18 +01002168}
2169
2170static void
2171seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2172 uint32_t id)
2173{
2174 struct weston_seat *seat = resource->data;
2175 struct wl_resource *cr;
2176
2177 if (!seat->seat.keyboard)
2178 return;
2179
2180 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2181 seat);
2182 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002183 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002184
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002185 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2186 seat->xkb_info.keymap_fd,
2187 seat->xkb_info.keymap_size);
2188
Daniel Stone17b13bd2012-05-30 16:31:39 +01002189 if (seat->seat.keyboard->focus &&
2190 seat->seat.keyboard->focus->resource.client == client) {
2191 wl_keyboard_set_focus(seat->seat.keyboard,
2192 seat->seat.keyboard->focus);
2193 }
Daniel Stone37816df2012-05-16 18:45:18 +01002194}
2195
2196static void
2197seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2198 uint32_t id)
2199{
2200 struct weston_seat *seat = resource->data;
2201 struct wl_resource *cr;
2202
2203 if (!seat->seat.touch)
2204 return;
2205
2206 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2207 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002208 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002209}
2210
2211static const struct wl_seat_interface seat_interface = {
2212 seat_get_pointer,
2213 seat_get_keyboard,
2214 seat_get_touch,
2215};
2216
2217static void
2218bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2219{
2220 struct wl_seat *seat = data;
2221 struct wl_resource *resource;
2222 enum wl_seat_capability caps = 0;
2223
2224 resource = wl_client_add_object(client, &wl_seat_interface,
2225 &seat_interface, id, data);
2226 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002227 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002228
2229 if (seat->pointer)
2230 caps |= WL_SEAT_CAPABILITY_POINTER;
2231 if (seat->keyboard)
2232 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2233 if (seat->touch)
2234 caps |= WL_SEAT_CAPABILITY_TOUCH;
2235
2236 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002237}
2238
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002239static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002240device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002241{
Daniel Stone37816df2012-05-16 18:45:18 +01002242 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002243
Daniel Stone37816df2012-05-16 18:45:18 +01002244 seat = container_of(listener, struct weston_seat,
2245 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002246
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002247 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002248}
2249
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002250static void weston_compositor_xkb_init(struct weston_compositor *ec,
2251 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002252{
Daniel Stonee379da92012-05-30 16:32:04 +01002253 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002254 ec->xkb_context = xkb_context_new(0);
2255 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002256 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002257 exit(1);
2258 }
Daniel Stone994679a2012-05-30 16:31:43 +01002259 }
2260
2261 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002262 ec->xkb_names = *names;
2263 if (!ec->xkb_names.rules)
2264 ec->xkb_names.rules = strdup("evdev");
2265 if (!ec->xkb_names.model)
2266 ec->xkb_names.model = strdup("pc105");
2267 if (!ec->xkb_names.layout)
2268 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002269}
2270
Daniel Stonee379da92012-05-30 16:32:04 +01002271static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2272{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002273 if (xkb_info->keymap)
2274 xkb_map_unref(xkb_info->keymap);
2275
2276 if (xkb_info->keymap_area)
2277 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2278 if (xkb_info->keymap_fd >= 0)
2279 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002280}
2281
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002282static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2283{
Daniel Stonee379da92012-05-30 16:32:04 +01002284 free((char *) ec->xkb_names.rules);
2285 free((char *) ec->xkb_names.model);
2286 free((char *) ec->xkb_names.layout);
2287 free((char *) ec->xkb_names.variant);
2288 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002289
Daniel Stonee379da92012-05-30 16:32:04 +01002290 xkb_info_destroy(&ec->xkb_info);
2291 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002292}
2293
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002294static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002295weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002296{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002297 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002298
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002299 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2300 XKB_MOD_NAME_SHIFT);
2301 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2302 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002303 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2304 XKB_MOD_NAME_CTRL);
2305 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2306 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002307 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2308 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002309 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2310 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002311 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002312
2313 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2314 XKB_LED_NAME_NUM);
2315 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2316 XKB_LED_NAME_CAPS);
2317 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2318 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002319
2320 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2321 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002322 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002323 exit(EXIT_FAILURE);
2324 }
2325 xkb_info->keymap_size = strlen(keymap_str) + 1;
2326
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002327 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002328 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002329 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002330 (unsigned long) xkb_info->keymap_size);
2331 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002332 }
2333
2334 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2335 PROT_READ | PROT_WRITE,
2336 MAP_SHARED, xkb_info->keymap_fd, 0);
2337 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002338 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002339 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002340 goto err_dev_zero;
2341 }
2342 strcpy(xkb_info->keymap_area, keymap_str);
2343 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002344
2345 return;
2346
2347err_dev_zero:
2348 close(xkb_info->keymap_fd);
2349 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002350err_keymap_str:
2351 free(keymap_str);
2352 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002353}
2354
2355static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002356weston_compositor_build_global_keymap(struct weston_compositor *ec)
2357{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002358 if (ec->xkb_info.keymap != NULL)
2359 return;
2360
Daniel Stonee379da92012-05-30 16:32:04 +01002361 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002362 &ec->xkb_names,
2363 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002364 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002365 weston_log("failed to compile global XKB keymap\n");
2366 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002367 "options %s",
2368 ec->xkb_names.rules, ec->xkb_names.model,
2369 ec->xkb_names.layout, ec->xkb_names.variant,
2370 ec->xkb_names.options);
2371 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002372 }
2373
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002374 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002375}
2376
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002377WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002378weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002379{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002380 if (seat->has_keyboard)
2381 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002382
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002383 if (keymap != NULL) {
2384 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002385 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002386 }
2387 else {
2388 weston_compositor_build_global_keymap(seat->compositor);
2389 seat->xkb_info = seat->compositor->xkb_info;
2390 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2391 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002392
2393 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2394 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002395 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01002396 exit(1);
2397 }
2398
Daniel Stone71c38772012-06-22 13:21:30 +01002399 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002400
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002401 wl_keyboard_init(&seat->keyboard);
2402 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2403
2404 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01002405}
2406
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002407WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002408weston_seat_init_pointer(struct weston_seat *seat)
2409{
2410 if (seat->has_pointer)
2411 return;
2412
2413 wl_pointer_init(&seat->pointer);
2414 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2415
2416 seat->has_pointer = 1;
2417}
2418
2419WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002420weston_seat_init_touch(struct weston_seat *seat)
2421{
2422 if (seat->has_touch)
2423 return;
2424
2425 wl_touch_init(&seat->touch);
2426 wl_seat_set_touch(&seat->seat, &seat->touch);
2427
2428 seat->has_touch = 1;
2429}
2430
2431WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002432weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002433{
Daniel Stone37816df2012-05-16 18:45:18 +01002434 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002435 seat->has_pointer = 0;
2436 seat->has_keyboard = 0;
2437 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002438
Daniel Stone37816df2012-05-16 18:45:18 +01002439 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2440 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002441
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002442 seat->sprite = NULL;
2443 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002444
Daniel Stone37816df2012-05-16 18:45:18 +01002445 seat->compositor = ec;
2446 seat->hotspot_x = 16;
2447 seat->hotspot_y = 16;
2448 seat->modifier_state = 0;
2449 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002450
Daniel Stone37816df2012-05-16 18:45:18 +01002451 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002452 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002453
Daniel Stone37816df2012-05-16 18:45:18 +01002454 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002455
Daniel Stone37816df2012-05-16 18:45:18 +01002456 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2457 wl_signal_add(&seat->seat.drag_icon_signal,
2458 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002459
2460 clipboard_create(seat);
Jan Arne Petersene829adc2012-08-10 16:47:22 +02002461 input_method_create(ec, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002462}
2463
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002464WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002465weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002466{
Daniel Stone37816df2012-05-16 18:45:18 +01002467 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002468 /* The global object is destroyed at wl_display_destroy() time. */
2469
Daniel Stone37816df2012-05-16 18:45:18 +01002470 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002471 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002472
Daniel Stone74419a22012-05-30 16:32:02 +01002473 if (seat->xkb_state.state != NULL)
2474 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01002475 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002476
Daniel Stone37816df2012-05-16 18:45:18 +01002477 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002478}
2479
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002480static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002481drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2482{
2483 weston_surface_configure(es,
2484 es->geometry.x + sx, es->geometry.y + sy,
2485 es->buffer->width, es->buffer->height);
2486}
2487
2488static int
Daniel Stone37816df2012-05-16 18:45:18 +01002489device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002490 struct weston_surface *surface)
2491{
Daniel Stone37816df2012-05-16 18:45:18 +01002492 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002493
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002494 if (surface->configure) {
2495 wl_resource_post_error(&surface->surface.resource,
2496 WL_DISPLAY_ERROR_INVALID_OBJECT,
2497 "surface->configure already set");
2498 return 0;
2499 }
2500
Daniel Stone37816df2012-05-16 18:45:18 +01002501 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002502
Daniel Stone37816df2012-05-16 18:45:18 +01002503 weston_surface_set_position(ws->drag_surface,
2504 wl_fixed_to_double(seat->pointer->x),
2505 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002506
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002507 surface->configure = drag_surface_configure;
2508
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002509 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002510 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002511
2512 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002513}
2514
2515static void
Daniel Stone37816df2012-05-16 18:45:18 +01002516device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002517{
Daniel Stone37816df2012-05-16 18:45:18 +01002518 seat->drag_surface->configure = NULL;
2519 undef_region(&seat->drag_surface->input);
2520 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2521 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002522}
2523
2524static void
Daniel Stone37816df2012-05-16 18:45:18 +01002525device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002526{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002527 struct wl_list *list;
2528
Daniel Stone37816df2012-05-16 18:45:18 +01002529 if (weston_surface_is_mapped(seat->drag_surface) ||
2530 !seat->drag_surface->buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002531 return;
2532
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002533 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2534 list = &seat->sprite->layer_link;
2535 else
2536 list = &seat->compositor->cursor_layer.surface_list;
2537
2538 wl_list_insert(list, &seat->drag_surface->layer_link);
Daniel Stone37816df2012-05-16 18:45:18 +01002539 weston_surface_assign_output(seat->drag_surface);
2540 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002541}
2542
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002543static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002544weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01002545 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002546{
2547 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002548
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002549 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002550 return;
2551
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002552 if (seat->drag_surface && seat->seat.drag_surface &&
2553 (&seat->drag_surface->surface.resource !=
2554 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002555 /* between calls to this funcion we got a new drag_surface */
2556 surface_changed = 1;
2557
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002558 if (!seat->seat.drag_surface || surface_changed) {
2559 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002560 if (!surface_changed)
2561 return;
2562 }
2563
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002564 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002565 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002566 seat->seat.drag_surface;
2567 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002568 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002569 }
2570
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002571 /* the client may not have attached a buffer to the drag surface
2572 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002573 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002574
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02002575 /* the client may have attached a buffer with a different size to
2576 * the drag surface, causing the input region to be reset */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002577 if (region_is_undefined(&seat->drag_surface->input))
2578 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02002579
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002580 if (!dx && !dy)
2581 return;
2582
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002583 weston_surface_set_position(seat->drag_surface,
2584 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
2585 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002586}
2587
2588WL_EXPORT void
2589weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2590{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002591 struct weston_seat *seat;
2592
2593 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002594 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002595}
2596
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002597static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002598bind_output(struct wl_client *client,
2599 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002600{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002601 struct weston_output *output = data;
2602 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002603 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002604
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002605 resource = wl_client_add_object(client,
2606 &wl_output_interface, NULL, id, data);
2607
Casey Dahlin9074db52012-04-19 22:50:09 -04002608 wl_list_insert(&output->resource_list, &resource->link);
2609 resource->destroy = unbind_resource;
2610
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002611 wl_output_send_geometry(resource,
2612 output->x,
2613 output->y,
2614 output->mm_width,
2615 output->mm_height,
2616 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002617 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002618 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002619
2620 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002621 wl_output_send_mode(resource,
2622 mode->flags,
2623 mode->width,
2624 mode->height,
2625 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002626 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002627}
2628
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002629WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002630weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002631{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002632 struct weston_compositor *c = output->compositor;
2633
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002634 pixman_region32_fini(&output->region);
Kristian Høgsberg8b72f602011-06-23 20:46:34 -04002635 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002636 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002637
2638 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002639}
2640
Scott Moreau1bad5db2012-08-18 01:04:05 -06002641static void
2642weston_output_compute_transform(struct weston_output *output)
2643{
2644 struct weston_matrix transform;
2645 int flip;
2646
2647 weston_matrix_init(&transform);
2648
2649 switch(output->transform) {
2650 case WL_OUTPUT_TRANSFORM_FLIPPED:
2651 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2652 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2653 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2654 flip = -1;
2655 break;
2656 default:
2657 flip = 1;
2658 break;
2659 }
2660
2661 switch(output->transform) {
2662 case WL_OUTPUT_TRANSFORM_NORMAL:
2663 case WL_OUTPUT_TRANSFORM_FLIPPED:
2664 transform.d[0] = flip;
2665 transform.d[1] = 0;
2666 transform.d[4] = 0;
2667 transform.d[5] = 1;
2668 break;
2669 case WL_OUTPUT_TRANSFORM_90:
2670 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2671 transform.d[0] = 0;
2672 transform.d[1] = -flip;
2673 transform.d[4] = 1;
2674 transform.d[5] = 0;
2675 break;
2676 case WL_OUTPUT_TRANSFORM_180:
2677 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2678 transform.d[0] = -flip;
2679 transform.d[1] = 0;
2680 transform.d[4] = 0;
2681 transform.d[5] = -1;
2682 break;
2683 case WL_OUTPUT_TRANSFORM_270:
2684 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2685 transform.d[0] = 0;
2686 transform.d[1] = flip;
2687 transform.d[4] = -1;
2688 transform.d[5] = 0;
2689 break;
2690 default:
2691 break;
2692 }
2693
2694 weston_matrix_multiply(&output->matrix, &transform);
2695}
2696
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002697WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002698weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002699{
Scott Moreau850ca422012-05-21 15:21:25 -06002700 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002701 struct weston_matrix camera;
2702 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002703
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002704 weston_matrix_init(&output->matrix);
2705 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002706 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2707 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002708
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002709 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002710 2.0 / (output->width + output->border.left + output->border.right),
2711 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2712
2713 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002714
Scott Moreauccbf29d2012-02-22 14:21:41 -07002715 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002716 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002717 weston_matrix_init(&camera);
2718 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002719 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002720 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002721 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002722 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002723 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002724 weston_matrix_multiply(&output->matrix, &modelview);
2725 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002726
Scott Moreauccbf29d2012-02-22 14:21:41 -07002727 output->dirty = 0;
2728}
2729
Scott Moreau1bad5db2012-08-18 01:04:05 -06002730static void
2731weston_output_transform_init(struct weston_output *output, uint32_t transform)
2732{
2733 output->transform = transform;
2734
2735 switch (transform) {
2736 case WL_OUTPUT_TRANSFORM_90:
2737 case WL_OUTPUT_TRANSFORM_270:
2738 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2739 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2740 /* Swap width and height */
2741 output->width = output->current->height;
2742 output->height = output->current->width;
2743 break;
2744 case WL_OUTPUT_TRANSFORM_NORMAL:
2745 case WL_OUTPUT_TRANSFORM_180:
2746 case WL_OUTPUT_TRANSFORM_FLIPPED:
2747 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2748 output->width = output->current->width;
2749 output->height = output->current->height;
2750 break;
2751 default:
2752 break;
2753 }
2754}
2755
Scott Moreauccbf29d2012-02-22 14:21:41 -07002756WL_EXPORT void
2757weston_output_move(struct weston_output *output, int x, int y)
2758{
2759 output->x = x;
2760 output->y = y;
2761
2762 pixman_region32_init(&output->previous_damage);
2763 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002764 output->width,
2765 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002766}
2767
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002768WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002769weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002770 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002771{
2772 output->compositor = c;
2773 output->x = x;
2774 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002775 output->border.top = 0;
2776 output->border.bottom = 0;
2777 output->border.left = 0;
2778 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002779 output->mm_width = width;
2780 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002781 output->dirty = 1;
2782
Scott Moreau1bad5db2012-08-18 01:04:05 -06002783 if (transform != WL_OUTPUT_TRANSFORM_NORMAL)
2784 output->disable_planes++;
2785
2786 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06002787 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002788
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002789 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002790 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002791
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002792 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002793 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002794 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002795
Casey Dahlin58ba1372012-04-19 22:50:08 -04002796 output->id = ffs(~output->compositor->output_id_pool) - 1;
2797 output->compositor->output_id_pool |= 1 << output->id;
2798
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002799 output->global =
2800 wl_display_add_global(c->wl_display, &wl_output_interface,
2801 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002802}
2803
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002804static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002805compositor_bind(struct wl_client *client,
2806 void *data, uint32_t version, uint32_t id)
2807{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002808 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002809
2810 wl_client_add_object(client, &wl_compositor_interface,
2811 &compositor_interface, id, compositor);
2812}
2813
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002814static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002815log_uname(void)
2816{
2817 struct utsname usys;
2818
2819 uname(&usys);
2820
2821 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2822 usys.version, usys.machine);
2823}
2824
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002825WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002826weston_compositor_init(struct weston_compositor *ec,
2827 struct wl_display *display,
2828 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04002829 char *argv[],
2830 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002831{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002832 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002833 struct xkb_rule_names xkb_names;
2834 const struct config_key keyboard_config_keys[] = {
2835 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2836 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2837 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2838 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2839 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2840 };
2841 const struct config_section cs[] = {
2842 { "keyboard",
2843 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
2844 };
2845
2846 memset(&xkb_names, 0, sizeof(xkb_names));
2847 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002848
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002849 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002850 wl_signal_init(&ec->destroy_signal);
2851 wl_signal_init(&ec->activate_signal);
2852 wl_signal_init(&ec->lock_signal);
2853 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002854 wl_signal_init(&ec->show_input_panel_signal);
2855 wl_signal_init(&ec->hide_input_panel_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002856 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002857
Casey Dahlin58ba1372012-04-19 22:50:08 -04002858 ec->output_id_pool = 0;
2859
Kristian Høgsberga8873122011-11-23 10:39:34 -05002860 if (!wl_display_add_global(display, &wl_compositor_interface,
2861 ec, compositor_bind))
2862 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002863
Daniel Stone725c2c32012-06-22 14:04:36 +01002864 wl_list_init(&ec->surface_list);
2865 wl_list_init(&ec->layer_list);
2866 wl_list_init(&ec->seat_list);
2867 wl_list_init(&ec->output_list);
2868 wl_list_init(&ec->key_binding_list);
2869 wl_list_init(&ec->button_binding_list);
2870 wl_list_init(&ec->axis_binding_list);
2871 wl_list_init(&ec->fade.animation.link);
2872
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002873 weston_plane_init(&ec->primary_plane, 0, 0);
2874
Daniel Stone725c2c32012-06-22 14:04:36 +01002875 weston_compositor_xkb_init(ec, &xkb_names);
2876
2877 ec->ping_handler = NULL;
2878
2879 screenshooter_create(ec);
2880 text_cursor_position_notifier_create(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002881
2882 wl_data_device_manager_init(ec->wl_display);
2883
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002884 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002885
Daniel Stone725c2c32012-06-22 14:04:36 +01002886 loop = wl_display_get_event_loop(ec->wl_display);
2887 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2888 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2889
2890 ec->input_loop = wl_event_loop_create();
2891
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002892 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
2893 ec->fade.animation.frame = fade_frame;
2894
2895 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2896 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2897
2898 weston_compositor_schedule_repaint(ec);
2899
Daniel Stone725c2c32012-06-22 14:04:36 +01002900 return 0;
2901}
2902
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002903WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002904weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002905{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002906 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002907
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002908 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002909 if (ec->input_loop_source)
2910 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002911
Matt Roper361d2ad2011-08-29 13:52:23 -07002912 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002913 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002914 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002915
Daniel Stone325fc2d2012-05-30 16:31:58 +01002916 weston_binding_list_destroy_all(&ec->key_binding_list);
2917 weston_binding_list_destroy_all(&ec->button_binding_list);
2918 weston_binding_list_destroy_all(&ec->axis_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002919
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002920 weston_plane_release(&ec->primary_plane);
2921
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002922 wl_array_release(&ec->vertices);
2923 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05002924 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002925
2926 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07002927}
2928
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002929static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002930{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002931 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002932
Martin Minarik6d118362012-06-07 18:01:59 +02002933 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002934 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002935
2936 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002937}
2938
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002939static void
2940on_segv_signal(int s, siginfo_t *siginfo, void *context)
2941{
2942 void *buffer[32];
2943 int i, count;
2944 Dl_info info;
2945
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002946 /* This SIGSEGV handler will do a best-effort backtrace, and
2947 * then call the backend restore function, which will switch
2948 * back to the vt we launched from or ungrab X etc and then
2949 * raise SIGTRAP. If we run weston under gdb from X or a
2950 * different vt, and tell gdb "handle SIGSEGV nostop", this
2951 * will allow weston to switch back to gdb on crash and then
2952 * gdb will catch the crash with SIGTRAP. */
2953
Martin Minarik6d118362012-06-07 18:01:59 +02002954 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002955
2956 count = backtrace(buffer, ARRAY_LENGTH(buffer));
2957 for (i = 0; i < count; i++) {
2958 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02002959 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002960 (long) buffer[i],
2961 info.dli_sname ? info.dli_sname : "--",
2962 info.dli_fname);
2963 }
2964
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002965 segv_compositor->restore(segv_compositor);
2966
2967 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002968}
2969
2970
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002971static void *
2972load_module(const char *name, const char *entrypoint, void **handle)
2973{
2974 char path[PATH_MAX];
2975 void *module, *init;
2976
2977 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07002978 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002979 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02002980 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002981
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002982 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04002983 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002984 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002985 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002986 return NULL;
2987 }
2988
2989 init = dlsym(module, entrypoint);
2990 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002991 weston_log("Failed to lookup init function: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002992 return NULL;
2993 }
2994
2995 return init;
2996}
2997
Pekka Paalanen78a0b572012-06-06 16:59:44 +03002998static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02002999 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3000
3001static const char xdg_wrong_message[] =
3002 "fatal: environment variable XDG_RUNTIME_DIR\n"
3003 "is set to \"%s\", which is not a directory.\n";
3004
3005static const char xdg_wrong_mode_message[] =
3006 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3007 "correctly. Unix access mode must be 0700 but is %o,\n"
3008 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003009 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003010
3011static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003012 "Refer to your distribution on how to get it, or\n"
3013 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3014 "on how to implement it.\n";
3015
Martin Minarik37032f82012-06-18 20:15:18 +02003016static void
3017verify_xdg_runtime_dir(void)
3018{
3019 char *dir = getenv("XDG_RUNTIME_DIR");
3020 struct stat s;
3021
3022 if (!dir) {
3023 weston_log(xdg_error_message);
3024 weston_log_continue(xdg_detail_message);
3025 exit(EXIT_FAILURE);
3026 }
3027
3028 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3029 weston_log(xdg_wrong_message, dir);
3030 weston_log_continue(xdg_detail_message);
3031 exit(EXIT_FAILURE);
3032 }
3033
3034 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3035 weston_log(xdg_wrong_mode_message,
3036 dir, s.st_mode & 0777, s.st_uid);
3037 weston_log_continue(xdg_detail_message);
3038 }
3039}
3040
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003041static int
3042usage(int error_code)
3043{
3044 fprintf(stderr,
3045 "Usage: weston [OPTIONS]\n\n"
3046 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3047 "Weston supports multiple backends, and depending on which backend is in use\n"
3048 "different options will be accepted.\n\n"
3049
3050
3051 "Core options:\n\n"
3052 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3053 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3054 " -S, --socket=NAME\tName of socket to listen on\n"
3055 " -i, --idle-time=SECS\tIdle time in seconds\n"
3056 " --xserver\t\tEnable X server integration\n"
3057 " --module\t\tLoad the specified module\n"
3058 " --log==FILE\t\tLog to the given file\n"
3059 " -h, --help\t\tThis help message\n\n");
3060
3061 fprintf(stderr,
3062 "Options for drm-backend.so:\n\n"
3063 " --connector=ID\tBring up only this connector\n"
3064 " --seat=SEAT\t\tThe seat that weston should run on\n"
3065 " --tty=TTY\t\tThe tty to use\n"
3066 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3067
3068 fprintf(stderr,
3069 "Options for x11-backend.so:\n\n"
3070 " --width=WIDTH\t\tWidth of X window\n"
3071 " --height=HEIGHT\tHeight of X window\n"
3072 " --fullscreen\t\tRun in fullscreen mode\n"
3073 " --output-count=COUNT\tCreate multiple outputs\n"
3074 " --no-input\t\tDont create input devices\n\n");
3075
3076 fprintf(stderr,
3077 "Options for wayland-backend.so:\n\n"
3078 " --width=WIDTH\t\tWidth of Wayland surface\n"
3079 " --height=HEIGHT\tHeight of Wayland surface\n"
3080 " --display=DISPLAY\tWayland display to connect to\n\n");
3081
3082 exit(error_code);
3083}
3084
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003085int main(int argc, char *argv[])
3086{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003087 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003088 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003089 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003090 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003091 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003092 struct sigaction segv_action;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04003093 void *shell_module, *backend_module, *xserver_module;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003094 int (*module_init)(struct weston_compositor *ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003095 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003096 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003097 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003098 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003099 char *backend = NULL;
3100 char *shell = NULL;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003101 char *module = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003102 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003103 int32_t idle_time = 300;
Scott Moreaue17f6102012-04-25 10:03:06 -06003104 int32_t xserver = 0;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003105 int32_t help = 0;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003106 char *socket_name = NULL;
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003107 char *config_file;
3108
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003109 const struct config_key shell_config_keys[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003110 { "type", CONFIG_KEY_STRING, &shell },
3111 };
3112
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003113 const struct config_section cs[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003114 { "shell",
3115 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
3116 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003117
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003118 const struct weston_option core_options[] = {
3119 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3120 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3121 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003122 { WESTON_OPTION_BOOLEAN, "xserver", 0, &xserver },
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003123 { WESTON_OPTION_STRING, "module", 0, &module },
Martin Minarik19e6f262012-06-07 13:08:46 +02003124 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003125 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003126 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003127
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003128 argc = parse_options(core_options,
3129 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003130
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003131 if (help)
3132 usage(EXIT_SUCCESS);
3133
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003134 weston_log_file_open(log);
3135
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003136 weston_log("%s\n"
3137 STAMP_SPACE "%s\n"
3138 STAMP_SPACE "Bug reports to: %s\n"
3139 STAMP_SPACE "Build: %s\n",
3140 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003141 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003142 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003143
Martin Minarik37032f82012-06-18 20:15:18 +02003144 verify_xdg_runtime_dir();
3145
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003146 display = wl_display_create();
3147
Tiago Vignatti2116b892011-08-08 05:52:59 -07003148 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003149 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3150 display);
3151 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3152 display);
3153 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3154 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003155
3156 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003157 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3158 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003159
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003160 if (!backend) {
3161 if (getenv("WAYLAND_DISPLAY"))
3162 backend = "wayland-backend.so";
3163 else if (getenv("DISPLAY"))
3164 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003165 else
3166 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003167 }
3168
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003169 config_file = config_file_path("weston.ini");
3170 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003171
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003172 backend_init = load_module(backend, "backend_init", &backend_module);
3173 if (!backend_init)
3174 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003175
Daniel Stonec1be8e52012-06-01 11:14:02 -04003176 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003177 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003178 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003179 exit(EXIT_FAILURE);
3180 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003181
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003182 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3183 segv_action.sa_sigaction = on_segv_signal;
3184 sigemptyset(&segv_action.sa_mask);
3185 sigaction(SIGSEGV, &segv_action, NULL);
3186 segv_compositor = ec;
3187
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003188 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03003189 weston_log("fatal: unhandled option: %s\n", argv[i]);
3190 if (argv[1]) {
3191 ret = EXIT_FAILURE;
3192 goto out;
3193 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003194
Daniel Stonec1be8e52012-06-01 11:14:02 -04003195 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01003196
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003197 ec->option_idle_time = idle_time;
3198 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003199
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003200 module_init = NULL;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04003201 if (xserver)
Tiago Vignatti629ce232012-05-23 23:04:14 +03003202 module_init = load_module("xwayland.so",
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003203 "weston_xserver_init",
3204 &xserver_module);
Pekka Paalanen33616392012-07-30 16:56:57 +03003205 if (module_init && module_init(ec) < 0) {
3206 ret = EXIT_FAILURE;
3207 goto out;
3208 }
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04003209
Kristian Høgsberg33d75092012-08-13 13:56:03 -04003210 if (socket_name)
3211 setenv("WAYLAND_DISPLAY", socket_name, 1);
3212
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003213 if (!shell)
3214 shell = "desktop-shell.so";
3215 module_init = load_module(shell, "shell_init", &shell_module);
Pekka Paalanen33616392012-07-30 16:56:57 +03003216 if (!module_init || module_init(ec) < 0) {
3217 ret = EXIT_FAILURE;
3218 goto out;
3219 }
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003220
3221
3222 module_init = NULL;
3223 if (module)
3224 module_init = load_module(module, "module_init", NULL);
Pekka Paalanen33616392012-07-30 16:56:57 +03003225 if (module_init && module_init(ec) < 0) {
3226 ret = EXIT_FAILURE;
3227 goto out;
3228 }
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003229
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003230 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003231 weston_log("fatal: failed to add socket: %m\n");
3232 ret = EXIT_FAILURE;
3233 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003234 }
3235
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003236 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003237 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003238
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003239 wl_display_run(display);
3240
3241 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003242 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05003243 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003244
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003245 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003246
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003247 if (ec->has_bind_display)
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003248 ec->unbind_display(ec->egl_display, display);
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003249
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003250 for (i = ARRAY_LENGTH(signals); i;)
3251 wl_event_source_remove(signals[--i]);
3252
Daniel Stone855028f2012-05-01 20:37:10 +01003253 weston_compositor_xkb_destroy(ec);
3254
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003255 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003256 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003257
Martin Minarik19e6f262012-06-07 13:08:46 +02003258 weston_log_file_close();
3259
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003260 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003261}