blob: 6ee3a79faab63f9179d5880a3312c326f990ffd6 [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
Marcin Slusarz554a0da2013-02-18 13:27:22 -050052#ifdef HAVE_LIBUNWIND
53#define UNW_LOCAL_ONLY
54#include <libunwind.h>
55#endif
56
Pekka Paalanen50719bc2011-11-22 14:18:50 +020057#include <wayland-server.h>
Kristian Høgsberg82863022010-06-04 21:52:02 -040058#include "compositor.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030059#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040060#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050061#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050062
Kristian Høgsberg27da5382011-06-21 17:32:25 -040063static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040064static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040065
66static int
67sigchld_handler(int signal_number, void *data)
68{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050069 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040070 int status;
71 pid_t pid;
72
Bill Spitzak027b9622012-03-17 15:22:03 -070073 pid = waitpid(-1, &status, WNOHANG);
74 if (!pid)
75 return 1;
76
Kristian Høgsberg27da5382011-06-21 17:32:25 -040077 wl_list_for_each(p, &child_process_list, link) {
78 if (p->pid == pid)
79 break;
80 }
81
82 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020083 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040084 return 1;
85 }
86
87 wl_list_remove(&p->link);
88 p->cleanup(p, status);
89
90 return 1;
91}
92
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020093static void
94weston_output_transform_init(struct weston_output *output, uint32_t transform);
95
Alex Wu2dda6042012-04-17 17:20:47 +080096WL_EXPORT int
97weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode)
98{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020099 struct weston_seat *seat;
100 pixman_region32_t old_output_region;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200101 int ret;
102
Alex Wu2dda6042012-04-17 17:20:47 +0800103 if (!output->switch_mode)
104 return -1;
105
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200106 ret = output->switch_mode(output, mode);
107 if (ret < 0)
108 return ret;
109
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200110 pixman_region32_init(&old_output_region);
111 pixman_region32_copy(&old_output_region, &output->region);
112
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200113 /* Update output region and transformation matrix */
114 weston_output_transform_init(output, output->transform);
115
116 pixman_region32_init(&output->previous_damage);
117 pixman_region32_init_rect(&output->region, output->x, output->y,
118 output->width, output->height);
119
120 weston_output_update_matrix(output);
121
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200122 /* If a pointer falls outside the outputs new geometry, move it to its
123 * lower-right corner */
124 wl_list_for_each(seat, &output->compositor->seat_list, link) {
125 struct wl_pointer *pointer = seat->seat.pointer;
126 int32_t x, y;
127
128 if (!pointer)
129 continue;
130
131 x = wl_fixed_to_int(pointer->x);
132 y = wl_fixed_to_int(pointer->y);
133
134 if (!pixman_region32_contains_point(&old_output_region,
135 x, y, NULL) ||
136 pixman_region32_contains_point(&output->region,
137 x, y, NULL))
138 continue;
139
140 if (x >= output->x + output->width)
141 x = output->x + output->width - 1;
142 if (y >= output->y + output->height)
143 y = output->y + output->height - 1;
144
145 pointer->x = wl_fixed_from_int(x);
146 pointer->y = wl_fixed_from_int(y);
147 }
148
149 pixman_region32_fini(&old_output_region);
150
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200151 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800152}
153
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400154WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500155weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400156{
157 wl_list_insert(&child_process_list, &process->link);
158}
159
Benjamin Franzke06286262011-05-06 19:12:33 +0200160static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200161child_client_exec(int sockfd, const char *path)
162{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500163 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200164 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200165 sigset_t allsigs;
166
167 /* do not give our signal mask to the new process */
168 sigfillset(&allsigs);
169 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200170
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500171 /* Launch clients as the user. */
172 seteuid(getuid());
173
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500174 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
175 * non-CLOEXEC fd to pass through exec. */
176 clientfd = dup(sockfd);
177 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200178 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500179 return;
180 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200181
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500182 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200183 setenv("WAYLAND_SOCKET", s, 1);
184
185 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200186 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200187 path);
188}
189
190WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500191weston_client_launch(struct weston_compositor *compositor,
192 struct weston_process *proc,
193 const char *path,
194 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200195{
196 int sv[2];
197 pid_t pid;
198 struct wl_client *client;
199
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300200 weston_log("launching '%s'\n", path);
201
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300202 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200203 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200204 "socketpair failed while launching '%s': %m\n",
205 path);
206 return NULL;
207 }
208
209 pid = fork();
210 if (pid == -1) {
211 close(sv[0]);
212 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200213 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200214 "fork failed while launching '%s': %m\n", path);
215 return NULL;
216 }
217
218 if (pid == 0) {
219 child_client_exec(sv[1], path);
220 exit(-1);
221 }
222
223 close(sv[1]);
224
225 client = wl_client_create(compositor->wl_display, sv[0]);
226 if (!client) {
227 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200228 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200229 "wl_client_create failed while launching '%s'.\n",
230 path);
231 return NULL;
232 }
233
234 proc->pid = pid;
235 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500236 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200237
238 return client;
239}
240
241static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300242surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
243{
244 struct weston_surface *surface =
245 container_of(listener, struct weston_surface,
246 pending.buffer_destroy_listener);
247
248 surface->pending.buffer = NULL;
249}
250
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200251static void
252empty_region(pixman_region32_t *region)
253{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300254 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200255 pixman_region32_init(region);
256}
257
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300258static void
259region_init_infinite(pixman_region32_t *region)
260{
261 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
262 UINT32_MAX, UINT32_MAX);
263}
264
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500265WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500266weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500267{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500268 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400269
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200270 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400271 if (surface == NULL)
272 return NULL;
273
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400274 wl_signal_init(&surface->surface.resource.destroy_signal);
Kristian Høgsberg48891542012-02-23 17:38:33 -0500275
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500276 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500277 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500278
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400279 surface->surface.resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400280
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500281 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400282 surface->alpha = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400283
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100284 if (compositor->renderer->create_surface(surface) < 0) {
285 free(surface);
286 return NULL;
287 }
288
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200289 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
290 surface->pending.buffer_transform = surface->buffer_transform;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400291 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400292 surface->plane = &compositor->primary_plane;
Benjamin Franzke06286262011-05-06 19:12:33 +0200293
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400294 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500295 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500296 pixman_region32_init(&surface->clip);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300297 region_init_infinite(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200298 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500299 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400300
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200301 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200302 wl_list_insert(&surface->geometry.transformation_list,
303 &surface->transform.position.link);
304 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200305 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200306 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500307
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300308 surface->pending.buffer_destroy_listener.notify =
309 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300310 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300311 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300312 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300313 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300314
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400315 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500316}
317
Alex Wu8811bf92012-02-28 18:07:54 +0800318WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500319weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200320 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500321{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100322 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500323}
324
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400325WL_EXPORT void
326weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200327 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200328{
329 if (surface->transform.enabled) {
330 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
331
332 weston_matrix_transform(&surface->transform.matrix, &v);
333
334 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200335 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700336 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200337 v.f[3]);
338 *x = 0;
339 *y = 0;
340 return;
341 }
342
343 *x = v.f[0] / v.f[3];
344 *y = v.f[1] / v.f[3];
345 } else {
346 *x = sx + surface->geometry.x;
347 *y = sy + surface->geometry.y;
348 }
349}
350
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500351WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200352weston_surface_to_buffer_float(struct weston_surface *surface,
353 float sx, float sy, float *bx, float *by)
354{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200355 weston_transformed_coord(surface->geometry.width,
356 surface->geometry.height,
357 surface->buffer_transform,
358 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200359}
360
361WL_EXPORT pixman_box32_t
362weston_surface_to_buffer_rect(struct weston_surface *surface,
363 pixman_box32_t rect)
364{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200365 return weston_transformed_rect(surface->geometry.width,
366 surface->geometry.height,
367 surface->buffer_transform, rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200368}
369
370WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400371weston_surface_move_to_plane(struct weston_surface *surface,
372 struct weston_plane *plane)
373{
374 if (surface->plane == plane)
375 return;
376
377 weston_surface_damage_below(surface);
378 surface->plane = plane;
379 weston_surface_damage(surface);
380}
381
382WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500383weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200384{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500385 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200386
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500387 pixman_region32_init(&damage);
388 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
389 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400390 pixman_region32_union(&surface->plane->damage,
391 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500392 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200393}
394
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400395static struct wl_resource *
396find_resource_for_client(struct wl_list *list, struct wl_client *client)
397{
398 struct wl_resource *r;
399
400 wl_list_for_each(r, list, link) {
401 if (r->client == client)
402 return r;
403 }
404
405 return NULL;
406}
407
408static void
409weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
410{
411 uint32_t different = es->output_mask ^ mask;
412 uint32_t entered = mask & different;
413 uint32_t left = es->output_mask & different;
414 struct weston_output *output;
415 struct wl_resource *resource = NULL;
416 struct wl_client *client = es->surface.resource.client;
417
418 es->output_mask = mask;
419 if (es->surface.resource.client == NULL)
420 return;
421 if (different == 0)
422 return;
423
424 wl_list_for_each(output, &es->compositor->output_list, link) {
425 if (1 << output->id & different)
426 resource =
427 find_resource_for_client(&output->resource_list,
428 client);
429 if (resource == NULL)
430 continue;
431 if (1 << output->id & entered)
432 wl_surface_send_enter(&es->surface.resource, resource);
433 if (1 << output->id & left)
434 wl_surface_send_leave(&es->surface.resource, resource);
435 }
436}
437
438static void
439weston_surface_assign_output(struct weston_surface *es)
440{
441 struct weston_compositor *ec = es->compositor;
442 struct weston_output *output, *new_output;
443 pixman_region32_t region;
444 uint32_t max, area, mask;
445 pixman_box32_t *e;
446
447 new_output = NULL;
448 max = 0;
449 mask = 0;
450 pixman_region32_init(&region);
451 wl_list_for_each(output, &ec->output_list, link) {
452 pixman_region32_intersect(&region, &es->transform.boundingbox,
453 &output->region);
454
455 e = pixman_region32_extents(&region);
456 area = (e->x2 - e->x1) * (e->y2 - e->y1);
457
458 if (area > 0)
459 mask |= 1 << output->id;
460
461 if (area >= max) {
462 new_output = output;
463 max = area;
464 }
465 }
466 pixman_region32_fini(&region);
467
468 es->output = new_output;
469 weston_surface_update_output_mask(es, mask);
470}
471
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200472static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200473surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
474 int32_t width, int32_t height,
475 pixman_region32_t *bbox)
476{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200477 float min_x = HUGE_VALF, min_y = HUGE_VALF;
478 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200479 int32_t s[4][2] = {
480 { sx, sy },
481 { sx, sy + height },
482 { sx + width, sy },
483 { sx + width, sy + height }
484 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200485 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200486 int i;
487
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300488 if (width == 0 || height == 0) {
489 /* avoid rounding empty bbox to 1x1 */
490 pixman_region32_init(bbox);
491 return;
492 }
493
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200494 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200495 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400496 weston_surface_to_global_float(surface,
497 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200498 if (x < min_x)
499 min_x = x;
500 if (x > max_x)
501 max_x = x;
502 if (y < min_y)
503 min_y = y;
504 if (y > max_y)
505 max_y = y;
506 }
507
Pekka Paalanen219b9822012-02-08 15:38:37 +0200508 int_x = floorf(min_x);
509 int_y = floorf(min_y);
510 pixman_region32_init_rect(bbox, int_x, int_y,
511 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200512}
513
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200514static void
515weston_surface_update_transform_disable(struct weston_surface *surface)
516{
517 surface->transform.enabled = 0;
518
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200519 /* round off fractions when not transformed */
520 surface->geometry.x = roundf(surface->geometry.x);
521 surface->geometry.y = roundf(surface->geometry.y);
522
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200523 pixman_region32_init_rect(&surface->transform.boundingbox,
524 surface->geometry.x,
525 surface->geometry.y,
526 surface->geometry.width,
527 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500528
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400529 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500530 pixman_region32_copy(&surface->transform.opaque,
531 &surface->opaque);
532 pixman_region32_translate(&surface->transform.opaque,
533 surface->geometry.x,
534 surface->geometry.y);
535 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200536}
537
538static int
539weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200540{
541 struct weston_matrix *matrix = &surface->transform.matrix;
542 struct weston_matrix *inverse = &surface->transform.inverse;
543 struct weston_transform *tform;
544
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200545 surface->transform.enabled = 1;
546
547 /* Otherwise identity matrix, but with x and y translation. */
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300548 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200549 surface->transform.position.matrix.d[12] = surface->geometry.x;
550 surface->transform.position.matrix.d[13] = surface->geometry.y;
551
552 weston_matrix_init(matrix);
553 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
554 weston_matrix_multiply(matrix, &tform->matrix);
555
556 if (weston_matrix_invert(inverse, matrix) < 0) {
557 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200558 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200559 " transformation not invertible.\n", surface);
560 return -1;
561 }
562
563 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
564 surface->geometry.height,
565 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500566
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200567 return 0;
568}
569
570WL_EXPORT void
571weston_surface_update_transform(struct weston_surface *surface)
572{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200573 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200574 return;
575
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200576 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200577
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500578 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200579
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200580 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500581 pixman_region32_fini(&surface->transform.opaque);
582 pixman_region32_init(&surface->transform.opaque);
583
Pekka Paalanencd403622012-01-25 13:37:39 +0200584 /* transform.position is always in transformation_list */
585 if (surface->geometry.transformation_list.next ==
586 &surface->transform.position.link &&
587 surface->geometry.transformation_list.prev ==
588 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200589 weston_surface_update_transform_disable(surface);
590 } else {
591 if (weston_surface_update_transform_enable(surface) < 0)
592 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200593 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200594
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400595 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200596
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300597 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200598}
599
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200600WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100601weston_surface_to_global_fixed(struct weston_surface *surface,
602 wl_fixed_t sx, wl_fixed_t sy,
603 wl_fixed_t *x, wl_fixed_t *y)
604{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200605 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100606
607 weston_surface_to_global_float(surface,
608 wl_fixed_to_double(sx),
609 wl_fixed_to_double(sy),
610 &xf, &yf);
611 *x = wl_fixed_from_double(xf);
612 *y = wl_fixed_from_double(yf);
613}
614
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400615WL_EXPORT void
616weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200617 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200618{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200619 if (surface->transform.enabled) {
620 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
621
622 weston_matrix_transform(&surface->transform.inverse, &v);
623
624 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200625 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200626 "weston_surface_from_global(), divisor = %g\n",
627 v.f[3]);
628 *sx = 0;
629 *sy = 0;
630 return;
631 }
632
Pekka Paalanencd403622012-01-25 13:37:39 +0200633 *sx = v.f[0] / v.f[3];
634 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200635 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200636 *sx = x - surface->geometry.x;
637 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200638 }
639}
640
641WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100642weston_surface_from_global_fixed(struct weston_surface *surface,
643 wl_fixed_t x, wl_fixed_t y,
644 wl_fixed_t *sx, wl_fixed_t *sy)
645{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200646 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100647
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400648 weston_surface_from_global_float(surface,
649 wl_fixed_to_double(x),
650 wl_fixed_to_double(y),
651 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100652 *sx = wl_fixed_from_double(sxf);
653 *sy = wl_fixed_from_double(syf);
654}
655
656WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200657weston_surface_from_global(struct weston_surface *surface,
658 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
659{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200660 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200661
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400662 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200663 *sx = floorf(sxf);
664 *sy = floorf(syf);
665}
666
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400667WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400668weston_surface_schedule_repaint(struct weston_surface *surface)
669{
670 struct weston_output *output;
671
672 wl_list_for_each(output, &surface->compositor->output_list, link)
673 if (surface->output_mask & (1 << output->id))
674 weston_output_schedule_repaint(output);
675}
676
677WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500678weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500679{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400680 pixman_region32_union_rect(&surface->damage, &surface->damage,
681 0, 0, surface->geometry.width,
682 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200683
Kristian Høgsberg98238702012-08-03 16:29:12 -0400684 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500685}
686
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400687WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500688weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200689 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400690{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200691 surface->geometry.x = x;
692 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200693 surface->geometry.width = width;
694 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200695 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400696}
697
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200698WL_EXPORT void
699weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200700 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200701{
702 surface->geometry.x = x;
703 surface->geometry.y = y;
704 surface->geometry.dirty = 1;
705}
706
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300707WL_EXPORT int
708weston_surface_is_mapped(struct weston_surface *surface)
709{
710 if (surface->output)
711 return 1;
712 else
713 return 0;
714}
715
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200716WL_EXPORT int32_t
717weston_surface_buffer_width(struct weston_surface *surface)
718{
719 switch (surface->buffer_transform) {
720 case WL_OUTPUT_TRANSFORM_90:
721 case WL_OUTPUT_TRANSFORM_270:
722 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
723 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200724 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200725 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200726 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200727 }
728}
729
730WL_EXPORT int32_t
731weston_surface_buffer_height(struct weston_surface *surface)
732{
733 switch (surface->buffer_transform) {
734 case WL_OUTPUT_TRANSFORM_90:
735 case WL_OUTPUT_TRANSFORM_270:
736 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
737 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200738 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200739 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200740 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200741 }
742}
743
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400744WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500745weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500746{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400747 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500748
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400749 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500750
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400751 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500752}
753
Tiago Vignatti9d393522012-02-10 16:26:19 +0200754static struct weston_surface *
755weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100756 wl_fixed_t x, wl_fixed_t y,
757 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200758{
759 struct weston_surface *surface;
760
761 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100762 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500763 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100764 wl_fixed_to_int(*sx),
765 wl_fixed_to_int(*sy),
766 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200767 return surface;
768 }
769
770 return NULL;
771}
772
773static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400774weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500775{
Scott Moreau447013d2012-02-18 05:05:29 -0700776 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500777 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400778 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500779
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400780 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100781 return;
782
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400783 surface = weston_compositor_pick_surface(seat->compositor,
784 pointer->x,
785 pointer->y,
786 &pointer->current_x,
787 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500788
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400789 if (&surface->surface != pointer->current) {
790 interface = pointer->grab->interface;
791 pointer->current = &surface->surface;
792 interface->focus(pointer->grab, &surface->surface,
793 pointer->current_x,
794 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500795 }
796
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400797 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500798 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100799 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400800 pointer->x,
801 pointer->y,
802 &pointer->grab->x,
803 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500804}
805
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400806static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500807weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400808{
Daniel Stone37816df2012-05-16 18:45:18 +0100809 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400810
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500811 if (!compositor->focus)
812 return;
813
Daniel Stone37816df2012-05-16 18:45:18 +0100814 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400815 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400816}
817
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400818WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500819weston_surface_unmap(struct weston_surface *surface)
820{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100821 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500822
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500823 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500824 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500825 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500826
Daniel Stone4dab5db2012-05-30 16:31:53 +0100827 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100828 if (seat->seat.keyboard &&
829 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100830 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100831 if (seat->seat.pointer &&
832 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100833 wl_pointer_set_focus(seat->seat.pointer,
834 NULL,
835 wl_fixed_from_int(0),
836 wl_fixed_from_int(0));
837 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500838
Kristian Høgsberg98238702012-08-03 16:29:12 -0400839 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500840}
841
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400842struct weston_frame_callback {
843 struct wl_resource resource;
844 struct wl_list link;
845};
846
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500847static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400848destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500849{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500850 struct weston_surface *surface =
851 container_of(resource,
852 struct weston_surface, surface.resource);
853 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400854 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400855
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300856 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500857 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400858
Pekka Paalanenbc106382012-10-10 12:49:31 +0300859 wl_list_for_each_safe(cb, next,
860 &surface->pending.frame_callback_list, link)
861 wl_resource_destroy(&cb->resource);
862
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300863 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300864 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +0300865 pixman_region32_fini(&surface->pending.damage);
866
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300867 if (surface->pending.buffer)
868 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
869
Pekka Paalanende685b82012-12-04 15:58:12 +0200870 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400871
Kristian Høgsberg42263852012-09-06 21:59:29 -0400872 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200873
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200874 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200875 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500876 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500877 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300878 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200879
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400880 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
881 wl_resource_destroy(&cb->resource);
882
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400883 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500884}
885
Alex Wu8811bf92012-02-28 18:07:54 +0800886WL_EXPORT void
887weston_surface_destroy(struct weston_surface *surface)
888{
889 /* Not a valid way to destroy a client surface */
890 assert(surface->surface.resource.client == NULL);
891
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400892 wl_signal_emit(&surface->surface.resource.destroy_signal,
893 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800894 destroy_surface(&surface->surface.resource);
895}
896
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100897static void
Pekka Paalanende685b82012-12-04 15:58:12 +0200898weston_buffer_reference_handle_destroy(struct wl_listener *listener,
899 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100900{
Pekka Paalanende685b82012-12-04 15:58:12 +0200901 struct weston_buffer_reference *ref =
902 container_of(listener, struct weston_buffer_reference,
903 destroy_listener);
904
905 assert((struct wl_buffer *)data == ref->buffer);
906 ref->buffer = NULL;
907}
908
909WL_EXPORT void
910weston_buffer_reference(struct weston_buffer_reference *ref,
911 struct wl_buffer *buffer)
912{
913 if (ref->buffer && buffer != ref->buffer) {
914 weston_buffer_post_release(ref->buffer);
915 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300916 }
917
Pekka Paalanende685b82012-12-04 15:58:12 +0200918 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400919 buffer->busy_count++;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300920 wl_signal_add(&buffer->resource.destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +0200921 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +0200922 }
923
Pekka Paalanende685b82012-12-04 15:58:12 +0200924 ref->buffer = buffer;
925 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
926}
927
928static void
929weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
930{
931 weston_buffer_reference(&surface->buffer_ref, buffer);
932
Pekka Paalanena6421c42012-12-04 15:58:10 +0200933 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300934 if (weston_surface_is_mapped(surface))
935 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300936 }
937
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300938 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100939}
940
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500941WL_EXPORT void
942weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400943{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500944 wl_list_remove(&surface->layer_link);
945 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500946 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500947 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400948}
949
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400950WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500951weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400952{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500953 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400954
955 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500956 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400957}
958
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500959WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500960weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +0200961{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400962 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200963 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200964
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400965 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -0500966 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200967}
968
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400969WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500970weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400971{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500972 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400973
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400974 pixman_region32_union(&compositor->primary_plane.damage,
975 &compositor->primary_plane.damage,
976 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400977 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400978}
979
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400980static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500981fade_frame(struct weston_animation *animation,
982 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400983{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500984 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400985 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500986 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500987 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400988
Scott Moreau94b0b0c2012-06-14 01:01:56 -0600989 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -0600990 compositor->fade.spring.timestamp = msecs;
991
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500992 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500993 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500994 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
995 compositor->fade.spring.current);
996 weston_surface_damage(surface);
997
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500998 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400999 compositor->fade.spring.current =
1000 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001001 wl_list_remove(&animation->link);
1002 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001003
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001004 if (compositor->fade.spring.current < 0.001) {
1005 destroy_surface(&surface->surface.resource);
1006 compositor->fade.surface = NULL;
1007 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001008 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001009 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001010 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001011 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001012}
1013
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001014static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001015surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001016 pixman_region32_t *opaque)
1017{
Pekka Paalanende685b82012-12-04 15:58:12 +02001018 if (surface->buffer_ref.buffer &&
1019 wl_buffer_is_shm(surface->buffer_ref.buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001020 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001021
1022 if (surface->transform.enabled) {
1023 pixman_box32_t *extents;
1024
1025 extents = pixman_region32_extents(&surface->damage);
1026 surface_compute_bbox(surface, extents->x1, extents->y1,
1027 extents->x2 - extents->x1,
1028 extents->y2 - extents->y1,
1029 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001030 pixman_region32_translate(&surface->damage,
1031 -surface->plane->x,
1032 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001033 } else {
1034 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001035 surface->geometry.x - surface->plane->x,
1036 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001037 }
1038
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001039 pixman_region32_subtract(&surface->damage,
1040 &surface->damage, &surface->plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001041 pixman_region32_union(&surface->plane->damage,
1042 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001043 empty_region(&surface->damage);
1044 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001045 pixman_region32_union(&surface->plane->opaque,
1046 &surface->plane->opaque,
1047 &surface->transform.opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001048 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001049}
1050
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001051static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001052weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001053{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001054 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001055 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001056 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001057 struct weston_animation *animation, *next;
1058 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001059 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001060 pixman_region32_t opaque, output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001061
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001062 weston_compositor_update_drag_surfaces(ec);
1063
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001064 /* Rebuild the surface list and update surface transforms up front. */
1065 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +02001066 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001067 wl_list_for_each(layer, &ec->layer_list, link) {
1068 wl_list_for_each(es, &layer->surface_list, layer_link) {
1069 weston_surface_update_transform(es);
1070 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +02001071 if (es->output == output) {
1072 wl_list_insert_list(&frame_callback_list,
1073 &es->frame_callback_list);
1074 wl_list_init(&es->frame_callback_list);
1075 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001076 }
1077 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001078
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001079 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001080 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001081 else
1082 wl_list_for_each(es, &ec->surface_list, link)
1083 weston_surface_move_to_plane(es, &ec->primary_plane);
1084
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001085 pixman_region32_init(&opaque);
1086
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001087 pixman_region32_fini(&ec->primary_plane.opaque);
1088 pixman_region32_init(&ec->primary_plane.opaque);
1089
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001090 wl_list_for_each(es, &ec->surface_list, link) {
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001091 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001092
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001093 /* Both the renderer and the backend have seen the buffer
1094 * by now. If renderer needs the buffer, it has its own
1095 * reference set. If the backend wants to keep the buffer
1096 * around for migrating the surface into a non-primary plane
1097 * later, keep_buffer is true. Otherwise, drop the core
1098 * reference now, and allow early buffer release. This enables
1099 * clients to use single-buffering.
1100 */
1101 if (!es->keep_buffer)
1102 weston_buffer_reference(&es->buffer_ref, NULL);
1103 }
1104
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -04001105 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001106
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001107 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001108 pixman_region32_intersect(&output_damage,
1109 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001110
Scott Moreauccbf29d2012-02-22 14:21:41 -07001111 if (output->dirty)
1112 weston_output_update_matrix(output);
1113
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001114 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001115
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001116 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001117
Kristian Høgsbergef044142011-06-21 15:02:12 -04001118 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001119
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001120 weston_compositor_repick(ec);
1121 wl_event_loop_dispatch(ec->input_loop, 0);
1122
Jonas Ådahldb773762012-06-13 00:01:21 +02001123 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001124 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001125 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001126 }
1127
Scott Moreaud64cf212012-06-08 19:40:54 -06001128 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001129 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001130 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001131 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001132}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001133
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001134static int
1135weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001136{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001137 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001138
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001139 wl_event_loop_dispatch(compositor->input_loop, 0);
1140
1141 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001142}
1143
1144WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001145weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001146{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001147 struct weston_compositor *compositor = output->compositor;
1148 struct wl_event_loop *loop =
1149 wl_display_get_event_loop(compositor->wl_display);
1150 int fd;
1151
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001152 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001153 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001154 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001155 return;
1156 }
1157
1158 output->repaint_scheduled = 0;
1159 if (compositor->input_loop_source)
1160 return;
1161
1162 fd = wl_event_loop_get_fd(compositor->input_loop);
1163 compositor->input_loop_source =
1164 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1165 weston_compositor_read_input, compositor);
1166}
1167
1168static void
1169idle_repaint(void *data)
1170{
1171 struct weston_output *output = data;
1172
1173 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001174}
1175
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001176WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001177weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1178{
1179 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001180 if (below != NULL)
1181 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001182}
1183
1184WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001185weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001186{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001187 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001188 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001189
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001190 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001191 return;
1192
Kristian Høgsbergef044142011-06-21 15:02:12 -04001193 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001194 output->repaint_needed = 1;
1195 if (output->repaint_scheduled)
1196 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001197
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001198 wl_event_loop_add_idle(loop, idle_repaint, output);
1199 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001200
1201 if (compositor->input_loop_source) {
1202 wl_event_source_remove(compositor->input_loop_source);
1203 compositor->input_loop_source = NULL;
1204 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001205}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001206
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001207WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001208weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1209{
1210 struct weston_output *output;
1211
1212 wl_list_for_each(output, &compositor->output_list, link)
1213 weston_output_schedule_repaint(output);
1214}
1215
1216WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001217weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001218{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001219 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001220 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001221
Scott Moreau9d1b1122012-06-08 19:40:53 -06001222 output = container_of(compositor->output_list.next,
1223 struct weston_output, link);
1224
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001225 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001226 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001227 return;
1228
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001229 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001230 surface = weston_surface_create(compositor);
John Kåre Alsakere2e3d072012-10-12 12:25:09 +02001231 if (!surface)
1232 return;
1233
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001234 weston_surface_configure(surface, 0, 0, 8192, 8192);
Ander Conselvan de Oliveira4c65da52013-02-13 16:06:37 +02001235 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0 - tint);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001236 wl_list_insert(&compositor->fade_layer.surface_list,
1237 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03001238 weston_surface_update_transform(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001239 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001240 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001241 }
1242
1243 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001244 if (wl_list_empty(&compositor->fade.animation.link)) {
1245 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001246 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001247 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001248 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001249}
1250
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001251static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001252surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001253{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001254 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001255}
1256
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001257static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001258surface_attach(struct wl_client *client,
1259 struct wl_resource *resource,
1260 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1261{
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001262 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001263 struct wl_buffer *buffer = NULL;
1264
1265 if (buffer_resource)
1266 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001267
Pekka Paalanende685b82012-12-04 15:58:12 +02001268 /* Attach, attach, without commit in between does not send
1269 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001270 if (surface->pending.buffer)
1271 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001272
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001273 surface->pending.sx = sx;
1274 surface->pending.sy = sy;
1275 surface->pending.buffer = buffer;
1276 if (buffer) {
1277 wl_signal_add(&buffer->resource.destroy_signal,
1278 &surface->pending.buffer_destroy_listener);
1279 surface->pending.remove_contents = 0;
1280 } else {
1281 surface->pending.remove_contents = 1;
1282 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001283}
1284
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001285static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001286surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001287 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001288 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001289{
Pekka Paalanen8e159182012-10-10 12:49:25 +03001290 struct weston_surface *surface = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001291
Pekka Paalanen8e159182012-10-10 12:49:25 +03001292 pixman_region32_union_rect(&surface->pending.damage,
1293 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001294 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001295}
1296
Kristian Høgsberg33418202011-08-16 23:01:28 -04001297static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001298destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001299{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001300 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001301
1302 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001303 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001304}
1305
1306static void
1307surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001308 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001309{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001310 struct weston_frame_callback *cb;
Pekka Paalanenbc106382012-10-10 12:49:31 +03001311 struct weston_surface *surface = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001312
1313 cb = malloc(sizeof *cb);
1314 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001315 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001316 return;
1317 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001318
Kristian Høgsberg33418202011-08-16 23:01:28 -04001319 cb->resource.object.interface = &wl_callback_interface;
1320 cb->resource.object.id = callback;
1321 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001322 cb->resource.client = client;
1323 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001324
1325 wl_client_add_resource(client, &cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001326 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001327}
1328
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001329static void
1330surface_set_opaque_region(struct wl_client *client,
1331 struct wl_resource *resource,
1332 struct wl_resource *region_resource)
1333{
1334 struct weston_surface *surface = resource->data;
1335 struct weston_region *region;
1336
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001337 if (region_resource) {
1338 region = region_resource->data;
Pekka Paalanen512dde82012-10-10 12:49:27 +03001339 pixman_region32_copy(&surface->pending.opaque,
1340 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001341 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001342 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001343 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001344}
1345
1346static void
1347surface_set_input_region(struct wl_client *client,
1348 struct wl_resource *resource,
1349 struct wl_resource *region_resource)
1350{
1351 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001352 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001353
1354 if (region_resource) {
1355 region = region_resource->data;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001356 pixman_region32_copy(&surface->pending.input,
1357 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001358 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001359 pixman_region32_fini(&surface->pending.input);
1360 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001361 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001362}
1363
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001364static int
1365surface_pending_buffer_has_different_size(struct weston_surface *surface)
1366{
1367 int width, height;
1368
1369 switch (surface->pending.buffer_transform) {
1370 case WL_OUTPUT_TRANSFORM_90:
1371 case WL_OUTPUT_TRANSFORM_270:
1372 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1373 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1374 height = surface->pending.buffer->width;
1375 width = surface->pending.buffer->height;
1376 break;
1377 default:
1378 width = surface->pending.buffer->width;
1379 height = surface->pending.buffer->height;
1380 }
1381
1382 if (width == surface->geometry.width &&
1383 height == surface->geometry.height)
1384 return 0;
1385 else
1386 return 1;
1387}
1388
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001389static void
1390surface_commit(struct wl_client *client, struct wl_resource *resource)
1391{
1392 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001393 pixman_region32_t opaque;
1394
1395 if (surface->pending.sx || surface->pending.sy ||
1396 (surface->pending.buffer &&
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001397 surface_pending_buffer_has_different_size(surface)))
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001398 surface->geometry.dirty = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001399
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001400 /* wl_surface.set_buffer_rotation */
1401 surface->buffer_transform = surface->pending.buffer_transform;
1402
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001403 /* wl_surface.attach */
1404 if (surface->pending.buffer || surface->pending.remove_contents)
1405 weston_surface_attach(surface, surface->pending.buffer);
1406
Pekka Paalanende685b82012-12-04 15:58:12 +02001407 if (surface->buffer_ref.buffer && surface->configure)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001408 surface->configure(surface, surface->pending.sx,
1409 surface->pending.sy);
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001410 surface->pending.sx = 0;
1411 surface->pending.sy = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001412
1413 /* wl_surface.damage */
1414 pixman_region32_union(&surface->damage, &surface->damage,
1415 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001416 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1417 0, 0,
1418 surface->geometry.width,
1419 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001420 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001421
1422 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001423 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001424 surface->geometry.width,
1425 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001426 pixman_region32_intersect(&opaque,
1427 &opaque, &surface->pending.opaque);
1428
1429 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1430 pixman_region32_copy(&surface->opaque, &opaque);
1431 surface->geometry.dirty = 1;
1432 }
1433
1434 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001435
1436 /* wl_surface.set_input_region */
1437 pixman_region32_fini(&surface->input);
1438 pixman_region32_init_rect(&surface->input, 0, 0,
1439 surface->geometry.width,
1440 surface->geometry.height);
1441 pixman_region32_intersect(&surface->input,
1442 &surface->input, &surface->pending.input);
1443
Pekka Paalanenbc106382012-10-10 12:49:31 +03001444 /* wl_surface.frame */
1445 wl_list_insert_list(&surface->frame_callback_list,
1446 &surface->pending.frame_callback_list);
1447 wl_list_init(&surface->pending.frame_callback_list);
1448
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001449 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001450}
1451
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001452static void
1453surface_set_buffer_transform(struct wl_client *client,
1454 struct wl_resource *resource, int transform)
1455{
1456 struct weston_surface *surface = resource->data;
1457
1458 surface->pending.buffer_transform = transform;
1459}
1460
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001461static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001462 surface_destroy,
1463 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001464 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001465 surface_frame,
1466 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001467 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001468 surface_commit,
1469 surface_set_buffer_transform
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001470};
1471
1472static void
1473compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001474 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001475{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001476 struct weston_compositor *ec = resource->data;
1477 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001478
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001479 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001480 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001481 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001482 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001483 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001484
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001485 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001486
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001487 surface->surface.resource.object.id = id;
1488 surface->surface.resource.object.interface = &wl_surface_interface;
1489 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001490 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001491 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001492
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001493 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001494}
1495
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001496static void
1497destroy_region(struct wl_resource *resource)
1498{
1499 struct weston_region *region =
1500 container_of(resource, struct weston_region, resource);
1501
1502 pixman_region32_fini(&region->region);
1503 free(region);
1504}
1505
1506static void
1507region_destroy(struct wl_client *client, struct wl_resource *resource)
1508{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001509 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001510}
1511
1512static void
1513region_add(struct wl_client *client, struct wl_resource *resource,
1514 int32_t x, int32_t y, int32_t width, int32_t height)
1515{
1516 struct weston_region *region = resource->data;
1517
1518 pixman_region32_union_rect(&region->region, &region->region,
1519 x, y, width, height);
1520}
1521
1522static void
1523region_subtract(struct wl_client *client, struct wl_resource *resource,
1524 int32_t x, int32_t y, int32_t width, int32_t height)
1525{
1526 struct weston_region *region = resource->data;
1527 pixman_region32_t rect;
1528
1529 pixman_region32_init_rect(&rect, x, y, width, height);
1530 pixman_region32_subtract(&region->region, &region->region, &rect);
1531 pixman_region32_fini(&rect);
1532}
1533
1534static const struct wl_region_interface region_interface = {
1535 region_destroy,
1536 region_add,
1537 region_subtract
1538};
1539
1540static void
1541compositor_create_region(struct wl_client *client,
1542 struct wl_resource *resource, uint32_t id)
1543{
1544 struct weston_region *region;
1545
1546 region = malloc(sizeof *region);
1547 if (region == NULL) {
1548 wl_resource_post_no_memory(resource);
1549 return;
1550 }
1551
1552 region->resource.destroy = destroy_region;
1553
1554 region->resource.object.id = id;
1555 region->resource.object.interface = &wl_region_interface;
1556 region->resource.object.implementation =
1557 (void (**)(void)) &region_interface;
1558 region->resource.data = region;
1559
1560 pixman_region32_init(&region->region);
1561
1562 wl_client_add_resource(client, &region->resource);
1563}
1564
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001565static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001566 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001567 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001568};
1569
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001570WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001571weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001572{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001573 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1574 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001575
1576 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001577 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001578}
1579
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001580static void
1581weston_compositor_dpms_on(struct weston_compositor *compositor)
1582{
1583 struct weston_output *output;
1584
1585 wl_list_for_each(output, &compositor->output_list, link)
1586 if (output->set_dpms)
1587 output->set_dpms(output, WESTON_DPMS_ON);
1588}
1589
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001590WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001591weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001592{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001593 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1594 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001595 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001596 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001597 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001598 }
1599}
1600
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001601static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001602weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001603{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001604 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001605 compositor->idle_inhibit++;
1606}
1607
1608static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001609weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001610{
1611 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001612 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001613}
1614
1615static int
1616idle_handler(void *data)
1617{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001618 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001619
1620 if (compositor->idle_inhibit)
1621 return 1;
1622
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001623 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001624
1625 return 1;
1626}
1627
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001628WL_EXPORT void
1629weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1630{
1631 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001632 pixman_region32_init(&plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001633 plane->x = x;
1634 plane->y = y;
1635}
1636
1637WL_EXPORT void
1638weston_plane_release(struct weston_plane *plane)
1639{
1640 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001641 pixman_region32_fini(&plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001642}
1643
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001644static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001645weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001646
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001647static void
Daniel Stone37816df2012-05-16 18:45:18 +01001648clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001649{
Daniel Stone37816df2012-05-16 18:45:18 +01001650 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001651 struct weston_output *output, *prev = NULL;
1652 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001653
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001654 x = wl_fixed_to_int(*fx);
1655 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001656 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1657 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001658
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001659 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001660 if (pixman_region32_contains_point(&output->region,
1661 x, y, NULL))
1662 valid = 1;
1663 if (pixman_region32_contains_point(&output->region,
1664 old_x, old_y, NULL))
1665 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001666 }
Daniel Stone37816df2012-05-16 18:45:18 +01001667
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001668 if (!valid) {
1669 if (x < prev->x)
1670 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001671 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001672 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001673 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001674 if (y < prev->y)
1675 *fy = wl_fixed_from_int(prev->y);
1676 else if (y >= prev->y + prev->current->height)
1677 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001678 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001679 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001680}
1681
1682WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001683notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001684{
1685 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001686 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001687 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001688 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001689 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001690
1691 weston_compositor_activity(ec);
1692
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001693 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001694
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001695 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001696
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001697 pointer->x = x;
1698 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001699
1700 ix = wl_fixed_to_int(x);
1701 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001702
Scott Moreauccbf29d2012-02-22 14:21:41 -07001703 wl_list_for_each(output, &ec->output_list, link)
1704 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001705 pixman_region32_contains_point(&output->region,
1706 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06001707 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001708
Daniel Stone37816df2012-05-16 18:45:18 +01001709 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001710 interface = pointer->grab->interface;
1711 interface->motion(pointer->grab, time,
1712 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001713
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001714 if (seat->sprite) {
1715 weston_surface_set_position(seat->sprite,
1716 ix - seat->hotspot_x,
1717 iy - seat->hotspot_y);
1718 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001719 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001720}
1721
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001722WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001723weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001724 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001725{
Daniel Stone37816df2012-05-16 18:45:18 +01001726 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001727
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001728 if (seat->seat.keyboard) {
1729 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1730 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04001731 }
1732
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001733 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001734}
1735
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001736WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001737notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001738 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001739{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001740 struct weston_compositor *compositor = seat->compositor;
1741 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001742 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001743 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001744 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001745
Daniel Stone4dbadb12012-05-30 16:31:51 +01001746 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001747 if (compositor->ping_handler && focus)
1748 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001749 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001750 if (pointer->button_count == 0) {
1751 pointer->grab_button = button;
1752 pointer->grab_time = time;
1753 pointer->grab_x = pointer->x;
1754 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001755 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001756 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001757 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001758 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001759 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001760 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001761
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001762 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001763 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001764
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001765 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001766
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001767 if (pointer->button_count == 1)
1768 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001769 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001770}
1771
Scott Moreau210d0792012-03-22 10:47:01 -06001772WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001773notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01001774 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001775{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001776 struct weston_compositor *compositor = seat->compositor;
1777 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001778 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001779 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001780 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1781
1782 if (compositor->ping_handler && focus)
1783 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001784
1785 weston_compositor_activity(compositor);
1786
Scott Moreau6a3633d2012-03-20 08:47:59 -06001787 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001788 weston_compositor_run_axis_binding(compositor, seat,
1789 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06001790 else
1791 return;
1792
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001793 if (pointer->focus_resource)
1794 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01001795 value);
Scott Moreau210d0792012-03-22 10:47:01 -06001796}
1797
Daniel Stone05d58682012-06-22 13:21:38 +01001798WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001799notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001800{
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001801 struct wl_keyboard *keyboard = &seat->keyboard.keyboard;
Daniel Stone05d58682012-06-22 13:21:38 +01001802 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01001803 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001804 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01001805 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01001806 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01001807
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001808 /* Serialize and update our internal state, checking to see if it's
1809 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01001810 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1811 XKB_STATE_DEPRESSED);
1812 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1813 XKB_STATE_LATCHED);
1814 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1815 XKB_STATE_LOCKED);
1816 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01001817 XKB_STATE_EFFECTIVE);
1818
Daniel Stone71c38772012-06-22 13:21:30 +01001819 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
1820 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
1821 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
1822 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01001823 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01001824
Daniel Stone71c38772012-06-22 13:21:30 +01001825 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
1826 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
1827 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
1828 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001829
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001830 /* And update the modifier_state for bindings. */
1831 mods_lookup = mods_depressed | mods_latched;
1832 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01001833 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001834 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01001835 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001836 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01001837 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001838 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04001839 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
1840 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01001841
Daniel Stone8c8164f2012-05-30 16:31:45 +01001842 /* Finally, notify the compositor that LEDs have changed. */
1843 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001844 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001845 leds |= LED_NUM_LOCK;
1846 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001847 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001848 leds |= LED_CAPS_LOCK;
1849 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001850 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001851 leds |= LED_SCROLL_LOCK;
1852 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01001853 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01001854 seat->xkb_state.leds = leds;
1855
Daniel Stone05d58682012-06-22 13:21:38 +01001856 if (changed) {
1857 grab->interface->modifiers(grab,
1858 serial,
1859 keyboard->modifiers.mods_depressed,
1860 keyboard->modifiers.mods_latched,
1861 keyboard->modifiers.mods_locked,
1862 keyboard->modifiers.group);
1863 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001864}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001865
Daniel Stone05d58682012-06-22 13:21:38 +01001866static void
1867update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001868 enum wl_keyboard_key_state state)
1869{
1870 enum xkb_key_direction direction;
1871
1872 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1873 direction = XKB_KEY_DOWN;
1874 else
1875 direction = XKB_KEY_UP;
1876
1877 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1878 * broken keycode system, which starts at 8. */
1879 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1880
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001881 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001882}
1883
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001884WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001885notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001886 enum wl_keyboard_key_state state,
1887 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001888{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001889 struct weston_compositor *compositor = seat->compositor;
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001890 struct weston_keyboard *keyboard = &seat->keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01001891 struct weston_surface *focus =
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001892 (struct weston_surface *) keyboard->keyboard.focus;
1893 struct wl_keyboard_grab *grab = keyboard->keyboard.grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001894 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001895 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001896
Daniel Stonec9785ea2012-05-30 16:31:52 +01001897 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001898 if (compositor->ping_handler && focus)
1899 compositor->ping_handler(focus, serial);
1900
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001901 weston_compositor_idle_inhibit(compositor);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001902 keyboard->keyboard.grab_key = key;
1903 keyboard->keyboard.grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07001904 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001905 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001906 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001907
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001908 end = keyboard->keyboard.keys.data + keyboard->keyboard.keys.size;
1909 for (k = keyboard->keyboard.keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01001910 if (*k == key) {
1911 /* Ignore server-generated repeats. */
1912 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1913 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001914 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01001915 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001916 }
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001917 keyboard->keyboard.keys.size = (void *) end - keyboard->keyboard.keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001918 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001919 k = wl_array_add(&keyboard->keyboard.keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001920 *k = key;
1921 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001922
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001923 if (grab == &keyboard->keyboard.default_grab ||
1924 grab == &keyboard->input_method_grab) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001925 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001926 state);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001927 grab = keyboard->keyboard.grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06001928 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001929
Daniel Stone7ace3902012-05-30 16:31:40 +01001930 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01001931
1932 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001933 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01001934 wl_display_get_serial(compositor->wl_display),
1935 key,
1936 state);
1937 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001938}
1939
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001940WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001941notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01001942 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001943{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001944 struct weston_compositor *compositor = seat->compositor;
1945 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001946
1947 if (output) {
Kristian Høgsberg00fbbe62012-10-23 13:04:09 -04001948 clip_pointer_motion(seat, &x, &y);
Daniel Stone37816df2012-05-16 18:45:18 +01001949 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001950 x - pointer->x,
1951 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001952
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001953 pointer->x = x;
1954 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001955 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001956 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001957 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001958 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03001959 /* FIXME: We should call wl_pointer_set_focus(seat,
1960 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001961 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001962}
1963
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001964static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001965destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001966{
Daniel Stone37816df2012-05-16 18:45:18 +01001967 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001968
Daniel Stone37816df2012-05-16 18:45:18 +01001969 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001970 saved_kbd_focus_listener);
1971
Daniel Stone37816df2012-05-16 18:45:18 +01001972 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001973}
1974
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001975WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001976notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001977 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001978{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001979 struct weston_compositor *compositor = seat->compositor;
1980 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001981 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001982 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001983
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001984 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001985 wl_array_copy(&keyboard->keys, keys);
1986 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001987 weston_compositor_idle_inhibit(compositor);
1988 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001989 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001990 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001991 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001992
Daniel Stoneef172672012-06-22 13:21:32 +01001993 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001994 wl_array_for_each(k, &keyboard->keys) {
1995 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01001996 WL_KEYBOARD_KEY_STATE_PRESSED);
1997 }
1998
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001999 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002000
2001 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002002 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2003 wl_keyboard_set_focus(keyboard, surface);
2004 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002005 }
2006}
2007
2008WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002009notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01002010{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002011 struct weston_compositor *compositor = seat->compositor;
2012 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002013 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002014
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002015 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002016 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002017 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002018 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002019 WL_KEYBOARD_KEY_STATE_RELEASED);
2020 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002021
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002022 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002023
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002024 if (keyboard->focus) {
2025 seat->saved_kbd_focus = keyboard->focus;
2026 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01002027 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002028 wl_signal_add(&keyboard->focus->resource.destroy_signal,
2029 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002030 }
2031
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002032 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002033 /* FIXME: We really need keyboard grab cancel here to
2034 * let the grab shut down properly. As it is we leak
2035 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002036 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002037}
2038
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002039static void
Daniel Stone37816df2012-05-16 18:45:18 +01002040touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002041{
Daniel Stone37816df2012-05-16 18:45:18 +01002042 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002043 struct wl_resource *resource;
2044
Pekka Paalanen56464252012-07-31 13:21:08 +03002045 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002046 return;
2047
Pekka Paalanen56464252012-07-31 13:21:08 +03002048 if (seat->touch->focus_resource)
2049 wl_list_remove(&seat->touch->focus_listener.link);
2050 seat->touch->focus = NULL;
2051 seat->touch->focus_resource = NULL;
2052
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002053 if (surface) {
2054 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01002055 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04002056 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002057 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02002058 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002059 return;
2060 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002061
Daniel Stone37816df2012-05-16 18:45:18 +01002062 seat->touch->focus = surface;
2063 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03002064 wl_signal_add(&resource->destroy_signal,
2065 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002066 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002067}
2068
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002069/**
2070 * notify_touch - emulates button touches and notifies surfaces accordingly.
2071 *
2072 * It assumes always the correct cycle sequence until it gets here: touch_down
2073 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2074 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002075 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002076 */
2077WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002078notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002079 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002080{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002081 struct weston_compositor *ec = seat->compositor;
2082 struct wl_touch *touch = seat->seat.touch;
Matt Roper47c1b982012-10-10 16:56:53 -07002083 struct wl_touch_grab *grab = touch->grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002084 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002085 wl_fixed_t sx, sy;
Matt Roper47c1b982012-10-10 16:56:53 -07002086
2087 /* Update grab's global coordinates. */
2088 touch->grab_x = x;
2089 touch->grab_y = y;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002090
2091 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01002092 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002093 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002094
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002095 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002096
2097 /* the first finger down picks the surface, and all further go
2098 * to that surface for the remainder of the touch session i.e.
2099 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002100 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002101 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002102 touch_set_focus(seat, &es->surface);
2103 } else if (touch->focus) {
2104 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002105 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Rob Bradford26e009c2012-12-05 18:47:05 +00002106 } else {
2107 /* Unexpected condition: We have non-initial touch but
2108 * there is no focused surface.
2109 */
2110 weston_log("touch event received with %d points down"
2111 "but no surface focused\n", seat->num_tp);
2112 return;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002113 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002114
Matt Roper47c1b982012-10-10 16:56:53 -07002115 grab->interface->down(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002116 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002117 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002118 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002119 if (!es)
2120 break;
2121
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002122 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Matt Roper47c1b982012-10-10 16:56:53 -07002123 grab->interface->motion(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002124 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002125 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002126 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002127 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002128
Matt Roper47c1b982012-10-10 16:56:53 -07002129 grab->interface->up(grab, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002130 if (seat->num_tp == 0)
2131 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002132 break;
2133 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002134}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002135
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04002136static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002137pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
2138{
2139 struct weston_seat *seat = container_of(listener, struct weston_seat,
2140 sprite_destroy_listener);
2141
2142 seat->sprite = NULL;
2143}
2144
2145static void
2146pointer_cursor_surface_configure(struct weston_surface *es,
2147 int32_t dx, int32_t dy)
2148{
2149 struct weston_seat *seat = es->private;
2150 int x, y;
2151
2152 assert(es == seat->sprite);
2153
2154 seat->hotspot_x -= dx;
2155 seat->hotspot_y -= dy;
2156
2157 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
2158 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
2159
2160 weston_surface_configure(seat->sprite, x, y,
Pekka Paalanende685b82012-12-04 15:58:12 +02002161 es->buffer_ref.buffer->width,
2162 es->buffer_ref.buffer->height);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002163
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002164 empty_region(&es->pending.input);
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03002165
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002166 if (!weston_surface_is_mapped(es)) {
2167 wl_list_insert(&es->compositor->cursor_layer.surface_list,
2168 &es->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002169 weston_surface_update_transform(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002170 }
2171}
2172
2173static void
2174pointer_unmap_sprite(struct weston_seat *seat)
2175{
2176 if (weston_surface_is_mapped(seat->sprite))
2177 weston_surface_unmap(seat->sprite);
2178
2179 wl_list_remove(&seat->sprite_destroy_listener.link);
2180 seat->sprite->configure = NULL;
2181 seat->sprite->private = NULL;
2182 seat->sprite = NULL;
2183}
2184
2185static void
2186pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2187 uint32_t serial, struct wl_resource *surface_resource,
2188 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002189{
Daniel Stone37816df2012-05-16 18:45:18 +01002190 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002191 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002192
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002193 if (surface_resource)
Pekka Paalanen6c71ee12012-10-10 12:49:30 +03002194 surface = surface_resource->data;
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002195
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04002196 if (seat->seat.pointer->focus == NULL)
2197 return;
2198 if (seat->seat.pointer->focus->resource.client != client)
2199 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002200 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002201 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002202
2203 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002204 if (surface->configure) {
2205 wl_resource_post_error(&surface->surface.resource,
2206 WL_DISPLAY_ERROR_INVALID_OBJECT,
2207 "surface->configure already "
2208 "set");
2209 return;
2210 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002211 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002212
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002213 if (seat->sprite)
2214 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002215
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002216 if (!surface)
2217 return;
2218
2219 wl_signal_add(&surface->surface.resource.destroy_signal,
2220 &seat->sprite_destroy_listener);
2221
2222 surface->configure = pointer_cursor_surface_configure;
2223 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002224 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002225 seat->hotspot_x = x;
2226 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002227
Pekka Paalanende685b82012-12-04 15:58:12 +02002228 if (surface->buffer_ref.buffer)
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04002229 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002230}
2231
Daniel Stone37816df2012-05-16 18:45:18 +01002232static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002233 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002234};
2235
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002236static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002237handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002238{
Daniel Stone37816df2012-05-16 18:45:18 +01002239 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002240
Daniel Stone37816df2012-05-16 18:45:18 +01002241 seat = container_of(listener, struct weston_seat,
2242 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002243
Daniel Stone37816df2012-05-16 18:45:18 +01002244 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002245}
2246
Casey Dahlin9074db52012-04-19 22:50:09 -04002247static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002248{
2249 wl_list_remove(&resource->link);
2250 free(resource);
2251}
2252
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002253static void
Daniel Stone37816df2012-05-16 18:45:18 +01002254seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2255 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002256{
Daniel Stone37816df2012-05-16 18:45:18 +01002257 struct weston_seat *seat = resource->data;
2258 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002259
Daniel Stone37816df2012-05-16 18:45:18 +01002260 if (!seat->seat.pointer)
2261 return;
2262
2263 cr = wl_client_add_object(client, &wl_pointer_interface,
2264 &pointer_interface, id, seat);
2265 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002266 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002267
2268 if (seat->seat.pointer->focus &&
2269 seat->seat.pointer->focus->resource.client == client) {
2270 struct weston_surface *surface;
2271 wl_fixed_t sx, sy;
2272
2273 surface = (struct weston_surface *) seat->seat.pointer->focus;
2274 weston_surface_from_global_fixed(surface,
2275 seat->seat.pointer->x,
2276 seat->seat.pointer->y,
2277 &sx,
2278 &sy);
2279 wl_pointer_set_focus(seat->seat.pointer,
2280 seat->seat.pointer->focus,
2281 sx,
2282 sy);
2283 }
Daniel Stone37816df2012-05-16 18:45:18 +01002284}
2285
2286static void
2287seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2288 uint32_t id)
2289{
2290 struct weston_seat *seat = resource->data;
2291 struct wl_resource *cr;
2292
2293 if (!seat->seat.keyboard)
2294 return;
2295
2296 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2297 seat);
2298 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002299 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002300
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002301 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2302 seat->xkb_info.keymap_fd,
2303 seat->xkb_info.keymap_size);
2304
Daniel Stone17b13bd2012-05-30 16:31:39 +01002305 if (seat->seat.keyboard->focus &&
2306 seat->seat.keyboard->focus->resource.client == client) {
2307 wl_keyboard_set_focus(seat->seat.keyboard,
2308 seat->seat.keyboard->focus);
2309 }
Daniel Stone37816df2012-05-16 18:45:18 +01002310}
2311
2312static void
2313seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2314 uint32_t id)
2315{
2316 struct weston_seat *seat = resource->data;
2317 struct wl_resource *cr;
2318
2319 if (!seat->seat.touch)
2320 return;
2321
2322 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2323 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002324 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002325}
2326
2327static const struct wl_seat_interface seat_interface = {
2328 seat_get_pointer,
2329 seat_get_keyboard,
2330 seat_get_touch,
2331};
2332
2333static void
2334bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2335{
2336 struct wl_seat *seat = data;
2337 struct wl_resource *resource;
2338 enum wl_seat_capability caps = 0;
2339
2340 resource = wl_client_add_object(client, &wl_seat_interface,
2341 &seat_interface, id, data);
2342 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002343 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002344
2345 if (seat->pointer)
2346 caps |= WL_SEAT_CAPABILITY_POINTER;
2347 if (seat->keyboard)
2348 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2349 if (seat->touch)
2350 caps |= WL_SEAT_CAPABILITY_TOUCH;
2351
2352 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002353}
2354
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002355static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002356device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002357{
Daniel Stone37816df2012-05-16 18:45:18 +01002358 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002359
Daniel Stone37816df2012-05-16 18:45:18 +01002360 seat = container_of(listener, struct weston_seat,
2361 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002362
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002363 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002364}
2365
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002366static int
2367weston_compositor_xkb_init(struct weston_compositor *ec,
2368 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002369{
Daniel Stonee379da92012-05-30 16:32:04 +01002370 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002371 ec->xkb_context = xkb_context_new(0);
2372 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002373 weston_log("failed to create XKB context\n");
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002374 return -1;
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002375 }
Daniel Stone994679a2012-05-30 16:31:43 +01002376 }
2377
2378 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002379 ec->xkb_names = *names;
2380 if (!ec->xkb_names.rules)
2381 ec->xkb_names.rules = strdup("evdev");
2382 if (!ec->xkb_names.model)
2383 ec->xkb_names.model = strdup("pc105");
2384 if (!ec->xkb_names.layout)
2385 ec->xkb_names.layout = strdup("us");
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002386
2387 return 0;
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002388}
2389
Daniel Stonee379da92012-05-30 16:32:04 +01002390static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2391{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002392 if (xkb_info->keymap)
2393 xkb_map_unref(xkb_info->keymap);
2394
2395 if (xkb_info->keymap_area)
2396 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2397 if (xkb_info->keymap_fd >= 0)
2398 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002399}
2400
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002401static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2402{
Daniel Stonee379da92012-05-30 16:32:04 +01002403 free((char *) ec->xkb_names.rules);
2404 free((char *) ec->xkb_names.model);
2405 free((char *) ec->xkb_names.layout);
2406 free((char *) ec->xkb_names.variant);
2407 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002408
Daniel Stonee379da92012-05-30 16:32:04 +01002409 xkb_info_destroy(&ec->xkb_info);
2410 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002411}
2412
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002413static int
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002414weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002415{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002416 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002417
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002418 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2419 XKB_MOD_NAME_SHIFT);
2420 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2421 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002422 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2423 XKB_MOD_NAME_CTRL);
2424 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2425 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002426 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2427 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002428 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2429 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002430 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002431
2432 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2433 XKB_LED_NAME_NUM);
2434 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2435 XKB_LED_NAME_CAPS);
2436 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2437 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002438
2439 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2440 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002441 weston_log("failed to get string version of keymap\n");
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002442 return -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002443 }
2444 xkb_info->keymap_size = strlen(keymap_str) + 1;
2445
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002446 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002447 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002448 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002449 (unsigned long) xkb_info->keymap_size);
2450 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002451 }
2452
2453 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2454 PROT_READ | PROT_WRITE,
2455 MAP_SHARED, xkb_info->keymap_fd, 0);
2456 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002457 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002458 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002459 goto err_dev_zero;
2460 }
2461 strcpy(xkb_info->keymap_area, keymap_str);
2462 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002463
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002464 return 0;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002465
2466err_dev_zero:
2467 close(xkb_info->keymap_fd);
2468 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002469err_keymap_str:
2470 free(keymap_str);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002471 return -1;
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002472}
2473
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002474static int
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002475weston_compositor_build_global_keymap(struct weston_compositor *ec)
2476{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002477 if (ec->xkb_info.keymap != NULL)
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002478 return 0;
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002479
Daniel Stonee379da92012-05-30 16:32:04 +01002480 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002481 &ec->xkb_names,
2482 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002483 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002484 weston_log("failed to compile global XKB keymap\n");
2485 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002486 "options %s\n",
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002487 ec->xkb_names.rules, ec->xkb_names.model,
2488 ec->xkb_names.layout, ec->xkb_names.variant,
2489 ec->xkb_names.options);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002490 return -1;
Daniel Stone994679a2012-05-30 16:31:43 +01002491 }
2492
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002493 if (weston_xkb_info_new_keymap(&ec->xkb_info) < 0)
2494 return -1;
2495
2496 return 0;
Daniel Stone994679a2012-05-30 16:31:43 +01002497}
2498
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002499WL_EXPORT int
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002500weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002501{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002502 if (seat->has_keyboard)
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002503 return 0;
Daniel Stone994679a2012-05-30 16:31:43 +01002504
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002505 if (keymap != NULL) {
2506 seat->xkb_info.keymap = xkb_map_ref(keymap);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002507 if (weston_xkb_info_new_keymap(&seat->xkb_info) < 0)
2508 return -1;
2509 } else {
2510 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
2511 return -1;
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002512 seat->xkb_info = seat->compositor->xkb_info;
2513 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2514 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002515
2516 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2517 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002518 weston_log("failed to initialise XKB state\n");
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002519 return -1;
Daniel Stoned65b9092012-05-30 16:32:05 +01002520 }
2521
Daniel Stone71c38772012-06-22 13:21:30 +01002522 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002523
Jan Arne Petersena75a7892013-01-16 21:26:50 +01002524 wl_keyboard_init(&seat->keyboard.keyboard);
2525 wl_seat_set_keyboard(&seat->seat, &seat->keyboard.keyboard);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002526
2527 seat->has_keyboard = 1;
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002528
2529 return 0;
Daniel Stone994679a2012-05-30 16:31:43 +01002530}
2531
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002532WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002533weston_seat_init_pointer(struct weston_seat *seat)
2534{
2535 if (seat->has_pointer)
2536 return;
2537
2538 wl_pointer_init(&seat->pointer);
2539 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2540
2541 seat->has_pointer = 1;
2542}
2543
2544WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002545weston_seat_init_touch(struct weston_seat *seat)
2546{
2547 if (seat->has_touch)
2548 return;
2549
2550 wl_touch_init(&seat->touch);
2551 wl_seat_set_touch(&seat->seat, &seat->touch);
2552
2553 seat->has_touch = 1;
2554}
2555
2556WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002557weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002558{
Daniel Stone37816df2012-05-16 18:45:18 +01002559 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002560 seat->has_pointer = 0;
2561 seat->has_keyboard = 0;
2562 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002563
Daniel Stone37816df2012-05-16 18:45:18 +01002564 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2565 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002566
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002567 seat->sprite = NULL;
2568 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002569
Daniel Stone37816df2012-05-16 18:45:18 +01002570 seat->compositor = ec;
2571 seat->hotspot_x = 16;
2572 seat->hotspot_y = 16;
2573 seat->modifier_state = 0;
2574 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002575
Daniel Stone37816df2012-05-16 18:45:18 +01002576 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002577 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002578
Daniel Stone37816df2012-05-16 18:45:18 +01002579 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002580
Daniel Stone37816df2012-05-16 18:45:18 +01002581 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2582 wl_signal_add(&seat->seat.drag_icon_signal,
2583 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002584
2585 clipboard_create(seat);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002586 wl_signal_emit(&ec->seat_created_signal, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002587}
2588
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002589WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002590weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002591{
Daniel Stone37816df2012-05-16 18:45:18 +01002592 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002593 /* The global object is destroyed at wl_display_destroy() time. */
2594
Daniel Stone37816df2012-05-16 18:45:18 +01002595 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002596 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002597
Daniel Stone74419a22012-05-30 16:32:02 +01002598 if (seat->xkb_state.state != NULL)
2599 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01002600 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002601
Daniel Stone37816df2012-05-16 18:45:18 +01002602 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002603}
2604
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002605static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002606drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2607{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002608 empty_region(&es->pending.input);
2609
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002610 weston_surface_configure(es,
2611 es->geometry.x + sx, es->geometry.y + sy,
Pekka Paalanende685b82012-12-04 15:58:12 +02002612 es->buffer_ref.buffer->width,
2613 es->buffer_ref.buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002614}
2615
2616static int
Daniel Stone37816df2012-05-16 18:45:18 +01002617device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002618 struct weston_surface *surface)
2619{
Daniel Stone37816df2012-05-16 18:45:18 +01002620 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002621
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002622 if (surface->configure) {
2623 wl_resource_post_error(&surface->surface.resource,
2624 WL_DISPLAY_ERROR_INVALID_OBJECT,
2625 "surface->configure already set");
2626 return 0;
2627 }
2628
Daniel Stone37816df2012-05-16 18:45:18 +01002629 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002630
Daniel Stone37816df2012-05-16 18:45:18 +01002631 weston_surface_set_position(ws->drag_surface,
2632 wl_fixed_to_double(seat->pointer->x),
2633 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002634
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002635 surface->configure = drag_surface_configure;
2636
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002637 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002638 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002639
2640 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002641}
2642
2643static void
Daniel Stone37816df2012-05-16 18:45:18 +01002644device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002645{
Ander Conselvan de Oliveira5fd55802012-10-11 14:06:19 +03002646 if (weston_surface_is_mapped(seat->drag_surface))
2647 weston_surface_unmap(seat->drag_surface);
2648
Daniel Stone37816df2012-05-16 18:45:18 +01002649 seat->drag_surface->configure = NULL;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002650 empty_region(&seat->drag_surface->pending.input);
Daniel Stone37816df2012-05-16 18:45:18 +01002651 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2652 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002653}
2654
2655static void
Daniel Stone37816df2012-05-16 18:45:18 +01002656device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002657{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002658 struct wl_list *list;
2659
Daniel Stone37816df2012-05-16 18:45:18 +01002660 if (weston_surface_is_mapped(seat->drag_surface) ||
Pekka Paalanende685b82012-12-04 15:58:12 +02002661 !seat->drag_surface->buffer_ref.buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002662 return;
2663
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002664 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2665 list = &seat->sprite->layer_link;
2666 else
2667 list = &seat->compositor->cursor_layer.surface_list;
2668
2669 wl_list_insert(list, &seat->drag_surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002670 weston_surface_update_transform(seat->drag_surface);
Daniel Stone37816df2012-05-16 18:45:18 +01002671 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002672}
2673
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002674static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002675weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01002676 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002677{
2678 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002679
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002680 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002681 return;
2682
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002683 if (seat->drag_surface && seat->seat.drag_surface &&
2684 (&seat->drag_surface->surface.resource !=
2685 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002686 /* between calls to this funcion we got a new drag_surface */
2687 surface_changed = 1;
2688
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002689 if (!seat->seat.drag_surface || surface_changed) {
2690 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002691 if (!surface_changed)
2692 return;
2693 }
2694
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002695 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002696 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002697 seat->seat.drag_surface;
2698 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002699 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002700 }
2701
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002702 /* the client may not have attached a buffer to the drag surface
2703 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002704 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002705
2706 if (!dx && !dy)
2707 return;
2708
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002709 weston_surface_set_position(seat->drag_surface,
2710 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
2711 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002712}
2713
2714WL_EXPORT void
2715weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2716{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002717 struct weston_seat *seat;
2718
2719 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002720 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002721}
2722
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002723static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002724bind_output(struct wl_client *client,
2725 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002726{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002727 struct weston_output *output = data;
2728 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002729 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002730
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002731 resource = wl_client_add_object(client,
2732 &wl_output_interface, NULL, id, data);
2733
Casey Dahlin9074db52012-04-19 22:50:09 -04002734 wl_list_insert(&output->resource_list, &resource->link);
2735 resource->destroy = unbind_resource;
2736
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002737 wl_output_send_geometry(resource,
2738 output->x,
2739 output->y,
2740 output->mm_width,
2741 output->mm_height,
2742 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002743 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002744 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002745
2746 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002747 wl_output_send_mode(resource,
2748 mode->flags,
2749 mode->width,
2750 mode->height,
2751 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002752 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002753}
2754
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002755WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002756weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002757{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002758 struct weston_compositor *c = output->compositor;
2759
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002760 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002761 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002762 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002763
2764 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002765}
2766
Scott Moreau1bad5db2012-08-18 01:04:05 -06002767static void
2768weston_output_compute_transform(struct weston_output *output)
2769{
2770 struct weston_matrix transform;
2771 int flip;
2772
2773 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002774 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002775
2776 switch(output->transform) {
2777 case WL_OUTPUT_TRANSFORM_FLIPPED:
2778 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2779 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2780 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002781 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002782 flip = -1;
2783 break;
2784 default:
2785 flip = 1;
2786 break;
2787 }
2788
2789 switch(output->transform) {
2790 case WL_OUTPUT_TRANSFORM_NORMAL:
2791 case WL_OUTPUT_TRANSFORM_FLIPPED:
2792 transform.d[0] = flip;
2793 transform.d[1] = 0;
2794 transform.d[4] = 0;
2795 transform.d[5] = 1;
2796 break;
2797 case WL_OUTPUT_TRANSFORM_90:
2798 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2799 transform.d[0] = 0;
2800 transform.d[1] = -flip;
2801 transform.d[4] = 1;
2802 transform.d[5] = 0;
2803 break;
2804 case WL_OUTPUT_TRANSFORM_180:
2805 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2806 transform.d[0] = -flip;
2807 transform.d[1] = 0;
2808 transform.d[4] = 0;
2809 transform.d[5] = -1;
2810 break;
2811 case WL_OUTPUT_TRANSFORM_270:
2812 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2813 transform.d[0] = 0;
2814 transform.d[1] = flip;
2815 transform.d[4] = -1;
2816 transform.d[5] = 0;
2817 break;
2818 default:
2819 break;
2820 }
2821
2822 weston_matrix_multiply(&output->matrix, &transform);
2823}
2824
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002825WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002826weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002827{
Scott Moreau850ca422012-05-21 15:21:25 -06002828 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002829 struct weston_matrix camera;
2830 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002831
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002832 weston_matrix_init(&output->matrix);
2833 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002834 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2835 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002836
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002837 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002838 2.0 / (output->width + output->border.left + output->border.right),
2839 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2840
2841 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002842
Scott Moreauccbf29d2012-02-22 14:21:41 -07002843 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002844 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002845 weston_matrix_init(&camera);
2846 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002847 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002848 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002849 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002850 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002851 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002852 weston_matrix_multiply(&output->matrix, &modelview);
2853 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002854
Scott Moreauccbf29d2012-02-22 14:21:41 -07002855 output->dirty = 0;
2856}
2857
Scott Moreau1bad5db2012-08-18 01:04:05 -06002858static void
2859weston_output_transform_init(struct weston_output *output, uint32_t transform)
2860{
2861 output->transform = transform;
2862
2863 switch (transform) {
2864 case WL_OUTPUT_TRANSFORM_90:
2865 case WL_OUTPUT_TRANSFORM_270:
2866 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2867 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2868 /* Swap width and height */
2869 output->width = output->current->height;
2870 output->height = output->current->width;
2871 break;
2872 case WL_OUTPUT_TRANSFORM_NORMAL:
2873 case WL_OUTPUT_TRANSFORM_180:
2874 case WL_OUTPUT_TRANSFORM_FLIPPED:
2875 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2876 output->width = output->current->width;
2877 output->height = output->current->height;
2878 break;
2879 default:
2880 break;
2881 }
2882}
2883
Scott Moreauccbf29d2012-02-22 14:21:41 -07002884WL_EXPORT void
2885weston_output_move(struct weston_output *output, int x, int y)
2886{
2887 output->x = x;
2888 output->y = y;
2889
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002890 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002891 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002892 output->width,
2893 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002894}
2895
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002896WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002897weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002898 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002899{
2900 output->compositor = c;
2901 output->x = x;
2902 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002903 output->border.top = 0;
2904 output->border.bottom = 0;
2905 output->border.left = 0;
2906 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002907 output->mm_width = width;
2908 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002909 output->dirty = 1;
2910
Scott Moreau1bad5db2012-08-18 01:04:05 -06002911 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06002912 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002913
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002914 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002915 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002916
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002917 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002918 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002919 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002920
Casey Dahlin58ba1372012-04-19 22:50:08 -04002921 output->id = ffs(~output->compositor->output_id_pool) - 1;
2922 output->compositor->output_id_pool |= 1 << output->id;
2923
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002924 output->global =
2925 wl_display_add_global(c->wl_display, &wl_output_interface,
2926 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002927}
2928
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002929static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002930compositor_bind(struct wl_client *client,
2931 void *data, uint32_t version, uint32_t id)
2932{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002933 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002934
2935 wl_client_add_object(client, &wl_compositor_interface,
2936 &compositor_interface, id, compositor);
2937}
2938
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002939static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002940log_uname(void)
2941{
2942 struct utsname usys;
2943
2944 uname(&usys);
2945
2946 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2947 usys.version, usys.machine);
2948}
2949
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002950WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002951weston_compositor_init(struct weston_compositor *ec,
2952 struct wl_display *display,
2953 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04002954 char *argv[],
2955 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002956{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002957 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002958 struct xkb_rule_names xkb_names;
2959 const struct config_key keyboard_config_keys[] = {
2960 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2961 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2962 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2963 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2964 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2965 };
2966 const struct config_section cs[] = {
2967 { "keyboard",
2968 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
2969 };
2970
2971 memset(&xkb_names, 0, sizeof(xkb_names));
2972 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002973
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002974 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002975 wl_signal_init(&ec->destroy_signal);
2976 wl_signal_init(&ec->activate_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002977 wl_signal_init(&ec->kill_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002978 wl_signal_init(&ec->lock_signal);
2979 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002980 wl_signal_init(&ec->show_input_panel_signal);
2981 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002982 wl_signal_init(&ec->seat_created_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002983 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002984
Casey Dahlin58ba1372012-04-19 22:50:08 -04002985 ec->output_id_pool = 0;
2986
Kristian Høgsberga8873122011-11-23 10:39:34 -05002987 if (!wl_display_add_global(display, &wl_compositor_interface,
2988 ec, compositor_bind))
2989 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002990
Daniel Stone725c2c32012-06-22 14:04:36 +01002991 wl_list_init(&ec->surface_list);
2992 wl_list_init(&ec->layer_list);
2993 wl_list_init(&ec->seat_list);
2994 wl_list_init(&ec->output_list);
2995 wl_list_init(&ec->key_binding_list);
2996 wl_list_init(&ec->button_binding_list);
2997 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002998 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002999 wl_list_init(&ec->fade.animation.link);
3000
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003001 weston_plane_init(&ec->primary_plane, 0, 0);
3002
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003003 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3004 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003005
3006 ec->ping_handler = NULL;
3007
3008 screenshooter_create(ec);
3009 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003010 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003011
3012 wl_data_device_manager_init(ec->wl_display);
3013
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003014 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003015
Daniel Stone725c2c32012-06-22 14:04:36 +01003016 loop = wl_display_get_event_loop(ec->wl_display);
3017 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3018 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3019
3020 ec->input_loop = wl_event_loop_create();
3021
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003022 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
3023 ec->fade.animation.frame = fade_frame;
3024
3025 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3026 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3027
3028 weston_compositor_schedule_repaint(ec);
3029
Daniel Stone725c2c32012-06-22 14:04:36 +01003030 return 0;
3031}
3032
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003033WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003034weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003035{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003036 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003037
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003038 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003039 if (ec->input_loop_source)
3040 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003041
Matt Roper361d2ad2011-08-29 13:52:23 -07003042 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003043 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003044 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003045
Daniel Stone325fc2d2012-05-30 16:31:58 +01003046 weston_binding_list_destroy_all(&ec->key_binding_list);
3047 weston_binding_list_destroy_all(&ec->button_binding_list);
3048 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003049 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003050
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003051 weston_plane_release(&ec->primary_plane);
3052
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003053 wl_array_release(&ec->vertices);
3054 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05003055 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003056
3057 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07003058}
3059
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003060WL_EXPORT void
3061weston_version(int *major, int *minor, int *micro)
3062{
3063 *major = WESTON_VERSION_MAJOR;
3064 *minor = WESTON_VERSION_MINOR;
3065 *micro = WESTON_VERSION_MICRO;
3066}
3067
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003068static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003069{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003070 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003071
Martin Minarik6d118362012-06-07 18:01:59 +02003072 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003073 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003074
3075 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003076}
3077
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003078#ifdef HAVE_LIBUNWIND
3079
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003080static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003081print_backtrace(void)
3082{
3083 unw_cursor_t cursor;
3084 unw_context_t context;
3085 unw_word_t off;
3086 unw_proc_info_t pip;
3087 int ret, i = 0;
3088 char procname[256];
3089 const char *filename;
3090 Dl_info dlinfo;
3091
3092 pip.unwind_info = NULL;
3093 ret = unw_getcontext(&context);
3094 if (ret) {
3095 weston_log("unw_getcontext: %d\n", ret);
3096 return;
3097 }
3098
3099 ret = unw_init_local(&cursor, &context);
3100 if (ret) {
3101 weston_log("unw_init_local: %d\n", ret);
3102 return;
3103 }
3104
3105 ret = unw_step(&cursor);
3106 while (ret > 0) {
3107 ret = unw_get_proc_info(&cursor, &pip);
3108 if (ret) {
3109 weston_log("unw_get_proc_info: %d\n", ret);
3110 break;
3111 }
3112
3113 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3114 if (ret && ret != -UNW_ENOMEM) {
3115 if (ret != -UNW_EUNSPEC)
3116 weston_log("unw_get_proc_name: %d\n", ret);
3117 procname[0] = '?';
3118 procname[1] = 0;
3119 }
3120
3121 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3122 *dlinfo.dli_fname)
3123 filename = dlinfo.dli_fname;
3124 else
3125 filename = "?";
3126
3127 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3128 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3129
3130 ret = unw_step(&cursor);
3131 if (ret < 0)
3132 weston_log("unw_step: %d\n", ret);
3133 }
3134}
3135
3136#else
3137
3138static void
3139print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003140{
3141 void *buffer[32];
3142 int i, count;
3143 Dl_info info;
3144
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003145 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3146 for (i = 0; i < count; i++) {
3147 dladdr(buffer[i], &info);
3148 weston_log(" [%016lx] %s (%s)\n",
3149 (long) buffer[i],
3150 info.dli_sname ? info.dli_sname : "--",
3151 info.dli_fname);
3152 }
3153}
3154
3155#endif
3156
3157static void
3158on_segv_signal(int s, siginfo_t *siginfo, void *context)
3159{
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003160 /* This SIGSEGV handler will do a best-effort backtrace, and
3161 * then call the backend restore function, which will switch
3162 * back to the vt we launched from or ungrab X etc and then
3163 * raise SIGTRAP. If we run weston under gdb from X or a
3164 * different vt, and tell gdb "handle SIGSEGV nostop", this
3165 * will allow weston to switch back to gdb on crash and then
3166 * gdb will catch the crash with SIGTRAP. */
3167
Martin Minarik6d118362012-06-07 18:01:59 +02003168 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003169
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003170 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003171
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003172 segv_compositor->restore(segv_compositor);
3173
3174 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003175}
3176
3177
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003178static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003179load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003180{
3181 char path[PATH_MAX];
3182 void *module, *init;
3183
3184 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003185 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003186 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003187 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003188
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003189 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3190 if (module) {
3191 weston_log("Module '%s' already loaded\n", path);
3192 dlclose(module);
3193 return NULL;
3194 }
3195
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003196 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003197 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003198 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003199 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003200 return NULL;
3201 }
3202
3203 init = dlsym(module, entrypoint);
3204 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003205 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003206 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003207 return NULL;
3208 }
3209
3210 return init;
3211}
3212
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003213static int
3214load_modules(struct weston_compositor *ec, const char *modules)
3215{
3216 const char *p, *end;
3217 char buffer[256];
3218 int (*module_init)(struct weston_compositor *ec);
3219
3220 if (modules == NULL)
3221 return 0;
3222
3223 p = modules;
3224 while (*p) {
3225 end = strchrnul(p, ',');
3226 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
3227 module_init = load_module(buffer, "module_init");
3228 if (module_init)
3229 module_init(ec);
3230 p = end;
3231 while (*p == ',')
3232 p++;
3233
3234 }
3235
3236 return 0;
3237}
3238
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003239static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003240 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3241
3242static const char xdg_wrong_message[] =
3243 "fatal: environment variable XDG_RUNTIME_DIR\n"
3244 "is set to \"%s\", which is not a directory.\n";
3245
3246static const char xdg_wrong_mode_message[] =
3247 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3248 "correctly. Unix access mode must be 0700 but is %o,\n"
3249 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003250 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003251
3252static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003253 "Refer to your distribution on how to get it, or\n"
3254 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3255 "on how to implement it.\n";
3256
Martin Minarik37032f82012-06-18 20:15:18 +02003257static void
3258verify_xdg_runtime_dir(void)
3259{
3260 char *dir = getenv("XDG_RUNTIME_DIR");
3261 struct stat s;
3262
3263 if (!dir) {
3264 weston_log(xdg_error_message);
3265 weston_log_continue(xdg_detail_message);
3266 exit(EXIT_FAILURE);
3267 }
3268
3269 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3270 weston_log(xdg_wrong_message, dir);
3271 weston_log_continue(xdg_detail_message);
3272 exit(EXIT_FAILURE);
3273 }
3274
3275 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3276 weston_log(xdg_wrong_mode_message,
3277 dir, s.st_mode & 0777, s.st_uid);
3278 weston_log_continue(xdg_detail_message);
3279 }
3280}
3281
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003282static int
3283usage(int error_code)
3284{
3285 fprintf(stderr,
3286 "Usage: weston [OPTIONS]\n\n"
3287 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3288 "Weston supports multiple backends, and depending on which backend is in use\n"
3289 "different options will be accepted.\n\n"
3290
3291
3292 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003293 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003294 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3295 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3296 " -S, --socket=NAME\tName of socket to listen on\n"
3297 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003298 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003299 " --log==FILE\t\tLog to the given file\n"
3300 " -h, --help\t\tThis help message\n\n");
3301
3302 fprintf(stderr,
3303 "Options for drm-backend.so:\n\n"
3304 " --connector=ID\tBring up only this connector\n"
3305 " --seat=SEAT\t\tThe seat that weston should run on\n"
3306 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003307 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003308 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3309
3310 fprintf(stderr,
3311 "Options for x11-backend.so:\n\n"
3312 " --width=WIDTH\t\tWidth of X window\n"
3313 " --height=HEIGHT\tHeight of X window\n"
3314 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003315 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003316 " --output-count=COUNT\tCreate multiple outputs\n"
3317 " --no-input\t\tDont create input devices\n\n");
3318
3319 fprintf(stderr,
3320 "Options for wayland-backend.so:\n\n"
3321 " --width=WIDTH\t\tWidth of Wayland surface\n"
3322 " --height=HEIGHT\tHeight of Wayland surface\n"
3323 " --display=DISPLAY\tWayland display to connect to\n\n");
3324
3325 exit(error_code);
3326}
3327
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003328int main(int argc, char *argv[])
3329{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003330 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003331 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003332 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003333 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003334 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003335 struct sigaction segv_action;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003336 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003337 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003338 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003339 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003340 char *backend = NULL;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003341 const char *modules = "desktop-shell.so", *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003342 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003343 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003344 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003345 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003346 int32_t version = 0;
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003347 char *config_file;
3348
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003349 const struct config_key core_config_keys[] = {
3350 { "modules", CONFIG_KEY_STRING, &modules },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003351 };
3352
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003353 const struct config_section cs[] = {
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003354 { "core",
3355 core_config_keys, ARRAY_LENGTH(core_config_keys) },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003356 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003357
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003358 const struct weston_option core_options[] = {
3359 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3360 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3361 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003362 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003363 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003364 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003365 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003366 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003367
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003368 argc = parse_options(core_options,
3369 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003370
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003371 if (help)
3372 usage(EXIT_SUCCESS);
3373
Scott Moreau12245142012-08-29 15:15:58 -06003374 if (version) {
3375 printf(PACKAGE_STRING "\n");
3376 return EXIT_SUCCESS;
3377 }
3378
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003379 weston_log_file_open(log);
3380
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003381 weston_log("%s\n"
3382 STAMP_SPACE "%s\n"
3383 STAMP_SPACE "Bug reports to: %s\n"
3384 STAMP_SPACE "Build: %s\n",
3385 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003386 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003387 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003388
Martin Minarik37032f82012-06-18 20:15:18 +02003389 verify_xdg_runtime_dir();
3390
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003391 display = wl_display_create();
3392
Tiago Vignatti2116b892011-08-08 05:52:59 -07003393 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003394 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3395 display);
3396 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3397 display);
3398 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3399 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003400
3401 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003402 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3403 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003404
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003405 if (!backend) {
3406 if (getenv("WAYLAND_DISPLAY"))
3407 backend = "wayland-backend.so";
3408 else if (getenv("DISPLAY"))
3409 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003410 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003411 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003412 }
3413
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003414 config_file = config_file_path("weston.ini");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003415 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003416
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003417 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003418 if (!backend_init)
3419 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003420
Daniel Stonec1be8e52012-06-01 11:14:02 -04003421 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003422 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003423 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003424 exit(EXIT_FAILURE);
3425 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003426
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003427 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3428 segv_action.sa_sigaction = on_segv_signal;
3429 sigemptyset(&segv_action.sa_mask);
3430 sigaction(SIGSEGV, &segv_action, NULL);
3431 segv_compositor = ec;
3432
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003433 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03003434 weston_log("fatal: unhandled option: %s\n", argv[i]);
3435 if (argv[1]) {
3436 ret = EXIT_FAILURE;
3437 goto out;
3438 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003439
Daniel Stonec1be8e52012-06-01 11:14:02 -04003440 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01003441
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003442 ec->option_idle_time = idle_time;
3443 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003444
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003445 setenv("WAYLAND_DISPLAY", socket_name, 1);
3446
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003447 if (load_modules(ec, modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003448 goto out;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003449 if (load_modules(ec, option_modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003450 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003451
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003452 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003453 weston_log("fatal: failed to add socket: %m\n");
3454 ret = EXIT_FAILURE;
3455 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003456 }
3457
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003458 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003459 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003460
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003461 wl_display_run(display);
3462
3463 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003464 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05003465 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003466
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003467 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003468
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003469 for (i = ARRAY_LENGTH(signals); i;)
3470 wl_event_source_remove(signals[--i]);
3471
Daniel Stone855028f2012-05-01 20:37:10 +01003472 weston_compositor_xkb_destroy(ec);
3473
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003474 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003475 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003476
Martin Minarik19e6f262012-06-07 13:08:46 +02003477 weston_log_file_close();
3478
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003479 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003480}