blob: 9872bfef6a88c1900396fa487fb8d939c61b69f1 [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 Oliveiracbdebc22013-02-21 18:35:16 +0200352weston_transformed_coord(int width, int height,
353 enum wl_output_transform transform,
354 float sx, float sy, float *bx, float *by)
355{
356 switch (transform) {
357 case WL_OUTPUT_TRANSFORM_NORMAL:
358 default:
359 *bx = sx;
360 *by = sy;
361 break;
362 case WL_OUTPUT_TRANSFORM_FLIPPED:
363 *bx = width - sx;
364 *by = sy;
365 break;
366 case WL_OUTPUT_TRANSFORM_90:
367 *bx = height - sy;
368 *by = sx;
369 break;
370 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
371 *bx = height - sy;
372 *by = width - sx;
373 break;
374 case WL_OUTPUT_TRANSFORM_180:
375 *bx = width - sx;
376 *by = height - sy;
377 break;
378 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
379 *bx = sx;
380 *by = height - sy;
381 break;
382 case WL_OUTPUT_TRANSFORM_270:
383 *bx = sy;
384 *by = width - sx;
385 break;
386 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
387 *bx = sy;
388 *by = sx;
389 break;
390 }
391}
392
393WL_EXPORT pixman_box32_t
394weston_transformed_rect(int width, int height,
395 enum wl_output_transform transform,
396 pixman_box32_t rect)
397{
398 float x1, x2, y1, y2;
399
400 pixman_box32_t ret;
401
402 weston_transformed_coord(width, height, transform,
403 rect.x1, rect.y1, &x1, &y1);
404 weston_transformed_coord(width, height, transform,
405 rect.x2, rect.y2, &x2, &y2);
406
407 if (x1 <= x2) {
408 ret.x1 = x1;
409 ret.x2 = x2;
410 } else {
411 ret.x1 = x2;
412 ret.x2 = x1;
413 }
414
415 if (y1 <= y2) {
416 ret.y1 = y1;
417 ret.y2 = y2;
418 } else {
419 ret.y1 = y2;
420 ret.y2 = y1;
421 }
422
423 return ret;
424}
425
426WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200427weston_surface_to_buffer_float(struct weston_surface *surface,
428 float sx, float sy, float *bx, float *by)
429{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200430 weston_transformed_coord(surface->geometry.width,
431 surface->geometry.height,
432 surface->buffer_transform,
433 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200434}
435
436WL_EXPORT pixman_box32_t
437weston_surface_to_buffer_rect(struct weston_surface *surface,
438 pixman_box32_t rect)
439{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200440 return weston_transformed_rect(surface->geometry.width,
441 surface->geometry.height,
442 surface->buffer_transform, rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200443}
444
445WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400446weston_surface_move_to_plane(struct weston_surface *surface,
447 struct weston_plane *plane)
448{
449 if (surface->plane == plane)
450 return;
451
452 weston_surface_damage_below(surface);
453 surface->plane = plane;
454 weston_surface_damage(surface);
455}
456
457WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500458weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200459{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500460 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200461
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500462 pixman_region32_init(&damage);
463 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
464 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400465 pixman_region32_union(&surface->plane->damage,
466 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500467 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200468}
469
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400470static struct wl_resource *
471find_resource_for_client(struct wl_list *list, struct wl_client *client)
472{
473 struct wl_resource *r;
474
475 wl_list_for_each(r, list, link) {
476 if (r->client == client)
477 return r;
478 }
479
480 return NULL;
481}
482
483static void
484weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
485{
486 uint32_t different = es->output_mask ^ mask;
487 uint32_t entered = mask & different;
488 uint32_t left = es->output_mask & different;
489 struct weston_output *output;
490 struct wl_resource *resource = NULL;
491 struct wl_client *client = es->surface.resource.client;
492
493 es->output_mask = mask;
494 if (es->surface.resource.client == NULL)
495 return;
496 if (different == 0)
497 return;
498
499 wl_list_for_each(output, &es->compositor->output_list, link) {
500 if (1 << output->id & different)
501 resource =
502 find_resource_for_client(&output->resource_list,
503 client);
504 if (resource == NULL)
505 continue;
506 if (1 << output->id & entered)
507 wl_surface_send_enter(&es->surface.resource, resource);
508 if (1 << output->id & left)
509 wl_surface_send_leave(&es->surface.resource, resource);
510 }
511}
512
513static void
514weston_surface_assign_output(struct weston_surface *es)
515{
516 struct weston_compositor *ec = es->compositor;
517 struct weston_output *output, *new_output;
518 pixman_region32_t region;
519 uint32_t max, area, mask;
520 pixman_box32_t *e;
521
522 new_output = NULL;
523 max = 0;
524 mask = 0;
525 pixman_region32_init(&region);
526 wl_list_for_each(output, &ec->output_list, link) {
527 pixman_region32_intersect(&region, &es->transform.boundingbox,
528 &output->region);
529
530 e = pixman_region32_extents(&region);
531 area = (e->x2 - e->x1) * (e->y2 - e->y1);
532
533 if (area > 0)
534 mask |= 1 << output->id;
535
536 if (area >= max) {
537 new_output = output;
538 max = area;
539 }
540 }
541 pixman_region32_fini(&region);
542
543 es->output = new_output;
544 weston_surface_update_output_mask(es, mask);
545}
546
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200547static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200548surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
549 int32_t width, int32_t height,
550 pixman_region32_t *bbox)
551{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200552 float min_x = HUGE_VALF, min_y = HUGE_VALF;
553 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200554 int32_t s[4][2] = {
555 { sx, sy },
556 { sx, sy + height },
557 { sx + width, sy },
558 { sx + width, sy + height }
559 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200560 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200561 int i;
562
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300563 if (width == 0 || height == 0) {
564 /* avoid rounding empty bbox to 1x1 */
565 pixman_region32_init(bbox);
566 return;
567 }
568
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200569 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200570 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400571 weston_surface_to_global_float(surface,
572 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200573 if (x < min_x)
574 min_x = x;
575 if (x > max_x)
576 max_x = x;
577 if (y < min_y)
578 min_y = y;
579 if (y > max_y)
580 max_y = y;
581 }
582
Pekka Paalanen219b9822012-02-08 15:38:37 +0200583 int_x = floorf(min_x);
584 int_y = floorf(min_y);
585 pixman_region32_init_rect(bbox, int_x, int_y,
586 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200587}
588
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200589static void
590weston_surface_update_transform_disable(struct weston_surface *surface)
591{
592 surface->transform.enabled = 0;
593
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200594 /* round off fractions when not transformed */
595 surface->geometry.x = roundf(surface->geometry.x);
596 surface->geometry.y = roundf(surface->geometry.y);
597
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200598 pixman_region32_init_rect(&surface->transform.boundingbox,
599 surface->geometry.x,
600 surface->geometry.y,
601 surface->geometry.width,
602 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500603
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400604 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500605 pixman_region32_copy(&surface->transform.opaque,
606 &surface->opaque);
607 pixman_region32_translate(&surface->transform.opaque,
608 surface->geometry.x,
609 surface->geometry.y);
610 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200611}
612
613static int
614weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200615{
616 struct weston_matrix *matrix = &surface->transform.matrix;
617 struct weston_matrix *inverse = &surface->transform.inverse;
618 struct weston_transform *tform;
619
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200620 surface->transform.enabled = 1;
621
622 /* Otherwise identity matrix, but with x and y translation. */
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300623 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200624 surface->transform.position.matrix.d[12] = surface->geometry.x;
625 surface->transform.position.matrix.d[13] = surface->geometry.y;
626
627 weston_matrix_init(matrix);
628 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
629 weston_matrix_multiply(matrix, &tform->matrix);
630
631 if (weston_matrix_invert(inverse, matrix) < 0) {
632 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200633 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200634 " transformation not invertible.\n", surface);
635 return -1;
636 }
637
638 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
639 surface->geometry.height,
640 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500641
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200642 return 0;
643}
644
645WL_EXPORT void
646weston_surface_update_transform(struct weston_surface *surface)
647{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200648 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200649 return;
650
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200651 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200652
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500653 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200654
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200655 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500656 pixman_region32_fini(&surface->transform.opaque);
657 pixman_region32_init(&surface->transform.opaque);
658
Pekka Paalanencd403622012-01-25 13:37:39 +0200659 /* transform.position is always in transformation_list */
660 if (surface->geometry.transformation_list.next ==
661 &surface->transform.position.link &&
662 surface->geometry.transformation_list.prev ==
663 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200664 weston_surface_update_transform_disable(surface);
665 } else {
666 if (weston_surface_update_transform_enable(surface) < 0)
667 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200668 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200669
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400670 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200671
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300672 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200673}
674
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200675WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100676weston_surface_to_global_fixed(struct weston_surface *surface,
677 wl_fixed_t sx, wl_fixed_t sy,
678 wl_fixed_t *x, wl_fixed_t *y)
679{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200680 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100681
682 weston_surface_to_global_float(surface,
683 wl_fixed_to_double(sx),
684 wl_fixed_to_double(sy),
685 &xf, &yf);
686 *x = wl_fixed_from_double(xf);
687 *y = wl_fixed_from_double(yf);
688}
689
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400690WL_EXPORT void
691weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200692 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200693{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200694 if (surface->transform.enabled) {
695 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
696
697 weston_matrix_transform(&surface->transform.inverse, &v);
698
699 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200700 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200701 "weston_surface_from_global(), divisor = %g\n",
702 v.f[3]);
703 *sx = 0;
704 *sy = 0;
705 return;
706 }
707
Pekka Paalanencd403622012-01-25 13:37:39 +0200708 *sx = v.f[0] / v.f[3];
709 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200710 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200711 *sx = x - surface->geometry.x;
712 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200713 }
714}
715
716WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100717weston_surface_from_global_fixed(struct weston_surface *surface,
718 wl_fixed_t x, wl_fixed_t y,
719 wl_fixed_t *sx, wl_fixed_t *sy)
720{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200721 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100722
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400723 weston_surface_from_global_float(surface,
724 wl_fixed_to_double(x),
725 wl_fixed_to_double(y),
726 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100727 *sx = wl_fixed_from_double(sxf);
728 *sy = wl_fixed_from_double(syf);
729}
730
731WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200732weston_surface_from_global(struct weston_surface *surface,
733 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
734{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200735 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200736
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400737 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200738 *sx = floorf(sxf);
739 *sy = floorf(syf);
740}
741
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400742WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400743weston_surface_schedule_repaint(struct weston_surface *surface)
744{
745 struct weston_output *output;
746
747 wl_list_for_each(output, &surface->compositor->output_list, link)
748 if (surface->output_mask & (1 << output->id))
749 weston_output_schedule_repaint(output);
750}
751
752WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500753weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500754{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400755 pixman_region32_union_rect(&surface->damage, &surface->damage,
756 0, 0, surface->geometry.width,
757 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200758
Kristian Høgsberg98238702012-08-03 16:29:12 -0400759 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500760}
761
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400762WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500763weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200764 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400765{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200766 surface->geometry.x = x;
767 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200768 surface->geometry.width = width;
769 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200770 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400771}
772
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200773WL_EXPORT void
774weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200775 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200776{
777 surface->geometry.x = x;
778 surface->geometry.y = y;
779 surface->geometry.dirty = 1;
780}
781
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300782WL_EXPORT int
783weston_surface_is_mapped(struct weston_surface *surface)
784{
785 if (surface->output)
786 return 1;
787 else
788 return 0;
789}
790
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200791WL_EXPORT int32_t
792weston_surface_buffer_width(struct weston_surface *surface)
793{
794 switch (surface->buffer_transform) {
795 case WL_OUTPUT_TRANSFORM_90:
796 case WL_OUTPUT_TRANSFORM_270:
797 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
798 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200799 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200800 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200801 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200802 }
803}
804
805WL_EXPORT int32_t
806weston_surface_buffer_height(struct weston_surface *surface)
807{
808 switch (surface->buffer_transform) {
809 case WL_OUTPUT_TRANSFORM_90:
810 case WL_OUTPUT_TRANSFORM_270:
811 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
812 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200813 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200814 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200815 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200816 }
817}
818
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400819WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500820weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500821{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400822 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500823
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400824 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500825
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400826 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500827}
828
Tiago Vignatti9d393522012-02-10 16:26:19 +0200829static struct weston_surface *
830weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100831 wl_fixed_t x, wl_fixed_t y,
832 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200833{
834 struct weston_surface *surface;
835
836 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100837 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500838 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100839 wl_fixed_to_int(*sx),
840 wl_fixed_to_int(*sy),
841 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200842 return surface;
843 }
844
845 return NULL;
846}
847
848static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400849weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500850{
Scott Moreau447013d2012-02-18 05:05:29 -0700851 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500852 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400853 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500854
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400855 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100856 return;
857
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400858 surface = weston_compositor_pick_surface(seat->compositor,
859 pointer->x,
860 pointer->y,
861 &pointer->current_x,
862 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500863
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400864 if (&surface->surface != pointer->current) {
865 interface = pointer->grab->interface;
866 pointer->current = &surface->surface;
867 interface->focus(pointer->grab, &surface->surface,
868 pointer->current_x,
869 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500870 }
871
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400872 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500873 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100874 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400875 pointer->x,
876 pointer->y,
877 &pointer->grab->x,
878 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500879}
880
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400881static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500882weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400883{
Daniel Stone37816df2012-05-16 18:45:18 +0100884 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400885
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500886 if (!compositor->focus)
887 return;
888
Daniel Stone37816df2012-05-16 18:45:18 +0100889 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400890 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400891}
892
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400893WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500894weston_surface_unmap(struct weston_surface *surface)
895{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100896 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500897
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500898 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500899 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500900 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500901
Daniel Stone4dab5db2012-05-30 16:31:53 +0100902 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100903 if (seat->seat.keyboard &&
904 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100905 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100906 if (seat->seat.pointer &&
907 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100908 wl_pointer_set_focus(seat->seat.pointer,
909 NULL,
910 wl_fixed_from_int(0),
911 wl_fixed_from_int(0));
912 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500913
Kristian Høgsberg98238702012-08-03 16:29:12 -0400914 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500915}
916
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400917struct weston_frame_callback {
918 struct wl_resource resource;
919 struct wl_list link;
920};
921
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500922static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400923destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500924{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500925 struct weston_surface *surface =
926 container_of(resource,
927 struct weston_surface, surface.resource);
928 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400929 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400930
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300931 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500932 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400933
Pekka Paalanenbc106382012-10-10 12:49:31 +0300934 wl_list_for_each_safe(cb, next,
935 &surface->pending.frame_callback_list, link)
936 wl_resource_destroy(&cb->resource);
937
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300938 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300939 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +0300940 pixman_region32_fini(&surface->pending.damage);
941
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300942 if (surface->pending.buffer)
943 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
944
Pekka Paalanende685b82012-12-04 15:58:12 +0200945 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400946
Kristian Høgsberg42263852012-09-06 21:59:29 -0400947 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200948
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200949 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200950 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500951 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500952 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300953 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200954
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400955 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
956 wl_resource_destroy(&cb->resource);
957
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400958 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500959}
960
Alex Wu8811bf92012-02-28 18:07:54 +0800961WL_EXPORT void
962weston_surface_destroy(struct weston_surface *surface)
963{
964 /* Not a valid way to destroy a client surface */
965 assert(surface->surface.resource.client == NULL);
966
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400967 wl_signal_emit(&surface->surface.resource.destroy_signal,
968 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800969 destroy_surface(&surface->surface.resource);
970}
971
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100972static void
Pekka Paalanende685b82012-12-04 15:58:12 +0200973weston_buffer_reference_handle_destroy(struct wl_listener *listener,
974 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100975{
Pekka Paalanende685b82012-12-04 15:58:12 +0200976 struct weston_buffer_reference *ref =
977 container_of(listener, struct weston_buffer_reference,
978 destroy_listener);
979
980 assert((struct wl_buffer *)data == ref->buffer);
981 ref->buffer = NULL;
982}
983
984WL_EXPORT void
985weston_buffer_reference(struct weston_buffer_reference *ref,
986 struct wl_buffer *buffer)
987{
988 if (ref->buffer && buffer != ref->buffer) {
989 weston_buffer_post_release(ref->buffer);
990 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300991 }
992
Pekka Paalanende685b82012-12-04 15:58:12 +0200993 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400994 buffer->busy_count++;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300995 wl_signal_add(&buffer->resource.destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +0200996 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +0200997 }
998
Pekka Paalanende685b82012-12-04 15:58:12 +0200999 ref->buffer = buffer;
1000 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1001}
1002
1003static void
1004weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
1005{
1006 weston_buffer_reference(&surface->buffer_ref, buffer);
1007
Pekka Paalanena6421c42012-12-04 15:58:10 +02001008 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001009 if (weston_surface_is_mapped(surface))
1010 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001011 }
1012
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001013 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001014}
1015
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001016WL_EXPORT void
1017weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001018{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001019 wl_list_remove(&surface->layer_link);
1020 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001021 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001022 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001023}
1024
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001025WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001026weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001027{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001028 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001029
1030 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001031 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001032}
1033
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001034WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001035weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +02001036{
Kristian Høgsberg24596942011-10-24 17:51:02 -04001037 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +02001038 return;
Benjamin Franzke06286262011-05-06 19:12:33 +02001039
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001040 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -05001041 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +02001042}
1043
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001044WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001045weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001046{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001047 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001048
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001049 pixman_region32_union(&compositor->primary_plane.damage,
1050 &compositor->primary_plane.damage,
1051 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001052 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001053}
1054
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001055static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001056fade_frame(struct weston_animation *animation,
1057 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001058{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001059 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001060 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001061 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001062 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001063
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001064 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -06001065 compositor->fade.spring.timestamp = msecs;
1066
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001067 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001068 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001069 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
1070 compositor->fade.spring.current);
1071 weston_surface_damage(surface);
1072
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001073 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001074 compositor->fade.spring.current =
1075 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001076 wl_list_remove(&animation->link);
1077 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001078
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001079 if (compositor->fade.spring.current < 0.001) {
1080 destroy_surface(&surface->surface.resource);
1081 compositor->fade.surface = NULL;
1082 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001083 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001084 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001085 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001086 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001087}
1088
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001089static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001090surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001091 pixman_region32_t *opaque)
1092{
Pekka Paalanende685b82012-12-04 15:58:12 +02001093 if (surface->buffer_ref.buffer &&
1094 wl_buffer_is_shm(surface->buffer_ref.buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001095 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001096
1097 if (surface->transform.enabled) {
1098 pixman_box32_t *extents;
1099
1100 extents = pixman_region32_extents(&surface->damage);
1101 surface_compute_bbox(surface, extents->x1, extents->y1,
1102 extents->x2 - extents->x1,
1103 extents->y2 - extents->y1,
1104 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001105 pixman_region32_translate(&surface->damage,
1106 -surface->plane->x,
1107 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001108 } else {
1109 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001110 surface->geometry.x - surface->plane->x,
1111 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001112 }
1113
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001114 pixman_region32_subtract(&surface->damage,
1115 &surface->damage, &surface->plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001116 pixman_region32_union(&surface->plane->damage,
1117 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001118 empty_region(&surface->damage);
1119 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001120 pixman_region32_union(&surface->plane->opaque,
1121 &surface->plane->opaque,
1122 &surface->transform.opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001123 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001124}
1125
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001126static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001127weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001128{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001129 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001130 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001131 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001132 struct weston_animation *animation, *next;
1133 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001134 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001135 pixman_region32_t opaque, output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001136
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001137 weston_compositor_update_drag_surfaces(ec);
1138
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001139 /* Rebuild the surface list and update surface transforms up front. */
1140 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +02001141 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001142 wl_list_for_each(layer, &ec->layer_list, link) {
1143 wl_list_for_each(es, &layer->surface_list, layer_link) {
1144 weston_surface_update_transform(es);
1145 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +02001146 if (es->output == output) {
1147 wl_list_insert_list(&frame_callback_list,
1148 &es->frame_callback_list);
1149 wl_list_init(&es->frame_callback_list);
1150 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001151 }
1152 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001153
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001154 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001155 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001156 else
1157 wl_list_for_each(es, &ec->surface_list, link)
1158 weston_surface_move_to_plane(es, &ec->primary_plane);
1159
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001160 pixman_region32_init(&opaque);
1161
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001162 pixman_region32_fini(&ec->primary_plane.opaque);
1163 pixman_region32_init(&ec->primary_plane.opaque);
1164
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001165 wl_list_for_each(es, &ec->surface_list, link) {
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001166 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001167
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001168 /* Both the renderer and the backend have seen the buffer
1169 * by now. If renderer needs the buffer, it has its own
1170 * reference set. If the backend wants to keep the buffer
1171 * around for migrating the surface into a non-primary plane
1172 * later, keep_buffer is true. Otherwise, drop the core
1173 * reference now, and allow early buffer release. This enables
1174 * clients to use single-buffering.
1175 */
1176 if (!es->keep_buffer)
1177 weston_buffer_reference(&es->buffer_ref, NULL);
1178 }
1179
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -04001180 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001181
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001182 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001183 pixman_region32_intersect(&output_damage,
1184 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001185
Scott Moreauccbf29d2012-02-22 14:21:41 -07001186 if (output->dirty)
1187 weston_output_update_matrix(output);
1188
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001189 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001190
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001191 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001192
Kristian Høgsbergef044142011-06-21 15:02:12 -04001193 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001194
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001195 weston_compositor_repick(ec);
1196 wl_event_loop_dispatch(ec->input_loop, 0);
1197
Jonas Ådahldb773762012-06-13 00:01:21 +02001198 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001199 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001200 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001201 }
1202
Scott Moreaud64cf212012-06-08 19:40:54 -06001203 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001204 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001205 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001206 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001207}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001208
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001209static int
1210weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001211{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001212 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001213
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001214 wl_event_loop_dispatch(compositor->input_loop, 0);
1215
1216 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001217}
1218
1219WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001220weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001221{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001222 struct weston_compositor *compositor = output->compositor;
1223 struct wl_event_loop *loop =
1224 wl_display_get_event_loop(compositor->wl_display);
1225 int fd;
1226
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001227 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001228 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001229 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001230 return;
1231 }
1232
1233 output->repaint_scheduled = 0;
1234 if (compositor->input_loop_source)
1235 return;
1236
1237 fd = wl_event_loop_get_fd(compositor->input_loop);
1238 compositor->input_loop_source =
1239 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1240 weston_compositor_read_input, compositor);
1241}
1242
1243static void
1244idle_repaint(void *data)
1245{
1246 struct weston_output *output = data;
1247
1248 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001249}
1250
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001251WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001252weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1253{
1254 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001255 if (below != NULL)
1256 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001257}
1258
1259WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001260weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001261{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001262 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001263 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001264
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001265 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001266 return;
1267
Kristian Høgsbergef044142011-06-21 15:02:12 -04001268 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001269 output->repaint_needed = 1;
1270 if (output->repaint_scheduled)
1271 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001272
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001273 wl_event_loop_add_idle(loop, idle_repaint, output);
1274 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001275
1276 if (compositor->input_loop_source) {
1277 wl_event_source_remove(compositor->input_loop_source);
1278 compositor->input_loop_source = NULL;
1279 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001280}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001281
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001282WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001283weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1284{
1285 struct weston_output *output;
1286
1287 wl_list_for_each(output, &compositor->output_list, link)
1288 weston_output_schedule_repaint(output);
1289}
1290
1291WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001292weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001293{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001294 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001295 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001296
Scott Moreau9d1b1122012-06-08 19:40:53 -06001297 output = container_of(compositor->output_list.next,
1298 struct weston_output, link);
1299
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001300 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001301 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001302 return;
1303
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001304 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001305 surface = weston_surface_create(compositor);
John Kåre Alsakere2e3d072012-10-12 12:25:09 +02001306 if (!surface)
1307 return;
1308
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001309 weston_surface_configure(surface, 0, 0, 8192, 8192);
Ander Conselvan de Oliveira4c65da52013-02-13 16:06:37 +02001310 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0 - tint);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001311 wl_list_insert(&compositor->fade_layer.surface_list,
1312 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03001313 weston_surface_update_transform(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001314 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001315 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001316 }
1317
1318 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001319 if (wl_list_empty(&compositor->fade.animation.link)) {
1320 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001321 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001322 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001323 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001324}
1325
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001326static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001327surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001328{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001329 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001330}
1331
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001332static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001333surface_attach(struct wl_client *client,
1334 struct wl_resource *resource,
1335 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1336{
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001337 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001338 struct wl_buffer *buffer = NULL;
1339
1340 if (buffer_resource)
1341 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001342
Pekka Paalanende685b82012-12-04 15:58:12 +02001343 /* Attach, attach, without commit in between does not send
1344 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001345 if (surface->pending.buffer)
1346 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001347
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001348 surface->pending.sx = sx;
1349 surface->pending.sy = sy;
1350 surface->pending.buffer = buffer;
1351 if (buffer) {
1352 wl_signal_add(&buffer->resource.destroy_signal,
1353 &surface->pending.buffer_destroy_listener);
1354 surface->pending.remove_contents = 0;
1355 } else {
1356 surface->pending.remove_contents = 1;
1357 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001358}
1359
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001360static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001361surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001362 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001363 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001364{
Pekka Paalanen8e159182012-10-10 12:49:25 +03001365 struct weston_surface *surface = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001366
Pekka Paalanen8e159182012-10-10 12:49:25 +03001367 pixman_region32_union_rect(&surface->pending.damage,
1368 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001369 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001370}
1371
Kristian Høgsberg33418202011-08-16 23:01:28 -04001372static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001373destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001374{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001375 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001376
1377 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001378 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001379}
1380
1381static void
1382surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001383 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001384{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001385 struct weston_frame_callback *cb;
Pekka Paalanenbc106382012-10-10 12:49:31 +03001386 struct weston_surface *surface = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001387
1388 cb = malloc(sizeof *cb);
1389 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001390 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001391 return;
1392 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001393
Kristian Høgsberg33418202011-08-16 23:01:28 -04001394 cb->resource.object.interface = &wl_callback_interface;
1395 cb->resource.object.id = callback;
1396 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001397 cb->resource.client = client;
1398 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001399
1400 wl_client_add_resource(client, &cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001401 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001402}
1403
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001404static void
1405surface_set_opaque_region(struct wl_client *client,
1406 struct wl_resource *resource,
1407 struct wl_resource *region_resource)
1408{
1409 struct weston_surface *surface = resource->data;
1410 struct weston_region *region;
1411
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001412 if (region_resource) {
1413 region = region_resource->data;
Pekka Paalanen512dde82012-10-10 12:49:27 +03001414 pixman_region32_copy(&surface->pending.opaque,
1415 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001416 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001417 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001418 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001419}
1420
1421static void
1422surface_set_input_region(struct wl_client *client,
1423 struct wl_resource *resource,
1424 struct wl_resource *region_resource)
1425{
1426 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001427 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001428
1429 if (region_resource) {
1430 region = region_resource->data;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001431 pixman_region32_copy(&surface->pending.input,
1432 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001433 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001434 pixman_region32_fini(&surface->pending.input);
1435 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001436 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001437}
1438
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001439static int
1440surface_pending_buffer_has_different_size(struct weston_surface *surface)
1441{
1442 int width, height;
1443
1444 switch (surface->pending.buffer_transform) {
1445 case WL_OUTPUT_TRANSFORM_90:
1446 case WL_OUTPUT_TRANSFORM_270:
1447 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1448 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1449 height = surface->pending.buffer->width;
1450 width = surface->pending.buffer->height;
1451 break;
1452 default:
1453 width = surface->pending.buffer->width;
1454 height = surface->pending.buffer->height;
1455 }
1456
1457 if (width == surface->geometry.width &&
1458 height == surface->geometry.height)
1459 return 0;
1460 else
1461 return 1;
1462}
1463
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001464static void
1465surface_commit(struct wl_client *client, struct wl_resource *resource)
1466{
1467 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001468 pixman_region32_t opaque;
1469
1470 if (surface->pending.sx || surface->pending.sy ||
1471 (surface->pending.buffer &&
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001472 surface_pending_buffer_has_different_size(surface)))
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001473 surface->geometry.dirty = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001474
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001475 /* wl_surface.set_buffer_rotation */
1476 surface->buffer_transform = surface->pending.buffer_transform;
1477
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001478 /* wl_surface.attach */
1479 if (surface->pending.buffer || surface->pending.remove_contents)
1480 weston_surface_attach(surface, surface->pending.buffer);
1481
Pekka Paalanende685b82012-12-04 15:58:12 +02001482 if (surface->buffer_ref.buffer && surface->configure)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001483 surface->configure(surface, surface->pending.sx,
1484 surface->pending.sy);
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001485 surface->pending.sx = 0;
1486 surface->pending.sy = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001487
1488 /* wl_surface.damage */
1489 pixman_region32_union(&surface->damage, &surface->damage,
1490 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001491 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1492 0, 0,
1493 surface->geometry.width,
1494 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001495 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001496
1497 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001498 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001499 surface->geometry.width,
1500 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001501 pixman_region32_intersect(&opaque,
1502 &opaque, &surface->pending.opaque);
1503
1504 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1505 pixman_region32_copy(&surface->opaque, &opaque);
1506 surface->geometry.dirty = 1;
1507 }
1508
1509 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001510
1511 /* wl_surface.set_input_region */
1512 pixman_region32_fini(&surface->input);
1513 pixman_region32_init_rect(&surface->input, 0, 0,
1514 surface->geometry.width,
1515 surface->geometry.height);
1516 pixman_region32_intersect(&surface->input,
1517 &surface->input, &surface->pending.input);
1518
Pekka Paalanenbc106382012-10-10 12:49:31 +03001519 /* wl_surface.frame */
1520 wl_list_insert_list(&surface->frame_callback_list,
1521 &surface->pending.frame_callback_list);
1522 wl_list_init(&surface->pending.frame_callback_list);
1523
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001524 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001525}
1526
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001527static void
1528surface_set_buffer_transform(struct wl_client *client,
1529 struct wl_resource *resource, int transform)
1530{
1531 struct weston_surface *surface = resource->data;
1532
1533 surface->pending.buffer_transform = transform;
1534}
1535
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001536static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001537 surface_destroy,
1538 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001539 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001540 surface_frame,
1541 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001542 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001543 surface_commit,
1544 surface_set_buffer_transform
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001545};
1546
1547static void
1548compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001549 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001550{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001551 struct weston_compositor *ec = resource->data;
1552 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001553
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001554 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001555 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001556 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001557 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001558 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001559
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001560 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001561
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001562 surface->surface.resource.object.id = id;
1563 surface->surface.resource.object.interface = &wl_surface_interface;
1564 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001565 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001566 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001567
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001568 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001569}
1570
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001571static void
1572destroy_region(struct wl_resource *resource)
1573{
1574 struct weston_region *region =
1575 container_of(resource, struct weston_region, resource);
1576
1577 pixman_region32_fini(&region->region);
1578 free(region);
1579}
1580
1581static void
1582region_destroy(struct wl_client *client, struct wl_resource *resource)
1583{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001584 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001585}
1586
1587static void
1588region_add(struct wl_client *client, struct wl_resource *resource,
1589 int32_t x, int32_t y, int32_t width, int32_t height)
1590{
1591 struct weston_region *region = resource->data;
1592
1593 pixman_region32_union_rect(&region->region, &region->region,
1594 x, y, width, height);
1595}
1596
1597static void
1598region_subtract(struct wl_client *client, struct wl_resource *resource,
1599 int32_t x, int32_t y, int32_t width, int32_t height)
1600{
1601 struct weston_region *region = resource->data;
1602 pixman_region32_t rect;
1603
1604 pixman_region32_init_rect(&rect, x, y, width, height);
1605 pixman_region32_subtract(&region->region, &region->region, &rect);
1606 pixman_region32_fini(&rect);
1607}
1608
1609static const struct wl_region_interface region_interface = {
1610 region_destroy,
1611 region_add,
1612 region_subtract
1613};
1614
1615static void
1616compositor_create_region(struct wl_client *client,
1617 struct wl_resource *resource, uint32_t id)
1618{
1619 struct weston_region *region;
1620
1621 region = malloc(sizeof *region);
1622 if (region == NULL) {
1623 wl_resource_post_no_memory(resource);
1624 return;
1625 }
1626
1627 region->resource.destroy = destroy_region;
1628
1629 region->resource.object.id = id;
1630 region->resource.object.interface = &wl_region_interface;
1631 region->resource.object.implementation =
1632 (void (**)(void)) &region_interface;
1633 region->resource.data = region;
1634
1635 pixman_region32_init(&region->region);
1636
1637 wl_client_add_resource(client, &region->resource);
1638}
1639
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001640static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001641 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001642 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001643};
1644
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001645WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001646weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001647{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001648 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1649 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001650
1651 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001652 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001653}
1654
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001655static void
1656weston_compositor_dpms_on(struct weston_compositor *compositor)
1657{
1658 struct weston_output *output;
1659
1660 wl_list_for_each(output, &compositor->output_list, link)
1661 if (output->set_dpms)
1662 output->set_dpms(output, WESTON_DPMS_ON);
1663}
1664
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001665WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001666weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001667{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001668 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1669 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001670 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001671 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001672 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001673 }
1674}
1675
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001676static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001677weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001678{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001679 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001680 compositor->idle_inhibit++;
1681}
1682
1683static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001684weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001685{
1686 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001687 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001688}
1689
1690static int
1691idle_handler(void *data)
1692{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001693 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001694
1695 if (compositor->idle_inhibit)
1696 return 1;
1697
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001698 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001699
1700 return 1;
1701}
1702
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001703WL_EXPORT void
1704weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1705{
1706 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001707 pixman_region32_init(&plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001708 plane->x = x;
1709 plane->y = y;
1710}
1711
1712WL_EXPORT void
1713weston_plane_release(struct weston_plane *plane)
1714{
1715 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001716 pixman_region32_fini(&plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001717}
1718
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001719static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001720weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001721
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001722static void
Daniel Stone37816df2012-05-16 18:45:18 +01001723clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001724{
Daniel Stone37816df2012-05-16 18:45:18 +01001725 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001726 struct weston_output *output, *prev = NULL;
1727 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001728
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001729 x = wl_fixed_to_int(*fx);
1730 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001731 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1732 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001733
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001734 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001735 if (pixman_region32_contains_point(&output->region,
1736 x, y, NULL))
1737 valid = 1;
1738 if (pixman_region32_contains_point(&output->region,
1739 old_x, old_y, NULL))
1740 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001741 }
Daniel Stone37816df2012-05-16 18:45:18 +01001742
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001743 if (!valid) {
1744 if (x < prev->x)
1745 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001746 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001747 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001748 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001749 if (y < prev->y)
1750 *fy = wl_fixed_from_int(prev->y);
1751 else if (y >= prev->y + prev->current->height)
1752 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001753 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001754 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001755}
1756
1757WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001758notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001759{
1760 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001761 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001762 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001763 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001764 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001765
1766 weston_compositor_activity(ec);
1767
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001768 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001769
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001770 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001771
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001772 pointer->x = x;
1773 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001774
1775 ix = wl_fixed_to_int(x);
1776 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001777
Scott Moreauccbf29d2012-02-22 14:21:41 -07001778 wl_list_for_each(output, &ec->output_list, link)
1779 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001780 pixman_region32_contains_point(&output->region,
1781 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06001782 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001783
Daniel Stone37816df2012-05-16 18:45:18 +01001784 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001785 interface = pointer->grab->interface;
1786 interface->motion(pointer->grab, time,
1787 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001788
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001789 if (seat->sprite) {
1790 weston_surface_set_position(seat->sprite,
1791 ix - seat->hotspot_x,
1792 iy - seat->hotspot_y);
1793 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001794 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001795}
1796
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001797WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001798weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001799 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001800{
Daniel Stone37816df2012-05-16 18:45:18 +01001801 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001802
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001803 if (seat->seat.keyboard) {
1804 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1805 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04001806 }
1807
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001808 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001809}
1810
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001811WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001812notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001813 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001814{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001815 struct weston_compositor *compositor = seat->compositor;
1816 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001817 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001818 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001819 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001820
Daniel Stone4dbadb12012-05-30 16:31:51 +01001821 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001822 if (compositor->ping_handler && focus)
1823 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001824 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001825 if (pointer->button_count == 0) {
1826 pointer->grab_button = button;
1827 pointer->grab_time = time;
1828 pointer->grab_x = pointer->x;
1829 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001830 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001831 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001832 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001833 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001834 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001835 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001836
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001837 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001838 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001839
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001840 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001841
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001842 if (pointer->button_count == 1)
1843 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001844 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001845}
1846
Scott Moreau210d0792012-03-22 10:47:01 -06001847WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001848notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01001849 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001850{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001851 struct weston_compositor *compositor = seat->compositor;
1852 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001853 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001854 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001855 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1856
1857 if (compositor->ping_handler && focus)
1858 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001859
1860 weston_compositor_activity(compositor);
1861
Scott Moreau6a3633d2012-03-20 08:47:59 -06001862 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001863 weston_compositor_run_axis_binding(compositor, seat,
1864 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06001865 else
1866 return;
1867
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001868 if (pointer->focus_resource)
1869 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01001870 value);
Scott Moreau210d0792012-03-22 10:47:01 -06001871}
1872
Daniel Stone05d58682012-06-22 13:21:38 +01001873WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001874notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001875{
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001876 struct wl_keyboard *keyboard = &seat->keyboard.keyboard;
Daniel Stone05d58682012-06-22 13:21:38 +01001877 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01001878 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001879 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01001880 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01001881 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01001882
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001883 /* Serialize and update our internal state, checking to see if it's
1884 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01001885 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1886 XKB_STATE_DEPRESSED);
1887 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1888 XKB_STATE_LATCHED);
1889 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1890 XKB_STATE_LOCKED);
1891 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01001892 XKB_STATE_EFFECTIVE);
1893
Daniel Stone71c38772012-06-22 13:21:30 +01001894 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
1895 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
1896 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
1897 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01001898 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01001899
Daniel Stone71c38772012-06-22 13:21:30 +01001900 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
1901 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
1902 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
1903 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001904
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001905 /* And update the modifier_state for bindings. */
1906 mods_lookup = mods_depressed | mods_latched;
1907 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01001908 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001909 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01001910 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001911 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01001912 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001913 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04001914 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
1915 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01001916
Daniel Stone8c8164f2012-05-30 16:31:45 +01001917 /* Finally, notify the compositor that LEDs have changed. */
1918 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001919 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001920 leds |= LED_NUM_LOCK;
1921 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001922 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001923 leds |= LED_CAPS_LOCK;
1924 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001925 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001926 leds |= LED_SCROLL_LOCK;
1927 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01001928 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01001929 seat->xkb_state.leds = leds;
1930
Daniel Stone05d58682012-06-22 13:21:38 +01001931 if (changed) {
1932 grab->interface->modifiers(grab,
1933 serial,
1934 keyboard->modifiers.mods_depressed,
1935 keyboard->modifiers.mods_latched,
1936 keyboard->modifiers.mods_locked,
1937 keyboard->modifiers.group);
1938 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001939}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001940
Daniel Stone05d58682012-06-22 13:21:38 +01001941static void
1942update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001943 enum wl_keyboard_key_state state)
1944{
1945 enum xkb_key_direction direction;
1946
1947 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1948 direction = XKB_KEY_DOWN;
1949 else
1950 direction = XKB_KEY_UP;
1951
1952 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1953 * broken keycode system, which starts at 8. */
1954 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1955
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001956 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001957}
1958
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001959WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001960notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001961 enum wl_keyboard_key_state state,
1962 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001963{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001964 struct weston_compositor *compositor = seat->compositor;
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001965 struct weston_keyboard *keyboard = &seat->keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01001966 struct weston_surface *focus =
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001967 (struct weston_surface *) keyboard->keyboard.focus;
1968 struct wl_keyboard_grab *grab = keyboard->keyboard.grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001969 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001970 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001971
Daniel Stonec9785ea2012-05-30 16:31:52 +01001972 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001973 if (compositor->ping_handler && focus)
1974 compositor->ping_handler(focus, serial);
1975
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001976 weston_compositor_idle_inhibit(compositor);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001977 keyboard->keyboard.grab_key = key;
1978 keyboard->keyboard.grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07001979 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001980 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001981 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001982
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001983 end = keyboard->keyboard.keys.data + keyboard->keyboard.keys.size;
1984 for (k = keyboard->keyboard.keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01001985 if (*k == key) {
1986 /* Ignore server-generated repeats. */
1987 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1988 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001989 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01001990 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001991 }
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001992 keyboard->keyboard.keys.size = (void *) end - keyboard->keyboard.keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001993 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001994 k = wl_array_add(&keyboard->keyboard.keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001995 *k = key;
1996 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001997
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001998 if (grab == &keyboard->keyboard.default_grab ||
1999 grab == &keyboard->input_method_grab) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002000 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002001 state);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01002002 grab = keyboard->keyboard.grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06002003 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05002004
Daniel Stone7ace3902012-05-30 16:31:40 +01002005 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01002006
2007 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002008 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01002009 wl_display_get_serial(compositor->wl_display),
2010 key,
2011 state);
2012 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002013}
2014
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002015WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002016notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01002017 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002018{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002019 struct weston_compositor *compositor = seat->compositor;
2020 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002021
2022 if (output) {
Kristian Høgsberg00fbbe62012-10-23 13:04:09 -04002023 clip_pointer_motion(seat, &x, &y);
Daniel Stone37816df2012-05-16 18:45:18 +01002024 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002025 x - pointer->x,
2026 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002027
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002028 pointer->x = x;
2029 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002030 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002031 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002032 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002033 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03002034 /* FIXME: We should call wl_pointer_set_focus(seat,
2035 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002036 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002037}
2038
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002039static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002040destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002041{
Daniel Stone37816df2012-05-16 18:45:18 +01002042 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002043
Daniel Stone37816df2012-05-16 18:45:18 +01002044 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002045 saved_kbd_focus_listener);
2046
Daniel Stone37816df2012-05-16 18:45:18 +01002047 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002048}
2049
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002050WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002051notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002052 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002053{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002054 struct weston_compositor *compositor = seat->compositor;
2055 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04002056 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002057 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05002058
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002059 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002060 wl_array_copy(&keyboard->keys, keys);
2061 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002062 weston_compositor_idle_inhibit(compositor);
2063 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002064 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002065 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002066 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002067
Daniel Stoneef172672012-06-22 13:21:32 +01002068 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002069 wl_array_for_each(k, &keyboard->keys) {
2070 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01002071 WL_KEYBOARD_KEY_STATE_PRESSED);
2072 }
2073
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002074 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002075
2076 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002077 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2078 wl_keyboard_set_focus(keyboard, surface);
2079 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002080 }
2081}
2082
2083WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002084notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01002085{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002086 struct weston_compositor *compositor = seat->compositor;
2087 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002088 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002089
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002090 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002091 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002092 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002093 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002094 WL_KEYBOARD_KEY_STATE_RELEASED);
2095 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002096
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002097 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002098
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002099 if (keyboard->focus) {
2100 seat->saved_kbd_focus = keyboard->focus;
2101 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01002102 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002103 wl_signal_add(&keyboard->focus->resource.destroy_signal,
2104 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002105 }
2106
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002107 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002108 /* FIXME: We really need keyboard grab cancel here to
2109 * let the grab shut down properly. As it is we leak
2110 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002111 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002112}
2113
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002114static void
Daniel Stone37816df2012-05-16 18:45:18 +01002115touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002116{
Daniel Stone37816df2012-05-16 18:45:18 +01002117 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002118 struct wl_resource *resource;
2119
Pekka Paalanen56464252012-07-31 13:21:08 +03002120 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002121 return;
2122
Pekka Paalanen56464252012-07-31 13:21:08 +03002123 if (seat->touch->focus_resource)
2124 wl_list_remove(&seat->touch->focus_listener.link);
2125 seat->touch->focus = NULL;
2126 seat->touch->focus_resource = NULL;
2127
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002128 if (surface) {
2129 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01002130 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04002131 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002132 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02002133 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002134 return;
2135 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002136
Daniel Stone37816df2012-05-16 18:45:18 +01002137 seat->touch->focus = surface;
2138 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03002139 wl_signal_add(&resource->destroy_signal,
2140 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002141 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002142}
2143
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002144/**
2145 * notify_touch - emulates button touches and notifies surfaces accordingly.
2146 *
2147 * It assumes always the correct cycle sequence until it gets here: touch_down
2148 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2149 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002150 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002151 */
2152WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002153notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002154 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002155{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002156 struct weston_compositor *ec = seat->compositor;
2157 struct wl_touch *touch = seat->seat.touch;
Matt Roper47c1b982012-10-10 16:56:53 -07002158 struct wl_touch_grab *grab = touch->grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002159 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002160 wl_fixed_t sx, sy;
Matt Roper47c1b982012-10-10 16:56:53 -07002161
2162 /* Update grab's global coordinates. */
2163 touch->grab_x = x;
2164 touch->grab_y = y;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002165
2166 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01002167 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002168 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002169
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002170 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002171
2172 /* the first finger down picks the surface, and all further go
2173 * to that surface for the remainder of the touch session i.e.
2174 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002175 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002176 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002177 touch_set_focus(seat, &es->surface);
2178 } else if (touch->focus) {
2179 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002180 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Rob Bradford26e009c2012-12-05 18:47:05 +00002181 } else {
2182 /* Unexpected condition: We have non-initial touch but
2183 * there is no focused surface.
2184 */
2185 weston_log("touch event received with %d points down"
2186 "but no surface focused\n", seat->num_tp);
2187 return;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002188 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002189
Matt Roper47c1b982012-10-10 16:56:53 -07002190 grab->interface->down(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002191 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002192 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002193 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002194 if (!es)
2195 break;
2196
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002197 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Matt Roper47c1b982012-10-10 16:56:53 -07002198 grab->interface->motion(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002199 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002200 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002201 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002202 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002203
Matt Roper47c1b982012-10-10 16:56:53 -07002204 grab->interface->up(grab, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002205 if (seat->num_tp == 0)
2206 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002207 break;
2208 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002209}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002210
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04002211static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002212pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
2213{
2214 struct weston_seat *seat = container_of(listener, struct weston_seat,
2215 sprite_destroy_listener);
2216
2217 seat->sprite = NULL;
2218}
2219
2220static void
2221pointer_cursor_surface_configure(struct weston_surface *es,
2222 int32_t dx, int32_t dy)
2223{
2224 struct weston_seat *seat = es->private;
2225 int x, y;
2226
2227 assert(es == seat->sprite);
2228
2229 seat->hotspot_x -= dx;
2230 seat->hotspot_y -= dy;
2231
2232 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
2233 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
2234
2235 weston_surface_configure(seat->sprite, x, y,
Pekka Paalanende685b82012-12-04 15:58:12 +02002236 es->buffer_ref.buffer->width,
2237 es->buffer_ref.buffer->height);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002238
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002239 empty_region(&es->pending.input);
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03002240
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002241 if (!weston_surface_is_mapped(es)) {
2242 wl_list_insert(&es->compositor->cursor_layer.surface_list,
2243 &es->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002244 weston_surface_update_transform(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002245 }
2246}
2247
2248static void
2249pointer_unmap_sprite(struct weston_seat *seat)
2250{
2251 if (weston_surface_is_mapped(seat->sprite))
2252 weston_surface_unmap(seat->sprite);
2253
2254 wl_list_remove(&seat->sprite_destroy_listener.link);
2255 seat->sprite->configure = NULL;
2256 seat->sprite->private = NULL;
2257 seat->sprite = NULL;
2258}
2259
2260static void
2261pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2262 uint32_t serial, struct wl_resource *surface_resource,
2263 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002264{
Daniel Stone37816df2012-05-16 18:45:18 +01002265 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002266 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002267
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002268 if (surface_resource)
Pekka Paalanen6c71ee12012-10-10 12:49:30 +03002269 surface = surface_resource->data;
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002270
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04002271 if (seat->seat.pointer->focus == NULL)
2272 return;
2273 if (seat->seat.pointer->focus->resource.client != client)
2274 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002275 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002276 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002277
2278 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002279 if (surface->configure) {
2280 wl_resource_post_error(&surface->surface.resource,
2281 WL_DISPLAY_ERROR_INVALID_OBJECT,
2282 "surface->configure already "
2283 "set");
2284 return;
2285 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002286 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002287
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002288 if (seat->sprite)
2289 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002290
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002291 if (!surface)
2292 return;
2293
2294 wl_signal_add(&surface->surface.resource.destroy_signal,
2295 &seat->sprite_destroy_listener);
2296
2297 surface->configure = pointer_cursor_surface_configure;
2298 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002299 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002300 seat->hotspot_x = x;
2301 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002302
Pekka Paalanende685b82012-12-04 15:58:12 +02002303 if (surface->buffer_ref.buffer)
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04002304 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002305}
2306
Daniel Stone37816df2012-05-16 18:45:18 +01002307static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002308 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002309};
2310
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002311static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002312handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002313{
Daniel Stone37816df2012-05-16 18:45:18 +01002314 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002315
Daniel Stone37816df2012-05-16 18:45:18 +01002316 seat = container_of(listener, struct weston_seat,
2317 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002318
Daniel Stone37816df2012-05-16 18:45:18 +01002319 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002320}
2321
Casey Dahlin9074db52012-04-19 22:50:09 -04002322static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002323{
2324 wl_list_remove(&resource->link);
2325 free(resource);
2326}
2327
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002328static void
Daniel Stone37816df2012-05-16 18:45:18 +01002329seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2330 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002331{
Daniel Stone37816df2012-05-16 18:45:18 +01002332 struct weston_seat *seat = resource->data;
2333 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002334
Daniel Stone37816df2012-05-16 18:45:18 +01002335 if (!seat->seat.pointer)
2336 return;
2337
2338 cr = wl_client_add_object(client, &wl_pointer_interface,
2339 &pointer_interface, id, seat);
2340 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002341 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002342
2343 if (seat->seat.pointer->focus &&
2344 seat->seat.pointer->focus->resource.client == client) {
2345 struct weston_surface *surface;
2346 wl_fixed_t sx, sy;
2347
2348 surface = (struct weston_surface *) seat->seat.pointer->focus;
2349 weston_surface_from_global_fixed(surface,
2350 seat->seat.pointer->x,
2351 seat->seat.pointer->y,
2352 &sx,
2353 &sy);
2354 wl_pointer_set_focus(seat->seat.pointer,
2355 seat->seat.pointer->focus,
2356 sx,
2357 sy);
2358 }
Daniel Stone37816df2012-05-16 18:45:18 +01002359}
2360
2361static void
2362seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2363 uint32_t id)
2364{
2365 struct weston_seat *seat = resource->data;
2366 struct wl_resource *cr;
2367
2368 if (!seat->seat.keyboard)
2369 return;
2370
2371 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2372 seat);
2373 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002374 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002375
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002376 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2377 seat->xkb_info.keymap_fd,
2378 seat->xkb_info.keymap_size);
2379
Daniel Stone17b13bd2012-05-30 16:31:39 +01002380 if (seat->seat.keyboard->focus &&
2381 seat->seat.keyboard->focus->resource.client == client) {
2382 wl_keyboard_set_focus(seat->seat.keyboard,
2383 seat->seat.keyboard->focus);
Rafal Mielniczukdb790572013-02-17 15:55:55 +01002384 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone17b13bd2012-05-30 16:31:39 +01002385 }
Daniel Stone37816df2012-05-16 18:45:18 +01002386}
2387
2388static void
2389seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2390 uint32_t id)
2391{
2392 struct weston_seat *seat = resource->data;
2393 struct wl_resource *cr;
2394
2395 if (!seat->seat.touch)
2396 return;
2397
2398 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2399 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002400 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002401}
2402
2403static const struct wl_seat_interface seat_interface = {
2404 seat_get_pointer,
2405 seat_get_keyboard,
2406 seat_get_touch,
2407};
2408
2409static void
2410bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2411{
2412 struct wl_seat *seat = data;
2413 struct wl_resource *resource;
2414 enum wl_seat_capability caps = 0;
2415
2416 resource = wl_client_add_object(client, &wl_seat_interface,
2417 &seat_interface, id, data);
2418 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002419 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002420
2421 if (seat->pointer)
2422 caps |= WL_SEAT_CAPABILITY_POINTER;
2423 if (seat->keyboard)
2424 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2425 if (seat->touch)
2426 caps |= WL_SEAT_CAPABILITY_TOUCH;
2427
2428 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002429}
2430
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002431static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002432device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002433{
Daniel Stone37816df2012-05-16 18:45:18 +01002434 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002435
Daniel Stone37816df2012-05-16 18:45:18 +01002436 seat = container_of(listener, struct weston_seat,
2437 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002438
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002439 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002440}
2441
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002442static int
2443weston_compositor_xkb_init(struct weston_compositor *ec,
2444 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002445{
Daniel Stonee379da92012-05-30 16:32:04 +01002446 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002447 ec->xkb_context = xkb_context_new(0);
2448 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002449 weston_log("failed to create XKB context\n");
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002450 return -1;
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002451 }
Daniel Stone994679a2012-05-30 16:31:43 +01002452 }
2453
2454 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002455 ec->xkb_names = *names;
2456 if (!ec->xkb_names.rules)
2457 ec->xkb_names.rules = strdup("evdev");
2458 if (!ec->xkb_names.model)
2459 ec->xkb_names.model = strdup("pc105");
2460 if (!ec->xkb_names.layout)
2461 ec->xkb_names.layout = strdup("us");
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002462
2463 return 0;
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002464}
2465
Daniel Stonee379da92012-05-30 16:32:04 +01002466static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2467{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002468 if (xkb_info->keymap)
2469 xkb_map_unref(xkb_info->keymap);
2470
2471 if (xkb_info->keymap_area)
2472 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2473 if (xkb_info->keymap_fd >= 0)
2474 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002475}
2476
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002477static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2478{
Daniel Stonee379da92012-05-30 16:32:04 +01002479 free((char *) ec->xkb_names.rules);
2480 free((char *) ec->xkb_names.model);
2481 free((char *) ec->xkb_names.layout);
2482 free((char *) ec->xkb_names.variant);
2483 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002484
Daniel Stonee379da92012-05-30 16:32:04 +01002485 xkb_info_destroy(&ec->xkb_info);
2486 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002487}
2488
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002489static int
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002490weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002491{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002492 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002493
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002494 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2495 XKB_MOD_NAME_SHIFT);
2496 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2497 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002498 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2499 XKB_MOD_NAME_CTRL);
2500 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2501 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002502 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2503 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002504 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2505 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002506 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002507
2508 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2509 XKB_LED_NAME_NUM);
2510 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2511 XKB_LED_NAME_CAPS);
2512 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2513 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002514
2515 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2516 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002517 weston_log("failed to get string version of keymap\n");
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002518 return -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002519 }
2520 xkb_info->keymap_size = strlen(keymap_str) + 1;
2521
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002522 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002523 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002524 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002525 (unsigned long) xkb_info->keymap_size);
2526 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002527 }
2528
2529 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2530 PROT_READ | PROT_WRITE,
2531 MAP_SHARED, xkb_info->keymap_fd, 0);
2532 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002533 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002534 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002535 goto err_dev_zero;
2536 }
2537 strcpy(xkb_info->keymap_area, keymap_str);
2538 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002539
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002540 return 0;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002541
2542err_dev_zero:
2543 close(xkb_info->keymap_fd);
2544 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002545err_keymap_str:
2546 free(keymap_str);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002547 return -1;
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002548}
2549
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002550static int
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002551weston_compositor_build_global_keymap(struct weston_compositor *ec)
2552{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002553 if (ec->xkb_info.keymap != NULL)
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002554 return 0;
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002555
Daniel Stonee379da92012-05-30 16:32:04 +01002556 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002557 &ec->xkb_names,
2558 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002559 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002560 weston_log("failed to compile global XKB keymap\n");
2561 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002562 "options %s\n",
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002563 ec->xkb_names.rules, ec->xkb_names.model,
2564 ec->xkb_names.layout, ec->xkb_names.variant,
2565 ec->xkb_names.options);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002566 return -1;
Daniel Stone994679a2012-05-30 16:31:43 +01002567 }
2568
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002569 if (weston_xkb_info_new_keymap(&ec->xkb_info) < 0)
2570 return -1;
2571
2572 return 0;
Daniel Stone994679a2012-05-30 16:31:43 +01002573}
2574
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002575WL_EXPORT int
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002576weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002577{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002578 if (seat->has_keyboard)
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002579 return 0;
Daniel Stone994679a2012-05-30 16:31:43 +01002580
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002581 if (keymap != NULL) {
2582 seat->xkb_info.keymap = xkb_map_ref(keymap);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002583 if (weston_xkb_info_new_keymap(&seat->xkb_info) < 0)
2584 return -1;
2585 } else {
2586 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
2587 return -1;
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002588 seat->xkb_info = seat->compositor->xkb_info;
2589 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2590 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002591
2592 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2593 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002594 weston_log("failed to initialise XKB state\n");
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002595 return -1;
Daniel Stoned65b9092012-05-30 16:32:05 +01002596 }
2597
Daniel Stone71c38772012-06-22 13:21:30 +01002598 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002599
Jan Arne Petersena75a7892013-01-16 21:26:50 +01002600 wl_keyboard_init(&seat->keyboard.keyboard);
2601 wl_seat_set_keyboard(&seat->seat, &seat->keyboard.keyboard);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002602
2603 seat->has_keyboard = 1;
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002604
2605 return 0;
Daniel Stone994679a2012-05-30 16:31:43 +01002606}
2607
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002608WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002609weston_seat_init_pointer(struct weston_seat *seat)
2610{
2611 if (seat->has_pointer)
2612 return;
2613
2614 wl_pointer_init(&seat->pointer);
2615 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2616
2617 seat->has_pointer = 1;
2618}
2619
2620WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002621weston_seat_init_touch(struct weston_seat *seat)
2622{
2623 if (seat->has_touch)
2624 return;
2625
2626 wl_touch_init(&seat->touch);
2627 wl_seat_set_touch(&seat->seat, &seat->touch);
2628
2629 seat->has_touch = 1;
2630}
2631
2632WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002633weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002634{
Daniel Stone37816df2012-05-16 18:45:18 +01002635 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002636 seat->has_pointer = 0;
2637 seat->has_keyboard = 0;
2638 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002639
Daniel Stone37816df2012-05-16 18:45:18 +01002640 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2641 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002642
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002643 seat->sprite = NULL;
2644 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002645
Daniel Stone37816df2012-05-16 18:45:18 +01002646 seat->compositor = ec;
2647 seat->hotspot_x = 16;
2648 seat->hotspot_y = 16;
2649 seat->modifier_state = 0;
2650 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002651
Daniel Stone37816df2012-05-16 18:45:18 +01002652 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002653 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002654
Daniel Stone37816df2012-05-16 18:45:18 +01002655 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002656
Daniel Stone37816df2012-05-16 18:45:18 +01002657 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2658 wl_signal_add(&seat->seat.drag_icon_signal,
2659 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002660
2661 clipboard_create(seat);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002662 wl_signal_emit(&ec->seat_created_signal, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002663}
2664
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002665WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002666weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002667{
Daniel Stone37816df2012-05-16 18:45:18 +01002668 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002669 /* The global object is destroyed at wl_display_destroy() time. */
2670
Daniel Stone37816df2012-05-16 18:45:18 +01002671 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002672 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002673
Daniel Stone74419a22012-05-30 16:32:02 +01002674 if (seat->xkb_state.state != NULL)
2675 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01002676 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002677
Daniel Stone37816df2012-05-16 18:45:18 +01002678 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002679}
2680
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002681static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002682drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2683{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002684 empty_region(&es->pending.input);
2685
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002686 weston_surface_configure(es,
2687 es->geometry.x + sx, es->geometry.y + sy,
Pekka Paalanende685b82012-12-04 15:58:12 +02002688 es->buffer_ref.buffer->width,
2689 es->buffer_ref.buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002690}
2691
2692static int
Daniel Stone37816df2012-05-16 18:45:18 +01002693device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002694 struct weston_surface *surface)
2695{
Daniel Stone37816df2012-05-16 18:45:18 +01002696 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002697
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002698 if (surface->configure) {
2699 wl_resource_post_error(&surface->surface.resource,
2700 WL_DISPLAY_ERROR_INVALID_OBJECT,
2701 "surface->configure already set");
2702 return 0;
2703 }
2704
Daniel Stone37816df2012-05-16 18:45:18 +01002705 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002706
Daniel Stone37816df2012-05-16 18:45:18 +01002707 weston_surface_set_position(ws->drag_surface,
2708 wl_fixed_to_double(seat->pointer->x),
2709 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002710
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002711 surface->configure = drag_surface_configure;
2712
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002713 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002714 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002715
2716 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002717}
2718
2719static void
Daniel Stone37816df2012-05-16 18:45:18 +01002720device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002721{
Ander Conselvan de Oliveira5fd55802012-10-11 14:06:19 +03002722 if (weston_surface_is_mapped(seat->drag_surface))
2723 weston_surface_unmap(seat->drag_surface);
2724
Daniel Stone37816df2012-05-16 18:45:18 +01002725 seat->drag_surface->configure = NULL;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002726 empty_region(&seat->drag_surface->pending.input);
Daniel Stone37816df2012-05-16 18:45:18 +01002727 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2728 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002729}
2730
2731static void
Daniel Stone37816df2012-05-16 18:45:18 +01002732device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002733{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002734 struct wl_list *list;
2735
Daniel Stone37816df2012-05-16 18:45:18 +01002736 if (weston_surface_is_mapped(seat->drag_surface) ||
Pekka Paalanende685b82012-12-04 15:58:12 +02002737 !seat->drag_surface->buffer_ref.buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002738 return;
2739
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002740 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2741 list = &seat->sprite->layer_link;
2742 else
2743 list = &seat->compositor->cursor_layer.surface_list;
2744
2745 wl_list_insert(list, &seat->drag_surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002746 weston_surface_update_transform(seat->drag_surface);
Daniel Stone37816df2012-05-16 18:45:18 +01002747 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002748}
2749
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002750static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002751weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01002752 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002753{
2754 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002755
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002756 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002757 return;
2758
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002759 if (seat->drag_surface && seat->seat.drag_surface &&
2760 (&seat->drag_surface->surface.resource !=
2761 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002762 /* between calls to this funcion we got a new drag_surface */
2763 surface_changed = 1;
2764
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002765 if (!seat->seat.drag_surface || surface_changed) {
2766 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002767 if (!surface_changed)
2768 return;
2769 }
2770
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002771 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002772 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002773 seat->seat.drag_surface;
2774 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002775 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002776 }
2777
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002778 /* the client may not have attached a buffer to the drag surface
2779 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002780 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002781
2782 if (!dx && !dy)
2783 return;
2784
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002785 weston_surface_set_position(seat->drag_surface,
2786 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
2787 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002788}
2789
2790WL_EXPORT void
2791weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2792{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002793 struct weston_seat *seat;
2794
2795 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002796 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002797}
2798
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002799static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002800bind_output(struct wl_client *client,
2801 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002802{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002803 struct weston_output *output = data;
2804 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002805 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002806
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002807 resource = wl_client_add_object(client,
2808 &wl_output_interface, NULL, id, data);
2809
Casey Dahlin9074db52012-04-19 22:50:09 -04002810 wl_list_insert(&output->resource_list, &resource->link);
2811 resource->destroy = unbind_resource;
2812
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002813 wl_output_send_geometry(resource,
2814 output->x,
2815 output->y,
2816 output->mm_width,
2817 output->mm_height,
2818 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002819 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002820 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002821
2822 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002823 wl_output_send_mode(resource,
2824 mode->flags,
2825 mode->width,
2826 mode->height,
2827 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002828 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002829}
2830
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002831WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002832weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002833{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002834 struct weston_compositor *c = output->compositor;
2835
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002836 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002837 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002838 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002839
2840 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002841}
2842
Scott Moreau1bad5db2012-08-18 01:04:05 -06002843static void
2844weston_output_compute_transform(struct weston_output *output)
2845{
2846 struct weston_matrix transform;
2847 int flip;
2848
2849 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002850 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002851
2852 switch(output->transform) {
2853 case WL_OUTPUT_TRANSFORM_FLIPPED:
2854 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2855 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2856 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002857 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002858 flip = -1;
2859 break;
2860 default:
2861 flip = 1;
2862 break;
2863 }
2864
2865 switch(output->transform) {
2866 case WL_OUTPUT_TRANSFORM_NORMAL:
2867 case WL_OUTPUT_TRANSFORM_FLIPPED:
2868 transform.d[0] = flip;
2869 transform.d[1] = 0;
2870 transform.d[4] = 0;
2871 transform.d[5] = 1;
2872 break;
2873 case WL_OUTPUT_TRANSFORM_90:
2874 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2875 transform.d[0] = 0;
2876 transform.d[1] = -flip;
2877 transform.d[4] = 1;
2878 transform.d[5] = 0;
2879 break;
2880 case WL_OUTPUT_TRANSFORM_180:
2881 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2882 transform.d[0] = -flip;
2883 transform.d[1] = 0;
2884 transform.d[4] = 0;
2885 transform.d[5] = -1;
2886 break;
2887 case WL_OUTPUT_TRANSFORM_270:
2888 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2889 transform.d[0] = 0;
2890 transform.d[1] = flip;
2891 transform.d[4] = -1;
2892 transform.d[5] = 0;
2893 break;
2894 default:
2895 break;
2896 }
2897
2898 weston_matrix_multiply(&output->matrix, &transform);
2899}
2900
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002901WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002902weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002903{
Scott Moreau850ca422012-05-21 15:21:25 -06002904 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002905 struct weston_matrix camera;
2906 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002907
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002908 weston_matrix_init(&output->matrix);
2909 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002910 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2911 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002912
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002913 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002914 2.0 / (output->width + output->border.left + output->border.right),
2915 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2916
2917 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002918
Scott Moreauccbf29d2012-02-22 14:21:41 -07002919 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002920 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002921 weston_matrix_init(&camera);
2922 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002923 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002924 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002925 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002926 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002927 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002928 weston_matrix_multiply(&output->matrix, &modelview);
2929 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002930
Scott Moreauccbf29d2012-02-22 14:21:41 -07002931 output->dirty = 0;
2932}
2933
Scott Moreau1bad5db2012-08-18 01:04:05 -06002934static void
2935weston_output_transform_init(struct weston_output *output, uint32_t transform)
2936{
2937 output->transform = transform;
2938
2939 switch (transform) {
2940 case WL_OUTPUT_TRANSFORM_90:
2941 case WL_OUTPUT_TRANSFORM_270:
2942 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2943 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2944 /* Swap width and height */
2945 output->width = output->current->height;
2946 output->height = output->current->width;
2947 break;
2948 case WL_OUTPUT_TRANSFORM_NORMAL:
2949 case WL_OUTPUT_TRANSFORM_180:
2950 case WL_OUTPUT_TRANSFORM_FLIPPED:
2951 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2952 output->width = output->current->width;
2953 output->height = output->current->height;
2954 break;
2955 default:
2956 break;
2957 }
2958}
2959
Scott Moreauccbf29d2012-02-22 14:21:41 -07002960WL_EXPORT void
2961weston_output_move(struct weston_output *output, int x, int y)
2962{
2963 output->x = x;
2964 output->y = y;
2965
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002966 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002967 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002968 output->width,
2969 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002970}
2971
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002972WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002973weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002974 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002975{
2976 output->compositor = c;
2977 output->x = x;
2978 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002979 output->border.top = 0;
2980 output->border.bottom = 0;
2981 output->border.left = 0;
2982 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002983 output->mm_width = width;
2984 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002985 output->dirty = 1;
2986
Scott Moreau1bad5db2012-08-18 01:04:05 -06002987 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06002988 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002989
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002990 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002991 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002992
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002993 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002994 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002995 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002996
Casey Dahlin58ba1372012-04-19 22:50:08 -04002997 output->id = ffs(~output->compositor->output_id_pool) - 1;
2998 output->compositor->output_id_pool |= 1 << output->id;
2999
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003000 output->global =
3001 wl_display_add_global(c->wl_display, &wl_output_interface,
3002 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003003}
3004
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003005static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003006compositor_bind(struct wl_client *client,
3007 void *data, uint32_t version, uint32_t id)
3008{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003009 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003010
3011 wl_client_add_object(client, &wl_compositor_interface,
3012 &compositor_interface, id, compositor);
3013}
3014
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003015static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003016log_uname(void)
3017{
3018 struct utsname usys;
3019
3020 uname(&usys);
3021
3022 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3023 usys.version, usys.machine);
3024}
3025
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003026WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003027weston_environment_get_fd(const char *env)
3028{
3029 char *e, *end;
3030 int fd, flags;
3031
3032 e = getenv(env);
3033 if (!e)
3034 return -1;
3035 fd = strtol(e, &end, 0);
3036 if (*end != '\0')
3037 return -1;
3038
3039 flags = fcntl(fd, F_GETFD);
3040 if (flags == -1)
3041 return -1;
3042
3043 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3044 unsetenv(env);
3045
3046 return fd;
3047}
3048
3049WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003050weston_compositor_init(struct weston_compositor *ec,
3051 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003052 int *argc, char *argv[],
Daniel Stonec1be8e52012-06-01 11:14:02 -04003053 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003054{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003055 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003056 struct xkb_rule_names xkb_names;
3057 const struct config_key keyboard_config_keys[] = {
3058 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
3059 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
3060 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
3061 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
3062 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
3063 };
3064 const struct config_section cs[] = {
3065 { "keyboard",
3066 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
3067 };
3068
3069 memset(&xkb_names, 0, sizeof(xkb_names));
3070 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003071
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003072 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003073 wl_signal_init(&ec->destroy_signal);
3074 wl_signal_init(&ec->activate_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003075 wl_signal_init(&ec->kill_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003076 wl_signal_init(&ec->lock_signal);
3077 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003078 wl_signal_init(&ec->show_input_panel_signal);
3079 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003080 wl_signal_init(&ec->seat_created_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01003081 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003082
Casey Dahlin58ba1372012-04-19 22:50:08 -04003083 ec->output_id_pool = 0;
3084
Kristian Høgsberga8873122011-11-23 10:39:34 -05003085 if (!wl_display_add_global(display, &wl_compositor_interface,
3086 ec, compositor_bind))
3087 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003088
Daniel Stone725c2c32012-06-22 14:04:36 +01003089 wl_list_init(&ec->surface_list);
3090 wl_list_init(&ec->layer_list);
3091 wl_list_init(&ec->seat_list);
3092 wl_list_init(&ec->output_list);
3093 wl_list_init(&ec->key_binding_list);
3094 wl_list_init(&ec->button_binding_list);
3095 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003096 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003097 wl_list_init(&ec->fade.animation.link);
3098
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003099 weston_plane_init(&ec->primary_plane, 0, 0);
3100
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003101 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3102 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003103
3104 ec->ping_handler = NULL;
3105
3106 screenshooter_create(ec);
3107 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003108 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003109
3110 wl_data_device_manager_init(ec->wl_display);
3111
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003112 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003113
Daniel Stone725c2c32012-06-22 14:04:36 +01003114 loop = wl_display_get_event_loop(ec->wl_display);
3115 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3116 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3117
3118 ec->input_loop = wl_event_loop_create();
3119
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003120 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
3121 ec->fade.animation.frame = fade_frame;
3122
3123 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3124 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3125
3126 weston_compositor_schedule_repaint(ec);
3127
Daniel Stone725c2c32012-06-22 14:04:36 +01003128 return 0;
3129}
3130
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003131WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003132weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003133{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003134 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003135
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003136 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003137 if (ec->input_loop_source)
3138 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003139
Matt Roper361d2ad2011-08-29 13:52:23 -07003140 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003141 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003142 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003143
Daniel Stone325fc2d2012-05-30 16:31:58 +01003144 weston_binding_list_destroy_all(&ec->key_binding_list);
3145 weston_binding_list_destroy_all(&ec->button_binding_list);
3146 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003147 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003148
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003149 weston_plane_release(&ec->primary_plane);
3150
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003151 wl_array_release(&ec->vertices);
3152 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05003153 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003154
3155 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07003156}
3157
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003158WL_EXPORT void
3159weston_version(int *major, int *minor, int *micro)
3160{
3161 *major = WESTON_VERSION_MAJOR;
3162 *minor = WESTON_VERSION_MINOR;
3163 *micro = WESTON_VERSION_MICRO;
3164}
3165
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003166static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003167{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003168 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003169
Martin Minarik6d118362012-06-07 18:01:59 +02003170 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003171 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003172
3173 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003174}
3175
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003176#ifdef HAVE_LIBUNWIND
3177
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003178static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003179print_backtrace(void)
3180{
3181 unw_cursor_t cursor;
3182 unw_context_t context;
3183 unw_word_t off;
3184 unw_proc_info_t pip;
3185 int ret, i = 0;
3186 char procname[256];
3187 const char *filename;
3188 Dl_info dlinfo;
3189
3190 pip.unwind_info = NULL;
3191 ret = unw_getcontext(&context);
3192 if (ret) {
3193 weston_log("unw_getcontext: %d\n", ret);
3194 return;
3195 }
3196
3197 ret = unw_init_local(&cursor, &context);
3198 if (ret) {
3199 weston_log("unw_init_local: %d\n", ret);
3200 return;
3201 }
3202
3203 ret = unw_step(&cursor);
3204 while (ret > 0) {
3205 ret = unw_get_proc_info(&cursor, &pip);
3206 if (ret) {
3207 weston_log("unw_get_proc_info: %d\n", ret);
3208 break;
3209 }
3210
3211 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3212 if (ret && ret != -UNW_ENOMEM) {
3213 if (ret != -UNW_EUNSPEC)
3214 weston_log("unw_get_proc_name: %d\n", ret);
3215 procname[0] = '?';
3216 procname[1] = 0;
3217 }
3218
3219 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3220 *dlinfo.dli_fname)
3221 filename = dlinfo.dli_fname;
3222 else
3223 filename = "?";
3224
3225 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3226 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3227
3228 ret = unw_step(&cursor);
3229 if (ret < 0)
3230 weston_log("unw_step: %d\n", ret);
3231 }
3232}
3233
3234#else
3235
3236static void
3237print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003238{
3239 void *buffer[32];
3240 int i, count;
3241 Dl_info info;
3242
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003243 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3244 for (i = 0; i < count; i++) {
3245 dladdr(buffer[i], &info);
3246 weston_log(" [%016lx] %s (%s)\n",
3247 (long) buffer[i],
3248 info.dli_sname ? info.dli_sname : "--",
3249 info.dli_fname);
3250 }
3251}
3252
3253#endif
3254
3255static void
3256on_segv_signal(int s, siginfo_t *siginfo, void *context)
3257{
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003258 /* This SIGSEGV handler will do a best-effort backtrace, and
3259 * then call the backend restore function, which will switch
3260 * back to the vt we launched from or ungrab X etc and then
3261 * raise SIGTRAP. If we run weston under gdb from X or a
3262 * different vt, and tell gdb "handle SIGSEGV nostop", this
3263 * will allow weston to switch back to gdb on crash and then
3264 * gdb will catch the crash with SIGTRAP. */
3265
Martin Minarik6d118362012-06-07 18:01:59 +02003266 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003267
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003268 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003269
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003270 segv_compositor->restore(segv_compositor);
3271
3272 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003273}
3274
3275
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003276static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003277load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003278{
3279 char path[PATH_MAX];
3280 void *module, *init;
3281
3282 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003283 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003284 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003285 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003286
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003287 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3288 if (module) {
3289 weston_log("Module '%s' already loaded\n", path);
3290 dlclose(module);
3291 return NULL;
3292 }
3293
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003294 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003295 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003296 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003297 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003298 return NULL;
3299 }
3300
3301 init = dlsym(module, entrypoint);
3302 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003303 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003304 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003305 return NULL;
3306 }
3307
3308 return init;
3309}
3310
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003311static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003312load_modules(struct weston_compositor *ec, const char *modules,
3313 int *argc, char *argv[], const char *config_file)
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003314{
3315 const char *p, *end;
3316 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003317 int (*module_init)(struct weston_compositor *ec,
3318 int *argc, char *argv[], const char *config_file);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003319
3320 if (modules == NULL)
3321 return 0;
3322
3323 p = modules;
3324 while (*p) {
3325 end = strchrnul(p, ',');
3326 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
3327 module_init = load_module(buffer, "module_init");
3328 if (module_init)
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003329 module_init(ec, argc, argv, config_file);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003330 p = end;
3331 while (*p == ',')
3332 p++;
3333
3334 }
3335
3336 return 0;
3337}
3338
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003339static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003340 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3341
3342static const char xdg_wrong_message[] =
3343 "fatal: environment variable XDG_RUNTIME_DIR\n"
3344 "is set to \"%s\", which is not a directory.\n";
3345
3346static const char xdg_wrong_mode_message[] =
3347 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3348 "correctly. Unix access mode must be 0700 but is %o,\n"
3349 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003350 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003351
3352static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003353 "Refer to your distribution on how to get it, or\n"
3354 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3355 "on how to implement it.\n";
3356
Martin Minarik37032f82012-06-18 20:15:18 +02003357static void
3358verify_xdg_runtime_dir(void)
3359{
3360 char *dir = getenv("XDG_RUNTIME_DIR");
3361 struct stat s;
3362
3363 if (!dir) {
3364 weston_log(xdg_error_message);
3365 weston_log_continue(xdg_detail_message);
3366 exit(EXIT_FAILURE);
3367 }
3368
3369 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3370 weston_log(xdg_wrong_message, dir);
3371 weston_log_continue(xdg_detail_message);
3372 exit(EXIT_FAILURE);
3373 }
3374
3375 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3376 weston_log(xdg_wrong_mode_message,
3377 dir, s.st_mode & 0777, s.st_uid);
3378 weston_log_continue(xdg_detail_message);
3379 }
3380}
3381
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003382static int
3383usage(int error_code)
3384{
3385 fprintf(stderr,
3386 "Usage: weston [OPTIONS]\n\n"
3387 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3388 "Weston supports multiple backends, and depending on which backend is in use\n"
3389 "different options will be accepted.\n\n"
3390
3391
3392 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003393 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003394 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3395 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3396 " -S, --socket=NAME\tName of socket to listen on\n"
3397 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003398 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003399 " --log==FILE\t\tLog to the given file\n"
3400 " -h, --help\t\tThis help message\n\n");
3401
3402 fprintf(stderr,
3403 "Options for drm-backend.so:\n\n"
3404 " --connector=ID\tBring up only this connector\n"
3405 " --seat=SEAT\t\tThe seat that weston should run on\n"
3406 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003407 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003408 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3409
3410 fprintf(stderr,
3411 "Options for x11-backend.so:\n\n"
3412 " --width=WIDTH\t\tWidth of X window\n"
3413 " --height=HEIGHT\tHeight of X window\n"
3414 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003415 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003416 " --output-count=COUNT\tCreate multiple outputs\n"
3417 " --no-input\t\tDont create input devices\n\n");
3418
3419 fprintf(stderr,
3420 "Options for wayland-backend.so:\n\n"
3421 " --width=WIDTH\t\tWidth of Wayland surface\n"
3422 " --height=HEIGHT\tHeight of Wayland surface\n"
3423 " --display=DISPLAY\tWayland display to connect to\n\n");
3424
3425 exit(error_code);
3426}
3427
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003428int main(int argc, char *argv[])
3429{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003430 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003431 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003432 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003433 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003434 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003435 struct sigaction segv_action;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003436 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003437 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003438 int *argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003439 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003440 char *backend = NULL;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003441 const char *modules = "desktop-shell.so", *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003442 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003443 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003444 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003445 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003446 int32_t version = 0;
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003447 char *config_file;
3448
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003449 const struct config_key core_config_keys[] = {
3450 { "modules", CONFIG_KEY_STRING, &modules },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003451 };
3452
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003453 const struct config_section cs[] = {
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003454 { "core",
3455 core_config_keys, ARRAY_LENGTH(core_config_keys) },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003456 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003457
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003458 const struct weston_option core_options[] = {
3459 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3460 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3461 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003462 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003463 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003464 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003465 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003466 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003467
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003468 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003469
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003470 if (help)
3471 usage(EXIT_SUCCESS);
3472
Scott Moreau12245142012-08-29 15:15:58 -06003473 if (version) {
3474 printf(PACKAGE_STRING "\n");
3475 return EXIT_SUCCESS;
3476 }
3477
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003478 weston_log_file_open(log);
3479
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003480 weston_log("%s\n"
3481 STAMP_SPACE "%s\n"
3482 STAMP_SPACE "Bug reports to: %s\n"
3483 STAMP_SPACE "Build: %s\n",
3484 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003485 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003486 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003487
Martin Minarik37032f82012-06-18 20:15:18 +02003488 verify_xdg_runtime_dir();
3489
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003490 display = wl_display_create();
3491
Tiago Vignatti2116b892011-08-08 05:52:59 -07003492 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003493 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3494 display);
3495 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3496 display);
3497 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3498 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003499
3500 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003501 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3502 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003503
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003504 if (!backend) {
3505 if (getenv("WAYLAND_DISPLAY"))
3506 backend = "wayland-backend.so";
3507 else if (getenv("DISPLAY"))
3508 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003509 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003510 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003511 }
3512
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003513 config_file = config_file_path("weston.ini");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003514 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003515
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003516 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003517 if (!backend_init)
3518 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003519
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003520 ec = backend_init(display, &argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003521 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003522 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003523 exit(EXIT_FAILURE);
3524 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003525
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003526 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3527 segv_action.sa_sigaction = on_segv_signal;
3528 sigemptyset(&segv_action.sa_mask);
3529 sigaction(SIGSEGV, &segv_action, NULL);
3530 segv_compositor = ec;
3531
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003532 ec->option_idle_time = idle_time;
3533 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003534
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003535 setenv("WAYLAND_DISPLAY", socket_name, 1);
3536
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003537 if (load_modules(ec, modules, &argc, argv, config_file) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003538 goto out;
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003539 if (load_modules(ec, option_modules, &argc, argv, config_file) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003540 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003541
Kristian Høgsberg142d5462013-02-21 21:10:36 -05003542 free(config_file);
3543
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003544 for (i = 1; i < argc; i++)
3545 weston_log("fatal: unhandled option: %s\n", argv[i]);
3546 if (argc > 1) {
3547 ret = EXIT_FAILURE;
3548 goto out;
3549 }
3550
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003551 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003552 weston_log("fatal: failed to add socket: %m\n");
3553 ret = EXIT_FAILURE;
3554 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003555 }
3556
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003557 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003558 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003559
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003560 wl_display_run(display);
3561
3562 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003563 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05003564 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003565
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003566 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003567
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003568 for (i = ARRAY_LENGTH(signals); i;)
3569 wl_event_source_remove(signals[--i]);
3570
Daniel Stone855028f2012-05-01 20:37:10 +01003571 weston_compositor_xkb_destroy(ec);
3572
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003573 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003574 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003575
Martin Minarik19e6f262012-06-07 13:08:46 +02003576 weston_log_file_close();
3577
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003578 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003579}