blob: a2860fd018ec54fd8e2ee2038643089562dafb4d [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;
Giulio Camuffo184df502013-02-21 11:29:21 +0100293 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200294
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400295 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500296 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500297 pixman_region32_init(&surface->clip);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300298 region_init_infinite(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200299 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500300 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400301
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200302 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200303 wl_list_insert(&surface->geometry.transformation_list,
304 &surface->transform.position.link);
305 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200306 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200307 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500308
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300309 surface->pending.buffer_destroy_listener.notify =
310 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300311 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300312 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300313 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300314 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300315
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400316 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500317}
318
Alex Wu8811bf92012-02-28 18:07:54 +0800319WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500320weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200321 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500322{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100323 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500324}
325
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400326WL_EXPORT void
327weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200328 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200329{
330 if (surface->transform.enabled) {
331 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
332
333 weston_matrix_transform(&surface->transform.matrix, &v);
334
335 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200336 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700337 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200338 v.f[3]);
339 *x = 0;
340 *y = 0;
341 return;
342 }
343
344 *x = v.f[0] / v.f[3];
345 *y = v.f[1] / v.f[3];
346 } else {
347 *x = sx + surface->geometry.x;
348 *y = sy + surface->geometry.y;
349 }
350}
351
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500352WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200353weston_transformed_coord(int width, int height,
354 enum wl_output_transform transform,
355 float sx, float sy, float *bx, float *by)
356{
357 switch (transform) {
358 case WL_OUTPUT_TRANSFORM_NORMAL:
359 default:
360 *bx = sx;
361 *by = sy;
362 break;
363 case WL_OUTPUT_TRANSFORM_FLIPPED:
364 *bx = width - sx;
365 *by = sy;
366 break;
367 case WL_OUTPUT_TRANSFORM_90:
368 *bx = height - sy;
369 *by = sx;
370 break;
371 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
372 *bx = height - sy;
373 *by = width - sx;
374 break;
375 case WL_OUTPUT_TRANSFORM_180:
376 *bx = width - sx;
377 *by = height - sy;
378 break;
379 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
380 *bx = sx;
381 *by = height - sy;
382 break;
383 case WL_OUTPUT_TRANSFORM_270:
384 *bx = sy;
385 *by = width - sx;
386 break;
387 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
388 *bx = sy;
389 *by = sx;
390 break;
391 }
392}
393
394WL_EXPORT pixman_box32_t
395weston_transformed_rect(int width, int height,
396 enum wl_output_transform transform,
397 pixman_box32_t rect)
398{
399 float x1, x2, y1, y2;
400
401 pixman_box32_t ret;
402
403 weston_transformed_coord(width, height, transform,
404 rect.x1, rect.y1, &x1, &y1);
405 weston_transformed_coord(width, height, transform,
406 rect.x2, rect.y2, &x2, &y2);
407
408 if (x1 <= x2) {
409 ret.x1 = x1;
410 ret.x2 = x2;
411 } else {
412 ret.x1 = x2;
413 ret.x2 = x1;
414 }
415
416 if (y1 <= y2) {
417 ret.y1 = y1;
418 ret.y2 = y2;
419 } else {
420 ret.y1 = y2;
421 ret.y2 = y1;
422 }
423
424 return ret;
425}
426
427WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200428weston_surface_to_buffer_float(struct weston_surface *surface,
429 float sx, float sy, float *bx, float *by)
430{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200431 weston_transformed_coord(surface->geometry.width,
432 surface->geometry.height,
433 surface->buffer_transform,
434 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200435}
436
437WL_EXPORT pixman_box32_t
438weston_surface_to_buffer_rect(struct weston_surface *surface,
439 pixman_box32_t rect)
440{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200441 return weston_transformed_rect(surface->geometry.width,
442 surface->geometry.height,
443 surface->buffer_transform, rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200444}
445
446WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400447weston_surface_move_to_plane(struct weston_surface *surface,
448 struct weston_plane *plane)
449{
450 if (surface->plane == plane)
451 return;
452
453 weston_surface_damage_below(surface);
454 surface->plane = plane;
455 weston_surface_damage(surface);
456}
457
458WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500459weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200460{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500461 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200462
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500463 pixman_region32_init(&damage);
464 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
465 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400466 pixman_region32_union(&surface->plane->damage,
467 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500468 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200469}
470
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400471static struct wl_resource *
472find_resource_for_client(struct wl_list *list, struct wl_client *client)
473{
474 struct wl_resource *r;
475
476 wl_list_for_each(r, list, link) {
477 if (r->client == client)
478 return r;
479 }
480
481 return NULL;
482}
483
484static void
485weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
486{
487 uint32_t different = es->output_mask ^ mask;
488 uint32_t entered = mask & different;
489 uint32_t left = es->output_mask & different;
490 struct weston_output *output;
491 struct wl_resource *resource = NULL;
492 struct wl_client *client = es->surface.resource.client;
493
494 es->output_mask = mask;
495 if (es->surface.resource.client == NULL)
496 return;
497 if (different == 0)
498 return;
499
500 wl_list_for_each(output, &es->compositor->output_list, link) {
501 if (1 << output->id & different)
502 resource =
503 find_resource_for_client(&output->resource_list,
504 client);
505 if (resource == NULL)
506 continue;
507 if (1 << output->id & entered)
508 wl_surface_send_enter(&es->surface.resource, resource);
509 if (1 << output->id & left)
510 wl_surface_send_leave(&es->surface.resource, resource);
511 }
512}
513
514static void
515weston_surface_assign_output(struct weston_surface *es)
516{
517 struct weston_compositor *ec = es->compositor;
518 struct weston_output *output, *new_output;
519 pixman_region32_t region;
520 uint32_t max, area, mask;
521 pixman_box32_t *e;
522
523 new_output = NULL;
524 max = 0;
525 mask = 0;
526 pixman_region32_init(&region);
527 wl_list_for_each(output, &ec->output_list, link) {
528 pixman_region32_intersect(&region, &es->transform.boundingbox,
529 &output->region);
530
531 e = pixman_region32_extents(&region);
532 area = (e->x2 - e->x1) * (e->y2 - e->y1);
533
534 if (area > 0)
535 mask |= 1 << output->id;
536
537 if (area >= max) {
538 new_output = output;
539 max = area;
540 }
541 }
542 pixman_region32_fini(&region);
543
544 es->output = new_output;
545 weston_surface_update_output_mask(es, mask);
546}
547
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200548static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200549surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
550 int32_t width, int32_t height,
551 pixman_region32_t *bbox)
552{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200553 float min_x = HUGE_VALF, min_y = HUGE_VALF;
554 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200555 int32_t s[4][2] = {
556 { sx, sy },
557 { sx, sy + height },
558 { sx + width, sy },
559 { sx + width, sy + height }
560 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200561 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200562 int i;
563
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300564 if (width == 0 || height == 0) {
565 /* avoid rounding empty bbox to 1x1 */
566 pixman_region32_init(bbox);
567 return;
568 }
569
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200570 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200571 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400572 weston_surface_to_global_float(surface,
573 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200574 if (x < min_x)
575 min_x = x;
576 if (x > max_x)
577 max_x = x;
578 if (y < min_y)
579 min_y = y;
580 if (y > max_y)
581 max_y = y;
582 }
583
Pekka Paalanen219b9822012-02-08 15:38:37 +0200584 int_x = floorf(min_x);
585 int_y = floorf(min_y);
586 pixman_region32_init_rect(bbox, int_x, int_y,
587 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200588}
589
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200590static void
591weston_surface_update_transform_disable(struct weston_surface *surface)
592{
593 surface->transform.enabled = 0;
594
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200595 /* round off fractions when not transformed */
596 surface->geometry.x = roundf(surface->geometry.x);
597 surface->geometry.y = roundf(surface->geometry.y);
598
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500599 /* Otherwise identity matrix, but with x and y translation. */
600 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
601 surface->transform.position.matrix.d[12] = surface->geometry.x;
602 surface->transform.position.matrix.d[13] = surface->geometry.y;
603
604 surface->transform.matrix = surface->transform.position.matrix;
605
Kristian Høgsberg9bcaaeb2013-02-28 14:56:43 -0500606 surface->transform.inverse = surface->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500607 surface->transform.inverse.d[12] = -surface->geometry.x;
608 surface->transform.inverse.d[13] = -surface->geometry.y;
609
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200610 pixman_region32_init_rect(&surface->transform.boundingbox,
611 surface->geometry.x,
612 surface->geometry.y,
613 surface->geometry.width,
614 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500615
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400616 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500617 pixman_region32_copy(&surface->transform.opaque,
618 &surface->opaque);
619 pixman_region32_translate(&surface->transform.opaque,
620 surface->geometry.x,
621 surface->geometry.y);
622 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200623}
624
625static int
626weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200627{
628 struct weston_matrix *matrix = &surface->transform.matrix;
629 struct weston_matrix *inverse = &surface->transform.inverse;
630 struct weston_transform *tform;
631
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200632 surface->transform.enabled = 1;
633
634 /* Otherwise identity matrix, but with x and y translation. */
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300635 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200636 surface->transform.position.matrix.d[12] = surface->geometry.x;
637 surface->transform.position.matrix.d[13] = surface->geometry.y;
638
639 weston_matrix_init(matrix);
640 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
641 weston_matrix_multiply(matrix, &tform->matrix);
642
643 if (weston_matrix_invert(inverse, matrix) < 0) {
644 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200645 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200646 " transformation not invertible.\n", surface);
647 return -1;
648 }
649
650 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
651 surface->geometry.height,
652 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500653
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200654 return 0;
655}
656
657WL_EXPORT void
658weston_surface_update_transform(struct weston_surface *surface)
659{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200660 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200661 return;
662
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200663 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200664
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500665 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200666
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200667 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500668 pixman_region32_fini(&surface->transform.opaque);
669 pixman_region32_init(&surface->transform.opaque);
670
Pekka Paalanencd403622012-01-25 13:37:39 +0200671 /* transform.position is always in transformation_list */
672 if (surface->geometry.transformation_list.next ==
673 &surface->transform.position.link &&
674 surface->geometry.transformation_list.prev ==
675 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200676 weston_surface_update_transform_disable(surface);
677 } else {
678 if (weston_surface_update_transform_enable(surface) < 0)
679 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200680 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200681
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400682 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200683
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300684 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200685}
686
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200687WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100688weston_surface_to_global_fixed(struct weston_surface *surface,
689 wl_fixed_t sx, wl_fixed_t sy,
690 wl_fixed_t *x, wl_fixed_t *y)
691{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200692 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100693
694 weston_surface_to_global_float(surface,
695 wl_fixed_to_double(sx),
696 wl_fixed_to_double(sy),
697 &xf, &yf);
698 *x = wl_fixed_from_double(xf);
699 *y = wl_fixed_from_double(yf);
700}
701
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400702WL_EXPORT void
703weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200704 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200705{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200706 if (surface->transform.enabled) {
707 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
708
709 weston_matrix_transform(&surface->transform.inverse, &v);
710
711 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200712 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200713 "weston_surface_from_global(), divisor = %g\n",
714 v.f[3]);
715 *sx = 0;
716 *sy = 0;
717 return;
718 }
719
Pekka Paalanencd403622012-01-25 13:37:39 +0200720 *sx = v.f[0] / v.f[3];
721 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200722 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200723 *sx = x - surface->geometry.x;
724 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200725 }
726}
727
728WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100729weston_surface_from_global_fixed(struct weston_surface *surface,
730 wl_fixed_t x, wl_fixed_t y,
731 wl_fixed_t *sx, wl_fixed_t *sy)
732{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200733 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100734
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400735 weston_surface_from_global_float(surface,
736 wl_fixed_to_double(x),
737 wl_fixed_to_double(y),
738 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100739 *sx = wl_fixed_from_double(sxf);
740 *sy = wl_fixed_from_double(syf);
741}
742
743WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200744weston_surface_from_global(struct weston_surface *surface,
745 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
746{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200747 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200748
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400749 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200750 *sx = floorf(sxf);
751 *sy = floorf(syf);
752}
753
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400754WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400755weston_surface_schedule_repaint(struct weston_surface *surface)
756{
757 struct weston_output *output;
758
759 wl_list_for_each(output, &surface->compositor->output_list, link)
760 if (surface->output_mask & (1 << output->id))
761 weston_output_schedule_repaint(output);
762}
763
764WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500765weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500766{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400767 pixman_region32_union_rect(&surface->damage, &surface->damage,
768 0, 0, surface->geometry.width,
769 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200770
Kristian Høgsberg98238702012-08-03 16:29:12 -0400771 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500772}
773
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400774WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500775weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200776 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400777{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200778 surface->geometry.x = x;
779 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200780 surface->geometry.width = width;
781 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200782 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400783}
784
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200785WL_EXPORT void
786weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200787 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200788{
789 surface->geometry.x = x;
790 surface->geometry.y = y;
791 surface->geometry.dirty = 1;
792}
793
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300794WL_EXPORT int
795weston_surface_is_mapped(struct weston_surface *surface)
796{
797 if (surface->output)
798 return 1;
799 else
800 return 0;
801}
802
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200803WL_EXPORT int32_t
804weston_surface_buffer_width(struct weston_surface *surface)
805{
806 switch (surface->buffer_transform) {
807 case WL_OUTPUT_TRANSFORM_90:
808 case WL_OUTPUT_TRANSFORM_270:
809 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
810 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200811 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200812 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200813 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200814 }
815}
816
817WL_EXPORT int32_t
818weston_surface_buffer_height(struct weston_surface *surface)
819{
820 switch (surface->buffer_transform) {
821 case WL_OUTPUT_TRANSFORM_90:
822 case WL_OUTPUT_TRANSFORM_270:
823 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
824 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200825 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200826 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200827 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200828 }
829}
830
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400831WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500832weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500833{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400834 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500835
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400836 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500837
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400838 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500839}
840
Tiago Vignatti9d393522012-02-10 16:26:19 +0200841static struct weston_surface *
842weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100843 wl_fixed_t x, wl_fixed_t y,
844 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200845{
846 struct weston_surface *surface;
847
848 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100849 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500850 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100851 wl_fixed_to_int(*sx),
852 wl_fixed_to_int(*sy),
853 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200854 return surface;
855 }
856
857 return NULL;
858}
859
860static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400861weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500862{
Scott Moreau447013d2012-02-18 05:05:29 -0700863 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500864 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400865 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500866
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400867 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100868 return;
869
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400870 surface = weston_compositor_pick_surface(seat->compositor,
871 pointer->x,
872 pointer->y,
873 &pointer->current_x,
874 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500875
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400876 if (&surface->surface != pointer->current) {
877 interface = pointer->grab->interface;
878 pointer->current = &surface->surface;
879 interface->focus(pointer->grab, &surface->surface,
880 pointer->current_x,
881 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500882 }
883
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400884 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500885 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100886 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400887 pointer->x,
888 pointer->y,
889 &pointer->grab->x,
890 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500891}
892
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400893static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500894weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400895{
Daniel Stone37816df2012-05-16 18:45:18 +0100896 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400897
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500898 if (!compositor->focus)
899 return;
900
Daniel Stone37816df2012-05-16 18:45:18 +0100901 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400902 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400903}
904
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400905WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500906weston_surface_unmap(struct weston_surface *surface)
907{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100908 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500909
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500910 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500911 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500912 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500913
Daniel Stone4dab5db2012-05-30 16:31:53 +0100914 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100915 if (seat->seat.keyboard &&
916 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100917 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100918 if (seat->seat.pointer &&
919 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100920 wl_pointer_set_focus(seat->seat.pointer,
921 NULL,
922 wl_fixed_from_int(0),
923 wl_fixed_from_int(0));
924 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500925
Kristian Høgsberg98238702012-08-03 16:29:12 -0400926 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500927}
928
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400929struct weston_frame_callback {
930 struct wl_resource resource;
931 struct wl_list link;
932};
933
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500934static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400935destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500936{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500937 struct weston_surface *surface =
938 container_of(resource,
939 struct weston_surface, surface.resource);
940 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400941 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400942
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300943 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500944 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400945
Pekka Paalanenbc106382012-10-10 12:49:31 +0300946 wl_list_for_each_safe(cb, next,
947 &surface->pending.frame_callback_list, link)
948 wl_resource_destroy(&cb->resource);
949
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300950 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300951 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +0300952 pixman_region32_fini(&surface->pending.damage);
953
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300954 if (surface->pending.buffer)
955 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
956
Pekka Paalanende685b82012-12-04 15:58:12 +0200957 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400958
Kristian Høgsberg42263852012-09-06 21:59:29 -0400959 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200960
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200961 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200962 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500963 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500964 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300965 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200966
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400967 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
968 wl_resource_destroy(&cb->resource);
969
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400970 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500971}
972
Alex Wu8811bf92012-02-28 18:07:54 +0800973WL_EXPORT void
974weston_surface_destroy(struct weston_surface *surface)
975{
976 /* Not a valid way to destroy a client surface */
977 assert(surface->surface.resource.client == NULL);
978
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400979 wl_signal_emit(&surface->surface.resource.destroy_signal,
980 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800981 destroy_surface(&surface->surface.resource);
982}
983
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100984static void
Pekka Paalanende685b82012-12-04 15:58:12 +0200985weston_buffer_reference_handle_destroy(struct wl_listener *listener,
986 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100987{
Pekka Paalanende685b82012-12-04 15:58:12 +0200988 struct weston_buffer_reference *ref =
989 container_of(listener, struct weston_buffer_reference,
990 destroy_listener);
991
992 assert((struct wl_buffer *)data == ref->buffer);
993 ref->buffer = NULL;
994}
995
996WL_EXPORT void
997weston_buffer_reference(struct weston_buffer_reference *ref,
998 struct wl_buffer *buffer)
999{
1000 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001001 ref->buffer->busy_count--;
1002 if (ref->buffer->busy_count == 0) {
1003 assert(ref->buffer->resource.client != NULL);
1004 wl_resource_queue_event(&ref->buffer->resource,
1005 WL_BUFFER_RELEASE);
1006 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001007 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001008 }
1009
Pekka Paalanende685b82012-12-04 15:58:12 +02001010 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001011 buffer->busy_count++;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001012 wl_signal_add(&buffer->resource.destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001013 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001014 }
1015
Pekka Paalanende685b82012-12-04 15:58:12 +02001016 ref->buffer = buffer;
1017 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1018}
1019
1020static void
1021weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
1022{
1023 weston_buffer_reference(&surface->buffer_ref, buffer);
1024
Pekka Paalanena6421c42012-12-04 15:58:10 +02001025 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001026 if (weston_surface_is_mapped(surface))
1027 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001028 }
1029
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001030 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001031}
1032
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001033WL_EXPORT void
1034weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001035{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001036 wl_list_remove(&surface->layer_link);
1037 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001038 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001039 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001040}
1041
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001042WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001043weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001044{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001045 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001046
1047 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001048 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001049}
1050
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001051WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001052weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001053{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001054 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001055
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001056 pixman_region32_union(&compositor->primary_plane.damage,
1057 &compositor->primary_plane.damage,
1058 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001059 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001060}
1061
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001062static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001063surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001064 pixman_region32_t *opaque)
1065{
Pekka Paalanende685b82012-12-04 15:58:12 +02001066 if (surface->buffer_ref.buffer &&
1067 wl_buffer_is_shm(surface->buffer_ref.buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001068 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001069
1070 if (surface->transform.enabled) {
1071 pixman_box32_t *extents;
1072
1073 extents = pixman_region32_extents(&surface->damage);
1074 surface_compute_bbox(surface, extents->x1, extents->y1,
1075 extents->x2 - extents->x1,
1076 extents->y2 - extents->y1,
1077 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001078 pixman_region32_translate(&surface->damage,
1079 -surface->plane->x,
1080 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001081 } else {
1082 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001083 surface->geometry.x - surface->plane->x,
1084 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001085 }
1086
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001087 pixman_region32_subtract(&surface->damage,
1088 &surface->damage, &surface->plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001089 pixman_region32_union(&surface->plane->damage,
1090 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001091 empty_region(&surface->damage);
1092 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001093 pixman_region32_union(&surface->plane->opaque,
1094 &surface->plane->opaque,
1095 &surface->transform.opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001096 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001097}
1098
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001099static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001100weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001101{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001102 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001103 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001104 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001105 struct weston_animation *animation, *next;
1106 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001107 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001108 pixman_region32_t opaque, output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001109
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001110 weston_compositor_update_drag_surfaces(ec);
1111
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001112 /* Rebuild the surface list and update surface transforms up front. */
1113 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +02001114 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001115 wl_list_for_each(layer, &ec->layer_list, link) {
1116 wl_list_for_each(es, &layer->surface_list, layer_link) {
1117 weston_surface_update_transform(es);
1118 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +02001119 if (es->output == output) {
1120 wl_list_insert_list(&frame_callback_list,
1121 &es->frame_callback_list);
1122 wl_list_init(&es->frame_callback_list);
1123 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001124 }
1125 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001126
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001127 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001128 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001129 else
1130 wl_list_for_each(es, &ec->surface_list, link)
1131 weston_surface_move_to_plane(es, &ec->primary_plane);
1132
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001133 pixman_region32_init(&opaque);
1134
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001135 pixman_region32_fini(&ec->primary_plane.opaque);
1136 pixman_region32_init(&ec->primary_plane.opaque);
1137
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001138 wl_list_for_each(es, &ec->surface_list, link) {
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001139 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001140
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001141 /* Both the renderer and the backend have seen the buffer
1142 * by now. If renderer needs the buffer, it has its own
1143 * reference set. If the backend wants to keep the buffer
1144 * around for migrating the surface into a non-primary plane
1145 * later, keep_buffer is true. Otherwise, drop the core
1146 * reference now, and allow early buffer release. This enables
1147 * clients to use single-buffering.
1148 */
1149 if (!es->keep_buffer)
1150 weston_buffer_reference(&es->buffer_ref, NULL);
1151 }
1152
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -04001153 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001154
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001155 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001156 pixman_region32_intersect(&output_damage,
1157 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001158
Scott Moreauccbf29d2012-02-22 14:21:41 -07001159 if (output->dirty)
1160 weston_output_update_matrix(output);
1161
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001162 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001163
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001164 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001165
Kristian Høgsbergef044142011-06-21 15:02:12 -04001166 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001167
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001168 weston_compositor_repick(ec);
1169 wl_event_loop_dispatch(ec->input_loop, 0);
1170
Jonas Ådahldb773762012-06-13 00:01:21 +02001171 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001172 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001173 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001174 }
1175
Scott Moreaud64cf212012-06-08 19:40:54 -06001176 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001177 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001178 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001179 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001180}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001181
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001182static int
1183weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001184{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001185 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001186
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001187 wl_event_loop_dispatch(compositor->input_loop, 0);
1188
1189 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001190}
1191
1192WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001193weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001194{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001195 struct weston_compositor *compositor = output->compositor;
1196 struct wl_event_loop *loop =
1197 wl_display_get_event_loop(compositor->wl_display);
1198 int fd;
1199
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001200 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001201 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001202 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001203 return;
1204 }
1205
1206 output->repaint_scheduled = 0;
1207 if (compositor->input_loop_source)
1208 return;
1209
1210 fd = wl_event_loop_get_fd(compositor->input_loop);
1211 compositor->input_loop_source =
1212 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1213 weston_compositor_read_input, compositor);
1214}
1215
1216static void
1217idle_repaint(void *data)
1218{
1219 struct weston_output *output = data;
1220
1221 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001222}
1223
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001224WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001225weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1226{
1227 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001228 if (below != NULL)
1229 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001230}
1231
1232WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001233weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001234{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001235 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001236 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001237
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001238 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001239 return;
1240
Kristian Høgsbergef044142011-06-21 15:02:12 -04001241 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001242 output->repaint_needed = 1;
1243 if (output->repaint_scheduled)
1244 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001245
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001246 wl_event_loop_add_idle(loop, idle_repaint, output);
1247 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001248
1249 if (compositor->input_loop_source) {
1250 wl_event_source_remove(compositor->input_loop_source);
1251 compositor->input_loop_source = NULL;
1252 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001253}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001254
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001255WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001256weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1257{
1258 struct weston_output *output;
1259
1260 wl_list_for_each(output, &compositor->output_list, link)
1261 weston_output_schedule_repaint(output);
1262}
1263
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001264static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001265surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001266{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001267 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001268}
1269
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001270static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001271surface_attach(struct wl_client *client,
1272 struct wl_resource *resource,
1273 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1274{
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001275 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001276 struct wl_buffer *buffer = NULL;
1277
1278 if (buffer_resource)
1279 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001280
Pekka Paalanende685b82012-12-04 15:58:12 +02001281 /* Attach, attach, without commit in between does not send
1282 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001283 if (surface->pending.buffer)
1284 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001285
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001286 surface->pending.sx = sx;
1287 surface->pending.sy = sy;
1288 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001289 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001290 if (buffer) {
1291 wl_signal_add(&buffer->resource.destroy_signal,
1292 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001293 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001294}
1295
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001296static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001297surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001298 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001299 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001300{
Pekka Paalanen8e159182012-10-10 12:49:25 +03001301 struct weston_surface *surface = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001302
Pekka Paalanen8e159182012-10-10 12:49:25 +03001303 pixman_region32_union_rect(&surface->pending.damage,
1304 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001305 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001306}
1307
Kristian Høgsberg33418202011-08-16 23:01:28 -04001308static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001309destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001310{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001311 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001312
1313 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001314 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001315}
1316
1317static void
1318surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001319 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001320{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001321 struct weston_frame_callback *cb;
Pekka Paalanenbc106382012-10-10 12:49:31 +03001322 struct weston_surface *surface = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001323
1324 cb = malloc(sizeof *cb);
1325 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001326 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001327 return;
1328 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001329
Kristian Høgsberg33418202011-08-16 23:01:28 -04001330 cb->resource.object.interface = &wl_callback_interface;
1331 cb->resource.object.id = callback;
1332 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001333 cb->resource.client = client;
1334 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001335
1336 wl_client_add_resource(client, &cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001337 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001338}
1339
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001340static void
1341surface_set_opaque_region(struct wl_client *client,
1342 struct wl_resource *resource,
1343 struct wl_resource *region_resource)
1344{
1345 struct weston_surface *surface = resource->data;
1346 struct weston_region *region;
1347
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001348 if (region_resource) {
1349 region = region_resource->data;
Pekka Paalanen512dde82012-10-10 12:49:27 +03001350 pixman_region32_copy(&surface->pending.opaque,
1351 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001352 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001353 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001354 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001355}
1356
1357static void
1358surface_set_input_region(struct wl_client *client,
1359 struct wl_resource *resource,
1360 struct wl_resource *region_resource)
1361{
1362 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001363 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001364
1365 if (region_resource) {
1366 region = region_resource->data;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001367 pixman_region32_copy(&surface->pending.input,
1368 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001369 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001370 pixman_region32_fini(&surface->pending.input);
1371 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001372 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001373}
1374
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001375static int
1376surface_pending_buffer_has_different_size(struct weston_surface *surface)
1377{
1378 int width, height;
1379
1380 switch (surface->pending.buffer_transform) {
1381 case WL_OUTPUT_TRANSFORM_90:
1382 case WL_OUTPUT_TRANSFORM_270:
1383 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1384 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1385 height = surface->pending.buffer->width;
1386 width = surface->pending.buffer->height;
1387 break;
1388 default:
1389 width = surface->pending.buffer->width;
1390 height = surface->pending.buffer->height;
1391 }
1392
1393 if (width == surface->geometry.width &&
1394 height == surface->geometry.height)
1395 return 0;
1396 else
1397 return 1;
1398}
1399
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001400static void
1401surface_commit(struct wl_client *client, struct wl_resource *resource)
1402{
1403 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001404 pixman_region32_t opaque;
Giulio Camuffo184df502013-02-21 11:29:21 +01001405 int buffer_width = 0;
1406 int buffer_height = 0;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001407
1408 if (surface->pending.sx || surface->pending.sy ||
1409 (surface->pending.buffer &&
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001410 surface_pending_buffer_has_different_size(surface)))
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001411 surface->geometry.dirty = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001412
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001413 /* wl_surface.set_buffer_rotation */
1414 surface->buffer_transform = surface->pending.buffer_transform;
1415
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001416 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001417 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001418 weston_surface_attach(surface, surface->pending.buffer);
1419
Giulio Camuffo184df502013-02-21 11:29:21 +01001420 if (surface->buffer_ref.buffer) {
1421 buffer_width = weston_surface_buffer_width(surface);
1422 buffer_height = weston_surface_buffer_height(surface);
1423 }
1424
1425 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001426 surface->configure(surface, surface->pending.sx,
Giulio Camuffo184df502013-02-21 11:29:21 +01001427 surface->pending.sy, buffer_width, buffer_height);
1428
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001429 if (surface->pending.buffer)
1430 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1431 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001432 surface->pending.sx = 0;
1433 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001434 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001435
1436 /* wl_surface.damage */
1437 pixman_region32_union(&surface->damage, &surface->damage,
1438 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001439 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1440 0, 0,
1441 surface->geometry.width,
1442 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001443 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001444
1445 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001446 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001447 surface->geometry.width,
1448 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001449 pixman_region32_intersect(&opaque,
1450 &opaque, &surface->pending.opaque);
1451
1452 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1453 pixman_region32_copy(&surface->opaque, &opaque);
1454 surface->geometry.dirty = 1;
1455 }
1456
1457 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001458
1459 /* wl_surface.set_input_region */
1460 pixman_region32_fini(&surface->input);
1461 pixman_region32_init_rect(&surface->input, 0, 0,
1462 surface->geometry.width,
1463 surface->geometry.height);
1464 pixman_region32_intersect(&surface->input,
1465 &surface->input, &surface->pending.input);
1466
Pekka Paalanenbc106382012-10-10 12:49:31 +03001467 /* wl_surface.frame */
1468 wl_list_insert_list(&surface->frame_callback_list,
1469 &surface->pending.frame_callback_list);
1470 wl_list_init(&surface->pending.frame_callback_list);
1471
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001472 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001473}
1474
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001475static void
1476surface_set_buffer_transform(struct wl_client *client,
1477 struct wl_resource *resource, int transform)
1478{
1479 struct weston_surface *surface = resource->data;
1480
1481 surface->pending.buffer_transform = transform;
1482}
1483
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001484static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001485 surface_destroy,
1486 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001487 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001488 surface_frame,
1489 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001490 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001491 surface_commit,
1492 surface_set_buffer_transform
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001493};
1494
1495static void
1496compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001497 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001498{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001499 struct weston_compositor *ec = resource->data;
1500 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001501
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001502 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001503 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001504 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001505 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001506 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001507
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001508 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001509
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001510 surface->surface.resource.object.id = id;
1511 surface->surface.resource.object.interface = &wl_surface_interface;
1512 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001513 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001514 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001515
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001516 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001517}
1518
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001519static void
1520destroy_region(struct wl_resource *resource)
1521{
1522 struct weston_region *region =
1523 container_of(resource, struct weston_region, resource);
1524
1525 pixman_region32_fini(&region->region);
1526 free(region);
1527}
1528
1529static void
1530region_destroy(struct wl_client *client, struct wl_resource *resource)
1531{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001532 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001533}
1534
1535static void
1536region_add(struct wl_client *client, struct wl_resource *resource,
1537 int32_t x, int32_t y, int32_t width, int32_t height)
1538{
1539 struct weston_region *region = resource->data;
1540
1541 pixman_region32_union_rect(&region->region, &region->region,
1542 x, y, width, height);
1543}
1544
1545static void
1546region_subtract(struct wl_client *client, struct wl_resource *resource,
1547 int32_t x, int32_t y, int32_t width, int32_t height)
1548{
1549 struct weston_region *region = resource->data;
1550 pixman_region32_t rect;
1551
1552 pixman_region32_init_rect(&rect, x, y, width, height);
1553 pixman_region32_subtract(&region->region, &region->region, &rect);
1554 pixman_region32_fini(&rect);
1555}
1556
1557static const struct wl_region_interface region_interface = {
1558 region_destroy,
1559 region_add,
1560 region_subtract
1561};
1562
1563static void
1564compositor_create_region(struct wl_client *client,
1565 struct wl_resource *resource, uint32_t id)
1566{
1567 struct weston_region *region;
1568
1569 region = malloc(sizeof *region);
1570 if (region == NULL) {
1571 wl_resource_post_no_memory(resource);
1572 return;
1573 }
1574
1575 region->resource.destroy = destroy_region;
1576
1577 region->resource.object.id = id;
1578 region->resource.object.interface = &wl_region_interface;
1579 region->resource.object.implementation =
1580 (void (**)(void)) &region_interface;
1581 region->resource.data = region;
1582
1583 pixman_region32_init(&region->region);
1584
1585 wl_client_add_resource(client, &region->resource);
1586}
1587
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001588static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001589 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001590 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001591};
1592
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001593static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02001594weston_compositor_dpms(struct weston_compositor *compositor,
1595 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001596{
1597 struct weston_output *output;
1598
1599 wl_list_for_each(output, &compositor->output_list, link)
1600 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02001601 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001602}
1603
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001604WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02001605weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001606{
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02001607 switch (compositor->state) {
1608 case WESTON_COMPOSITOR_SLEEPING:
1609 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
1610 /* fall through */
1611 case WESTON_COMPOSITOR_IDLE:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02001612 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02001613 /* fall through */
1614 default:
1615 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1616 wl_event_source_timer_update(compositor->idle_source,
1617 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001618 }
1619}
1620
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02001621WL_EXPORT void
1622weston_compositor_sleep(struct weston_compositor *compositor)
1623{
1624 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
1625 return;
1626
1627 compositor->state = WESTON_COMPOSITOR_SLEEPING;
1628 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
1629}
1630
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001631static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001632weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001633{
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02001634 weston_compositor_wake(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001635 compositor->idle_inhibit++;
1636}
1637
1638static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001639weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001640{
1641 compositor->idle_inhibit--;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02001642 weston_compositor_wake(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001643}
1644
1645static int
1646idle_handler(void *data)
1647{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001648 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001649
1650 if (compositor->idle_inhibit)
1651 return 1;
1652
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02001653 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02001654 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001655
1656 return 1;
1657}
1658
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001659WL_EXPORT void
1660weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1661{
1662 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001663 pixman_region32_init(&plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001664 plane->x = x;
1665 plane->y = y;
1666}
1667
1668WL_EXPORT void
1669weston_plane_release(struct weston_plane *plane)
1670{
1671 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001672 pixman_region32_fini(&plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001673}
1674
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001675static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001676weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001677
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001678static void
Daniel Stone37816df2012-05-16 18:45:18 +01001679clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001680{
Daniel Stone37816df2012-05-16 18:45:18 +01001681 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001682 struct weston_output *output, *prev = NULL;
1683 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001684
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001685 x = wl_fixed_to_int(*fx);
1686 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001687 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1688 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001689
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001690 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001691 if (pixman_region32_contains_point(&output->region,
1692 x, y, NULL))
1693 valid = 1;
1694 if (pixman_region32_contains_point(&output->region,
1695 old_x, old_y, NULL))
1696 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001697 }
Daniel Stone37816df2012-05-16 18:45:18 +01001698
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001699 if (!valid) {
1700 if (x < prev->x)
1701 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001702 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001703 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001704 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001705 if (y < prev->y)
1706 *fy = wl_fixed_from_int(prev->y);
1707 else if (y >= prev->y + prev->current->height)
1708 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001709 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001710 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001711}
1712
Kristian Høgsberge2464fd2013-02-25 16:49:29 -05001713static void
Kristian Høgsberg068b61c2013-02-25 17:04:47 -05001714move_pointer(struct weston_seat *seat, wl_fixed_t dx, wl_fixed_t dy)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001715{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001716 struct weston_compositor *ec = seat->compositor;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001717 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge2464fd2013-02-25 16:49:29 -05001718 struct weston_output *output;
Kristian Høgsberg068b61c2013-02-25 17:04:47 -05001719 wl_fixed_t x, y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001720 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001721
Kristian Høgsberg068b61c2013-02-25 17:04:47 -05001722 x = pointer->x + dx;
1723 y = pointer->y + dy;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001724 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001725
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001726 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001727
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001728 pointer->x = x;
1729 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001730
1731 ix = wl_fixed_to_int(x);
1732 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001733
Scott Moreauccbf29d2012-02-22 14:21:41 -07001734 wl_list_for_each(output, &ec->output_list, link)
1735 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001736 pixman_region32_contains_point(&output->region,
1737 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06001738 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001739
Daniel Stone37816df2012-05-16 18:45:18 +01001740 weston_device_repick(seat);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001741
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001742 if (seat->sprite) {
1743 weston_surface_set_position(seat->sprite,
1744 ix - seat->hotspot_x,
1745 iy - seat->hotspot_y);
1746 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001747 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001748}
1749
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001750WL_EXPORT void
Kristian Høgsberge2464fd2013-02-25 16:49:29 -05001751notify_motion(struct weston_seat *seat,
Kristian Høgsberg068b61c2013-02-25 17:04:47 -05001752 uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
Kristian Høgsberge2464fd2013-02-25 16:49:29 -05001753{
1754 const struct wl_pointer_grab_interface *interface;
1755 struct weston_compositor *ec = seat->compositor;
1756 struct wl_pointer *pointer = seat->seat.pointer;
1757
1758 weston_compositor_wake(ec);
1759
Kristian Høgsberg068b61c2013-02-25 17:04:47 -05001760 move_pointer(seat, dx, dy);
Kristian Høgsberge2464fd2013-02-25 16:49:29 -05001761
1762 interface = pointer->grab->interface;
1763 interface->motion(pointer->grab, time,
1764 pointer->grab->x, pointer->grab->y);
1765}
1766
1767WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001768weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001769 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001770{
Daniel Stone37816df2012-05-16 18:45:18 +01001771 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001772
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001773 if (seat->seat.keyboard) {
1774 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1775 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04001776 }
1777
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001778 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001779}
1780
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001781WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001782notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001783 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001784{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001785 struct weston_compositor *compositor = seat->compositor;
1786 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001787 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001788 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001789 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001790
Daniel Stone4dbadb12012-05-30 16:31:51 +01001791 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001792 if (compositor->ping_handler && focus)
1793 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001794 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001795 if (pointer->button_count == 0) {
1796 pointer->grab_button = button;
1797 pointer->grab_time = time;
1798 pointer->grab_x = pointer->x;
1799 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001800 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001801 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001802 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001803 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001804 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001805 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001806
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001807 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001808 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001809
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001810 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001811
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001812 if (pointer->button_count == 1)
1813 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001814 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001815}
1816
Scott Moreau210d0792012-03-22 10:47:01 -06001817WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001818notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01001819 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001820{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001821 struct weston_compositor *compositor = seat->compositor;
1822 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001823 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001824 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001825 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1826
1827 if (compositor->ping_handler && focus)
1828 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001829
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02001830 weston_compositor_wake(compositor);
Scott Moreau210d0792012-03-22 10:47:01 -06001831
Scott Moreau6a3633d2012-03-20 08:47:59 -06001832 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001833 weston_compositor_run_axis_binding(compositor, seat,
1834 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06001835 else
1836 return;
1837
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001838 if (pointer->focus_resource)
1839 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01001840 value);
Scott Moreau210d0792012-03-22 10:47:01 -06001841}
1842
Daniel Stone05d58682012-06-22 13:21:38 +01001843WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001844notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001845{
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001846 struct wl_keyboard *keyboard = &seat->keyboard.keyboard;
Daniel Stone05d58682012-06-22 13:21:38 +01001847 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01001848 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001849 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01001850 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01001851 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01001852
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001853 /* Serialize and update our internal state, checking to see if it's
1854 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01001855 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1856 XKB_STATE_DEPRESSED);
1857 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1858 XKB_STATE_LATCHED);
1859 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1860 XKB_STATE_LOCKED);
1861 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01001862 XKB_STATE_EFFECTIVE);
1863
Daniel Stone71c38772012-06-22 13:21:30 +01001864 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
1865 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
1866 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
1867 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01001868 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01001869
Daniel Stone71c38772012-06-22 13:21:30 +01001870 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
1871 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
1872 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
1873 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001874
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001875 /* And update the modifier_state for bindings. */
1876 mods_lookup = mods_depressed | mods_latched;
1877 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01001878 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001879 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01001880 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001881 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01001882 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001883 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04001884 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
1885 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01001886
Daniel Stone8c8164f2012-05-30 16:31:45 +01001887 /* Finally, notify the compositor that LEDs have changed. */
1888 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001889 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001890 leds |= LED_NUM_LOCK;
1891 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001892 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001893 leds |= LED_CAPS_LOCK;
1894 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001895 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001896 leds |= LED_SCROLL_LOCK;
1897 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01001898 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01001899 seat->xkb_state.leds = leds;
1900
Daniel Stone05d58682012-06-22 13:21:38 +01001901 if (changed) {
1902 grab->interface->modifiers(grab,
1903 serial,
1904 keyboard->modifiers.mods_depressed,
1905 keyboard->modifiers.mods_latched,
1906 keyboard->modifiers.mods_locked,
1907 keyboard->modifiers.group);
1908 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001909}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001910
Daniel Stone05d58682012-06-22 13:21:38 +01001911static void
1912update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001913 enum wl_keyboard_key_state state)
1914{
1915 enum xkb_key_direction direction;
1916
1917 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1918 direction = XKB_KEY_DOWN;
1919 else
1920 direction = XKB_KEY_UP;
1921
1922 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1923 * broken keycode system, which starts at 8. */
1924 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1925
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001926 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001927}
1928
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001929WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001930notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001931 enum wl_keyboard_key_state state,
1932 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001933{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001934 struct weston_compositor *compositor = seat->compositor;
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001935 struct weston_keyboard *keyboard = &seat->keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01001936 struct weston_surface *focus =
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001937 (struct weston_surface *) keyboard->keyboard.focus;
1938 struct wl_keyboard_grab *grab = keyboard->keyboard.grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001939 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001940 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001941
Daniel Stonec9785ea2012-05-30 16:31:52 +01001942 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001943 if (compositor->ping_handler && focus)
1944 compositor->ping_handler(focus, serial);
1945
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001946 weston_compositor_idle_inhibit(compositor);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001947 keyboard->keyboard.grab_key = key;
1948 keyboard->keyboard.grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07001949 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001950 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001951 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001952
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001953 end = keyboard->keyboard.keys.data + keyboard->keyboard.keys.size;
1954 for (k = keyboard->keyboard.keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01001955 if (*k == key) {
1956 /* Ignore server-generated repeats. */
1957 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1958 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001959 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01001960 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001961 }
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001962 keyboard->keyboard.keys.size = (void *) end - keyboard->keyboard.keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001963 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001964 k = wl_array_add(&keyboard->keyboard.keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001965 *k = key;
1966 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001967
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001968 if (grab == &keyboard->keyboard.default_grab ||
1969 grab == &keyboard->input_method_grab) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001970 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001971 state);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001972 grab = keyboard->keyboard.grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06001973 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001974
Daniel Stone7ace3902012-05-30 16:31:40 +01001975 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01001976
1977 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001978 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01001979 wl_display_get_serial(compositor->wl_display),
1980 key,
1981 state);
1982 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001983}
1984
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001985WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001986notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01001987 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001988{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001989 struct weston_compositor *compositor = seat->compositor;
1990 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001991
1992 if (output) {
Kristian Høgsberg00fbbe62012-10-23 13:04:09 -04001993 clip_pointer_motion(seat, &x, &y);
Daniel Stone37816df2012-05-16 18:45:18 +01001994 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001995 x - pointer->x,
1996 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001997
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001998 pointer->x = x;
1999 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002000 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002001 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002002 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002003 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03002004 /* FIXME: We should call wl_pointer_set_focus(seat,
2005 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002006 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002007}
2008
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002009static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002010destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002011{
Daniel Stone37816df2012-05-16 18:45:18 +01002012 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002013
Daniel Stone37816df2012-05-16 18:45:18 +01002014 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002015 saved_kbd_focus_listener);
2016
Daniel Stone37816df2012-05-16 18:45:18 +01002017 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002018}
2019
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002020WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002021notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002022 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002023{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002024 struct weston_compositor *compositor = seat->compositor;
2025 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04002026 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002027 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05002028
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002029 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002030 wl_array_copy(&keyboard->keys, keys);
2031 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002032 weston_compositor_idle_inhibit(compositor);
2033 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002034 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002035 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002036 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002037
Daniel Stoneef172672012-06-22 13:21:32 +01002038 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002039 wl_array_for_each(k, &keyboard->keys) {
2040 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01002041 WL_KEYBOARD_KEY_STATE_PRESSED);
2042 }
2043
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002044 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002045
2046 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002047 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2048 wl_keyboard_set_focus(keyboard, surface);
2049 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002050 }
2051}
2052
2053WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002054notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01002055{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002056 struct weston_compositor *compositor = seat->compositor;
2057 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002058 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002059
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002060 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002061 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002062 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002063 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002064 WL_KEYBOARD_KEY_STATE_RELEASED);
2065 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002066
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002067 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002068
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002069 if (keyboard->focus) {
2070 seat->saved_kbd_focus = keyboard->focus;
2071 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01002072 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002073 wl_signal_add(&keyboard->focus->resource.destroy_signal,
2074 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002075 }
2076
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002077 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002078 /* FIXME: We really need keyboard grab cancel here to
2079 * let the grab shut down properly. As it is we leak
2080 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002081 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002082}
2083
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002084static void
Daniel Stone37816df2012-05-16 18:45:18 +01002085touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002086{
Daniel Stone37816df2012-05-16 18:45:18 +01002087 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002088 struct wl_resource *resource;
2089
Pekka Paalanen56464252012-07-31 13:21:08 +03002090 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002091 return;
2092
Pekka Paalanen56464252012-07-31 13:21:08 +03002093 if (seat->touch->focus_resource)
2094 wl_list_remove(&seat->touch->focus_listener.link);
2095 seat->touch->focus = NULL;
2096 seat->touch->focus_resource = NULL;
2097
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002098 if (surface) {
2099 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01002100 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04002101 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002102 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02002103 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002104 return;
2105 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002106
Daniel Stone37816df2012-05-16 18:45:18 +01002107 seat->touch->focus = surface;
2108 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03002109 wl_signal_add(&resource->destroy_signal,
2110 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002111 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002112}
2113
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002114/**
2115 * notify_touch - emulates button touches and notifies surfaces accordingly.
2116 *
2117 * It assumes always the correct cycle sequence until it gets here: touch_down
2118 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2119 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002120 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002121 */
2122WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002123notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002124 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002125{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002126 struct weston_compositor *ec = seat->compositor;
2127 struct wl_touch *touch = seat->seat.touch;
Matt Roper47c1b982012-10-10 16:56:53 -07002128 struct wl_touch_grab *grab = touch->grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002129 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002130 wl_fixed_t sx, sy;
Matt Roper47c1b982012-10-10 16:56:53 -07002131
2132 /* Update grab's global coordinates. */
2133 touch->grab_x = x;
2134 touch->grab_y = y;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002135
2136 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01002137 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002138 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002139
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002140 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002141
2142 /* the first finger down picks the surface, and all further go
2143 * to that surface for the remainder of the touch session i.e.
2144 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002145 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002146 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002147 touch_set_focus(seat, &es->surface);
2148 } else if (touch->focus) {
2149 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002150 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Rob Bradford26e009c2012-12-05 18:47:05 +00002151 } else {
2152 /* Unexpected condition: We have non-initial touch but
2153 * there is no focused surface.
2154 */
2155 weston_log("touch event received with %d points down"
2156 "but no surface focused\n", seat->num_tp);
2157 return;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002158 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002159
Matt Roper47c1b982012-10-10 16:56:53 -07002160 grab->interface->down(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002161 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002162 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002163 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002164 if (!es)
2165 break;
2166
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002167 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Matt Roper47c1b982012-10-10 16:56:53 -07002168 grab->interface->motion(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002169 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002170 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002171 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002172 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002173
Matt Roper47c1b982012-10-10 16:56:53 -07002174 grab->interface->up(grab, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002175 if (seat->num_tp == 0)
2176 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002177 break;
2178 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002179}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002180
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04002181static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002182pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
2183{
2184 struct weston_seat *seat = container_of(listener, struct weston_seat,
2185 sprite_destroy_listener);
2186
2187 seat->sprite = NULL;
2188}
2189
2190static void
2191pointer_cursor_surface_configure(struct weston_surface *es,
Giulio Camuffo184df502013-02-21 11:29:21 +01002192 int32_t dx, int32_t dy, int32_t width, int32_t height)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002193{
2194 struct weston_seat *seat = es->private;
2195 int x, y;
2196
Giulio Camuffo184df502013-02-21 11:29:21 +01002197 if (width == 0)
2198 return;
2199
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002200 assert(es == seat->sprite);
2201
2202 seat->hotspot_x -= dx;
2203 seat->hotspot_y -= dy;
2204
2205 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
2206 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
2207
2208 weston_surface_configure(seat->sprite, x, y,
Giulio Camuffo184df502013-02-21 11:29:21 +01002209 width, height);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002210
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002211 empty_region(&es->pending.input);
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03002212
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002213 if (!weston_surface_is_mapped(es)) {
2214 wl_list_insert(&es->compositor->cursor_layer.surface_list,
2215 &es->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002216 weston_surface_update_transform(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002217 }
2218}
2219
2220static void
2221pointer_unmap_sprite(struct weston_seat *seat)
2222{
2223 if (weston_surface_is_mapped(seat->sprite))
2224 weston_surface_unmap(seat->sprite);
2225
2226 wl_list_remove(&seat->sprite_destroy_listener.link);
2227 seat->sprite->configure = NULL;
2228 seat->sprite->private = NULL;
2229 seat->sprite = NULL;
2230}
2231
2232static void
2233pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2234 uint32_t serial, struct wl_resource *surface_resource,
2235 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002236{
Daniel Stone37816df2012-05-16 18:45:18 +01002237 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002238 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002239
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002240 if (surface_resource)
Pekka Paalanen6c71ee12012-10-10 12:49:30 +03002241 surface = surface_resource->data;
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002242
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04002243 if (seat->seat.pointer->focus == NULL)
2244 return;
2245 if (seat->seat.pointer->focus->resource.client != client)
2246 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002247 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002248 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002249
2250 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002251 if (surface->configure) {
2252 wl_resource_post_error(&surface->surface.resource,
2253 WL_DISPLAY_ERROR_INVALID_OBJECT,
2254 "surface->configure already "
2255 "set");
2256 return;
2257 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002258 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002259
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002260 if (seat->sprite)
2261 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002262
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002263 if (!surface)
2264 return;
2265
2266 wl_signal_add(&surface->surface.resource.destroy_signal,
2267 &seat->sprite_destroy_listener);
2268
2269 surface->configure = pointer_cursor_surface_configure;
2270 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002271 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002272 seat->hotspot_x = x;
2273 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002274
Pekka Paalanende685b82012-12-04 15:58:12 +02002275 if (surface->buffer_ref.buffer)
Giulio Camuffo184df502013-02-21 11:29:21 +01002276 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
2277 weston_surface_buffer_height(surface));
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002278}
2279
Daniel Stone37816df2012-05-16 18:45:18 +01002280static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002281 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002282};
2283
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002284static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002285handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002286{
Daniel Stone37816df2012-05-16 18:45:18 +01002287 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002288
Daniel Stone37816df2012-05-16 18:45:18 +01002289 seat = container_of(listener, struct weston_seat,
2290 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002291
Daniel Stone37816df2012-05-16 18:45:18 +01002292 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002293}
2294
Casey Dahlin9074db52012-04-19 22:50:09 -04002295static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002296{
2297 wl_list_remove(&resource->link);
2298 free(resource);
2299}
2300
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002301static void
Daniel Stone37816df2012-05-16 18:45:18 +01002302seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2303 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002304{
Daniel Stone37816df2012-05-16 18:45:18 +01002305 struct weston_seat *seat = resource->data;
2306 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002307
Daniel Stone37816df2012-05-16 18:45:18 +01002308 if (!seat->seat.pointer)
2309 return;
2310
2311 cr = wl_client_add_object(client, &wl_pointer_interface,
2312 &pointer_interface, id, seat);
2313 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002314 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002315
2316 if (seat->seat.pointer->focus &&
2317 seat->seat.pointer->focus->resource.client == client) {
2318 struct weston_surface *surface;
2319 wl_fixed_t sx, sy;
2320
2321 surface = (struct weston_surface *) seat->seat.pointer->focus;
2322 weston_surface_from_global_fixed(surface,
2323 seat->seat.pointer->x,
2324 seat->seat.pointer->y,
2325 &sx,
2326 &sy);
2327 wl_pointer_set_focus(seat->seat.pointer,
2328 seat->seat.pointer->focus,
2329 sx,
2330 sy);
2331 }
Daniel Stone37816df2012-05-16 18:45:18 +01002332}
2333
2334static void
2335seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2336 uint32_t id)
2337{
2338 struct weston_seat *seat = resource->data;
2339 struct wl_resource *cr;
2340
2341 if (!seat->seat.keyboard)
2342 return;
2343
2344 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2345 seat);
2346 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002347 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002348
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002349 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2350 seat->xkb_info.keymap_fd,
2351 seat->xkb_info.keymap_size);
2352
Daniel Stone17b13bd2012-05-30 16:31:39 +01002353 if (seat->seat.keyboard->focus &&
2354 seat->seat.keyboard->focus->resource.client == client) {
2355 wl_keyboard_set_focus(seat->seat.keyboard,
2356 seat->seat.keyboard->focus);
Rafal Mielniczukdb790572013-02-17 15:55:55 +01002357 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone17b13bd2012-05-30 16:31:39 +01002358 }
Daniel Stone37816df2012-05-16 18:45:18 +01002359}
2360
2361static void
2362seat_get_touch(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.touch)
2369 return;
2370
2371 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2372 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002373 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002374}
2375
2376static const struct wl_seat_interface seat_interface = {
2377 seat_get_pointer,
2378 seat_get_keyboard,
2379 seat_get_touch,
2380};
2381
2382static void
2383bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2384{
2385 struct wl_seat *seat = data;
2386 struct wl_resource *resource;
2387 enum wl_seat_capability caps = 0;
2388
2389 resource = wl_client_add_object(client, &wl_seat_interface,
2390 &seat_interface, id, data);
2391 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002392 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002393
2394 if (seat->pointer)
2395 caps |= WL_SEAT_CAPABILITY_POINTER;
2396 if (seat->keyboard)
2397 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2398 if (seat->touch)
2399 caps |= WL_SEAT_CAPABILITY_TOUCH;
2400
2401 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002402}
2403
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002404static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002405device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002406{
Daniel Stone37816df2012-05-16 18:45:18 +01002407 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002408
Daniel Stone37816df2012-05-16 18:45:18 +01002409 seat = container_of(listener, struct weston_seat,
2410 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002411
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002412 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002413}
2414
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002415static int
2416weston_compositor_xkb_init(struct weston_compositor *ec,
2417 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002418{
Daniel Stonee379da92012-05-30 16:32:04 +01002419 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002420 ec->xkb_context = xkb_context_new(0);
2421 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002422 weston_log("failed to create XKB context\n");
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002423 return -1;
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002424 }
Daniel Stone994679a2012-05-30 16:31:43 +01002425 }
2426
2427 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002428 ec->xkb_names = *names;
2429 if (!ec->xkb_names.rules)
2430 ec->xkb_names.rules = strdup("evdev");
2431 if (!ec->xkb_names.model)
2432 ec->xkb_names.model = strdup("pc105");
2433 if (!ec->xkb_names.layout)
2434 ec->xkb_names.layout = strdup("us");
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002435
2436 return 0;
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002437}
2438
Daniel Stonee379da92012-05-30 16:32:04 +01002439static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2440{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002441 if (xkb_info->keymap)
2442 xkb_map_unref(xkb_info->keymap);
2443
2444 if (xkb_info->keymap_area)
2445 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2446 if (xkb_info->keymap_fd >= 0)
2447 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002448}
2449
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002450static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2451{
Daniel Stonee379da92012-05-30 16:32:04 +01002452 free((char *) ec->xkb_names.rules);
2453 free((char *) ec->xkb_names.model);
2454 free((char *) ec->xkb_names.layout);
2455 free((char *) ec->xkb_names.variant);
2456 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002457
Daniel Stonee379da92012-05-30 16:32:04 +01002458 xkb_info_destroy(&ec->xkb_info);
2459 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002460}
2461
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002462static int
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002463weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002464{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002465 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002466
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002467 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2468 XKB_MOD_NAME_SHIFT);
2469 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2470 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002471 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2472 XKB_MOD_NAME_CTRL);
2473 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2474 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002475 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2476 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002477 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2478 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002479 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002480
2481 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2482 XKB_LED_NAME_NUM);
2483 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2484 XKB_LED_NAME_CAPS);
2485 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2486 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002487
2488 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2489 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002490 weston_log("failed to get string version of keymap\n");
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002491 return -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002492 }
2493 xkb_info->keymap_size = strlen(keymap_str) + 1;
2494
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002495 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002496 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002497 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002498 (unsigned long) xkb_info->keymap_size);
2499 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002500 }
2501
2502 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2503 PROT_READ | PROT_WRITE,
2504 MAP_SHARED, xkb_info->keymap_fd, 0);
2505 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002506 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002507 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002508 goto err_dev_zero;
2509 }
2510 strcpy(xkb_info->keymap_area, keymap_str);
2511 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002512
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002513 return 0;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002514
2515err_dev_zero:
2516 close(xkb_info->keymap_fd);
2517 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002518err_keymap_str:
2519 free(keymap_str);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002520 return -1;
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002521}
2522
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002523static int
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002524weston_compositor_build_global_keymap(struct weston_compositor *ec)
2525{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002526 if (ec->xkb_info.keymap != NULL)
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002527 return 0;
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002528
Daniel Stonee379da92012-05-30 16:32:04 +01002529 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002530 &ec->xkb_names,
2531 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002532 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002533 weston_log("failed to compile global XKB keymap\n");
2534 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002535 "options %s\n",
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002536 ec->xkb_names.rules, ec->xkb_names.model,
2537 ec->xkb_names.layout, ec->xkb_names.variant,
2538 ec->xkb_names.options);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002539 return -1;
Daniel Stone994679a2012-05-30 16:31:43 +01002540 }
2541
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002542 if (weston_xkb_info_new_keymap(&ec->xkb_info) < 0)
2543 return -1;
2544
2545 return 0;
Daniel Stone994679a2012-05-30 16:31:43 +01002546}
2547
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002548WL_EXPORT int
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002549weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002550{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002551 if (seat->has_keyboard)
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002552 return 0;
Daniel Stone994679a2012-05-30 16:31:43 +01002553
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002554 if (keymap != NULL) {
2555 seat->xkb_info.keymap = xkb_map_ref(keymap);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002556 if (weston_xkb_info_new_keymap(&seat->xkb_info) < 0)
2557 return -1;
2558 } else {
2559 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
2560 return -1;
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002561 seat->xkb_info = seat->compositor->xkb_info;
2562 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2563 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002564
2565 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2566 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002567 weston_log("failed to initialise XKB state\n");
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002568 return -1;
Daniel Stoned65b9092012-05-30 16:32:05 +01002569 }
2570
Daniel Stone71c38772012-06-22 13:21:30 +01002571 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002572
Jan Arne Petersena75a7892013-01-16 21:26:50 +01002573 wl_keyboard_init(&seat->keyboard.keyboard);
2574 wl_seat_set_keyboard(&seat->seat, &seat->keyboard.keyboard);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002575
2576 seat->has_keyboard = 1;
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002577
2578 return 0;
Daniel Stone994679a2012-05-30 16:31:43 +01002579}
2580
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002581WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002582weston_seat_init_pointer(struct weston_seat *seat)
2583{
2584 if (seat->has_pointer)
2585 return;
2586
2587 wl_pointer_init(&seat->pointer);
2588 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2589
2590 seat->has_pointer = 1;
2591}
2592
2593WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002594weston_seat_init_touch(struct weston_seat *seat)
2595{
2596 if (seat->has_touch)
2597 return;
2598
2599 wl_touch_init(&seat->touch);
2600 wl_seat_set_touch(&seat->seat, &seat->touch);
2601
2602 seat->has_touch = 1;
2603}
2604
2605WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002606weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002607{
Daniel Stone37816df2012-05-16 18:45:18 +01002608 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002609 seat->has_pointer = 0;
2610 seat->has_keyboard = 0;
2611 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002612
Daniel Stone37816df2012-05-16 18:45:18 +01002613 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2614 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002615
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002616 seat->sprite = NULL;
2617 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002618
Daniel Stone37816df2012-05-16 18:45:18 +01002619 seat->compositor = ec;
2620 seat->hotspot_x = 16;
2621 seat->hotspot_y = 16;
2622 seat->modifier_state = 0;
2623 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002624
Daniel Stone37816df2012-05-16 18:45:18 +01002625 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002626 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002627
Daniel Stone37816df2012-05-16 18:45:18 +01002628 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002629
Daniel Stone37816df2012-05-16 18:45:18 +01002630 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2631 wl_signal_add(&seat->seat.drag_icon_signal,
2632 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002633
2634 clipboard_create(seat);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002635 wl_signal_emit(&ec->seat_created_signal, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002636}
2637
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002638WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002639weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002640{
Daniel Stone37816df2012-05-16 18:45:18 +01002641 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002642 /* The global object is destroyed at wl_display_destroy() time. */
2643
Daniel Stone37816df2012-05-16 18:45:18 +01002644 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002645 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002646
Daniel Stone74419a22012-05-30 16:32:02 +01002647 if (seat->xkb_state.state != NULL)
2648 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01002649 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002650
Daniel Stone37816df2012-05-16 18:45:18 +01002651 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002652}
2653
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002654static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002655drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002656{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002657 empty_region(&es->pending.input);
2658
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002659 weston_surface_configure(es,
2660 es->geometry.x + sx, es->geometry.y + sy,
Giulio Camuffo184df502013-02-21 11:29:21 +01002661 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002662}
2663
2664static int
Daniel Stone37816df2012-05-16 18:45:18 +01002665device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002666 struct weston_surface *surface)
2667{
Daniel Stone37816df2012-05-16 18:45:18 +01002668 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002669
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002670 if (surface->configure) {
2671 wl_resource_post_error(&surface->surface.resource,
2672 WL_DISPLAY_ERROR_INVALID_OBJECT,
2673 "surface->configure already set");
2674 return 0;
2675 }
2676
Daniel Stone37816df2012-05-16 18:45:18 +01002677 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002678
Daniel Stone37816df2012-05-16 18:45:18 +01002679 weston_surface_set_position(ws->drag_surface,
2680 wl_fixed_to_double(seat->pointer->x),
2681 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002682
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002683 surface->configure = drag_surface_configure;
2684
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002685 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002686 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002687
2688 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002689}
2690
2691static void
Daniel Stone37816df2012-05-16 18:45:18 +01002692device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002693{
Ander Conselvan de Oliveira5fd55802012-10-11 14:06:19 +03002694 if (weston_surface_is_mapped(seat->drag_surface))
2695 weston_surface_unmap(seat->drag_surface);
2696
Daniel Stone37816df2012-05-16 18:45:18 +01002697 seat->drag_surface->configure = NULL;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002698 empty_region(&seat->drag_surface->pending.input);
Daniel Stone37816df2012-05-16 18:45:18 +01002699 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2700 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002701}
2702
2703static void
Daniel Stone37816df2012-05-16 18:45:18 +01002704device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002705{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002706 struct wl_list *list;
2707
Daniel Stone37816df2012-05-16 18:45:18 +01002708 if (weston_surface_is_mapped(seat->drag_surface) ||
Pekka Paalanende685b82012-12-04 15:58:12 +02002709 !seat->drag_surface->buffer_ref.buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002710 return;
2711
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002712 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2713 list = &seat->sprite->layer_link;
2714 else
2715 list = &seat->compositor->cursor_layer.surface_list;
2716
2717 wl_list_insert(list, &seat->drag_surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002718 weston_surface_update_transform(seat->drag_surface);
Daniel Stone37816df2012-05-16 18:45:18 +01002719 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002720}
2721
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002722static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002723weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01002724 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002725{
2726 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002727
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002728 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002729 return;
2730
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002731 if (seat->drag_surface && seat->seat.drag_surface &&
2732 (&seat->drag_surface->surface.resource !=
2733 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002734 /* between calls to this funcion we got a new drag_surface */
2735 surface_changed = 1;
2736
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002737 if (!seat->seat.drag_surface || surface_changed) {
2738 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002739 if (!surface_changed)
2740 return;
2741 }
2742
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002743 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002744 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002745 seat->seat.drag_surface;
2746 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002747 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002748 }
2749
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002750 /* the client may not have attached a buffer to the drag surface
2751 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002752 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002753
2754 if (!dx && !dy)
2755 return;
2756
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002757 weston_surface_set_position(seat->drag_surface,
2758 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
2759 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002760}
2761
2762WL_EXPORT void
2763weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2764{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002765 struct weston_seat *seat;
2766
2767 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002768 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002769}
2770
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002771static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002772bind_output(struct wl_client *client,
2773 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002774{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002775 struct weston_output *output = data;
2776 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002777 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002778
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002779 resource = wl_client_add_object(client,
2780 &wl_output_interface, NULL, id, data);
2781
Casey Dahlin9074db52012-04-19 22:50:09 -04002782 wl_list_insert(&output->resource_list, &resource->link);
2783 resource->destroy = unbind_resource;
2784
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002785 wl_output_send_geometry(resource,
2786 output->x,
2787 output->y,
2788 output->mm_width,
2789 output->mm_height,
2790 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002791 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002792 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002793
2794 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002795 wl_output_send_mode(resource,
2796 mode->flags,
2797 mode->width,
2798 mode->height,
2799 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002800 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002801}
2802
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002803WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002804weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002805{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002806 struct weston_compositor *c = output->compositor;
2807
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002808 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002809 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002810 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002811
2812 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002813}
2814
Scott Moreau1bad5db2012-08-18 01:04:05 -06002815static void
2816weston_output_compute_transform(struct weston_output *output)
2817{
2818 struct weston_matrix transform;
2819 int flip;
2820
2821 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002822 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002823
2824 switch(output->transform) {
2825 case WL_OUTPUT_TRANSFORM_FLIPPED:
2826 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2827 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2828 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002829 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002830 flip = -1;
2831 break;
2832 default:
2833 flip = 1;
2834 break;
2835 }
2836
2837 switch(output->transform) {
2838 case WL_OUTPUT_TRANSFORM_NORMAL:
2839 case WL_OUTPUT_TRANSFORM_FLIPPED:
2840 transform.d[0] = flip;
2841 transform.d[1] = 0;
2842 transform.d[4] = 0;
2843 transform.d[5] = 1;
2844 break;
2845 case WL_OUTPUT_TRANSFORM_90:
2846 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2847 transform.d[0] = 0;
2848 transform.d[1] = -flip;
2849 transform.d[4] = 1;
2850 transform.d[5] = 0;
2851 break;
2852 case WL_OUTPUT_TRANSFORM_180:
2853 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2854 transform.d[0] = -flip;
2855 transform.d[1] = 0;
2856 transform.d[4] = 0;
2857 transform.d[5] = -1;
2858 break;
2859 case WL_OUTPUT_TRANSFORM_270:
2860 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2861 transform.d[0] = 0;
2862 transform.d[1] = flip;
2863 transform.d[4] = -1;
2864 transform.d[5] = 0;
2865 break;
2866 default:
2867 break;
2868 }
2869
2870 weston_matrix_multiply(&output->matrix, &transform);
2871}
2872
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002873WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002874weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002875{
Scott Moreau850ca422012-05-21 15:21:25 -06002876 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002877 struct weston_matrix camera;
2878 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002879
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002880 weston_matrix_init(&output->matrix);
2881 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002882 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2883 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002884
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002885 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002886 2.0 / (output->width + output->border.left + output->border.right),
2887 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2888
2889 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002890
Scott Moreauccbf29d2012-02-22 14:21:41 -07002891 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002892 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002893 weston_matrix_init(&camera);
2894 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002895 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002896 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002897 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002898 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002899 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002900 weston_matrix_multiply(&output->matrix, &modelview);
2901 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002902
Scott Moreauccbf29d2012-02-22 14:21:41 -07002903 output->dirty = 0;
2904}
2905
Scott Moreau1bad5db2012-08-18 01:04:05 -06002906static void
2907weston_output_transform_init(struct weston_output *output, uint32_t transform)
2908{
2909 output->transform = transform;
2910
2911 switch (transform) {
2912 case WL_OUTPUT_TRANSFORM_90:
2913 case WL_OUTPUT_TRANSFORM_270:
2914 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2915 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2916 /* Swap width and height */
2917 output->width = output->current->height;
2918 output->height = output->current->width;
2919 break;
2920 case WL_OUTPUT_TRANSFORM_NORMAL:
2921 case WL_OUTPUT_TRANSFORM_180:
2922 case WL_OUTPUT_TRANSFORM_FLIPPED:
2923 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2924 output->width = output->current->width;
2925 output->height = output->current->height;
2926 break;
2927 default:
2928 break;
2929 }
2930}
2931
Scott Moreauccbf29d2012-02-22 14:21:41 -07002932WL_EXPORT void
2933weston_output_move(struct weston_output *output, int x, int y)
2934{
2935 output->x = x;
2936 output->y = y;
2937
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002938 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002939 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002940 output->width,
2941 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002942}
2943
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002944WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002945weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002946 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002947{
2948 output->compositor = c;
2949 output->x = x;
2950 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002951 output->border.top = 0;
2952 output->border.bottom = 0;
2953 output->border.left = 0;
2954 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002955 output->mm_width = width;
2956 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002957 output->dirty = 1;
2958
Scott Moreau1bad5db2012-08-18 01:04:05 -06002959 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06002960 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002961
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002962 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002963 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002964
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002965 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002966 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002967 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002968
Casey Dahlin58ba1372012-04-19 22:50:08 -04002969 output->id = ffs(~output->compositor->output_id_pool) - 1;
2970 output->compositor->output_id_pool |= 1 << output->id;
2971
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002972 output->global =
2973 wl_display_add_global(c->wl_display, &wl_output_interface,
2974 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002975}
2976
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002977static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002978compositor_bind(struct wl_client *client,
2979 void *data, uint32_t version, uint32_t id)
2980{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002981 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002982
2983 wl_client_add_object(client, &wl_compositor_interface,
2984 &compositor_interface, id, compositor);
2985}
2986
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002987static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002988log_uname(void)
2989{
2990 struct utsname usys;
2991
2992 uname(&usys);
2993
2994 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2995 usys.version, usys.machine);
2996}
2997
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002998WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02002999weston_environment_get_fd(const char *env)
3000{
3001 char *e, *end;
3002 int fd, flags;
3003
3004 e = getenv(env);
3005 if (!e)
3006 return -1;
3007 fd = strtol(e, &end, 0);
3008 if (*end != '\0')
3009 return -1;
3010
3011 flags = fcntl(fd, F_GETFD);
3012 if (flags == -1)
3013 return -1;
3014
3015 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3016 unsetenv(env);
3017
3018 return fd;
3019}
3020
3021WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003022weston_compositor_init(struct weston_compositor *ec,
3023 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003024 int *argc, char *argv[],
Daniel Stonec1be8e52012-06-01 11:14:02 -04003025 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003026{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003027 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003028 struct xkb_rule_names xkb_names;
3029 const struct config_key keyboard_config_keys[] = {
3030 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
3031 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
3032 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
3033 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
3034 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
3035 };
3036 const struct config_section cs[] = {
3037 { "keyboard",
3038 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
3039 };
3040
3041 memset(&xkb_names, 0, sizeof(xkb_names));
3042 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003043
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003044 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003045 wl_signal_init(&ec->destroy_signal);
3046 wl_signal_init(&ec->activate_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003047 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003048 wl_signal_init(&ec->idle_signal);
3049 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003050 wl_signal_init(&ec->show_input_panel_signal);
3051 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003052 wl_signal_init(&ec->seat_created_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01003053 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003054
Casey Dahlin58ba1372012-04-19 22:50:08 -04003055 ec->output_id_pool = 0;
3056
Kristian Høgsberga8873122011-11-23 10:39:34 -05003057 if (!wl_display_add_global(display, &wl_compositor_interface,
3058 ec, compositor_bind))
3059 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003060
Daniel Stone725c2c32012-06-22 14:04:36 +01003061 wl_list_init(&ec->surface_list);
3062 wl_list_init(&ec->layer_list);
3063 wl_list_init(&ec->seat_list);
3064 wl_list_init(&ec->output_list);
3065 wl_list_init(&ec->key_binding_list);
3066 wl_list_init(&ec->button_binding_list);
3067 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003068 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003069
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003070 weston_plane_init(&ec->primary_plane, 0, 0);
3071
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003072 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3073 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003074
3075 ec->ping_handler = NULL;
3076
3077 screenshooter_create(ec);
3078 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003079 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003080
3081 wl_data_device_manager_init(ec->wl_display);
3082
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003083 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003084
Daniel Stone725c2c32012-06-22 14:04:36 +01003085 loop = wl_display_get_event_loop(ec->wl_display);
3086 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3087 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3088
3089 ec->input_loop = wl_event_loop_create();
3090
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003091 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3092 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3093
3094 weston_compositor_schedule_repaint(ec);
3095
Daniel Stone725c2c32012-06-22 14:04:36 +01003096 return 0;
3097}
3098
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003099WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003100weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003101{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003102 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003103
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003104 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003105 if (ec->input_loop_source)
3106 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003107
Matt Roper361d2ad2011-08-29 13:52:23 -07003108 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003109 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003110 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003111
Daniel Stone325fc2d2012-05-30 16:31:58 +01003112 weston_binding_list_destroy_all(&ec->key_binding_list);
3113 weston_binding_list_destroy_all(&ec->button_binding_list);
3114 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003115 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003116
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003117 weston_plane_release(&ec->primary_plane);
3118
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003119 wl_array_release(&ec->vertices);
3120 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05003121 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003122
3123 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07003124}
3125
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003126WL_EXPORT void
3127weston_version(int *major, int *minor, int *micro)
3128{
3129 *major = WESTON_VERSION_MAJOR;
3130 *minor = WESTON_VERSION_MINOR;
3131 *micro = WESTON_VERSION_MICRO;
3132}
3133
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003134static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003135{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003136 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003137
Martin Minarik6d118362012-06-07 18:01:59 +02003138 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003139 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003140
3141 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003142}
3143
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003144#ifdef HAVE_LIBUNWIND
3145
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003146static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003147print_backtrace(void)
3148{
3149 unw_cursor_t cursor;
3150 unw_context_t context;
3151 unw_word_t off;
3152 unw_proc_info_t pip;
3153 int ret, i = 0;
3154 char procname[256];
3155 const char *filename;
3156 Dl_info dlinfo;
3157
3158 pip.unwind_info = NULL;
3159 ret = unw_getcontext(&context);
3160 if (ret) {
3161 weston_log("unw_getcontext: %d\n", ret);
3162 return;
3163 }
3164
3165 ret = unw_init_local(&cursor, &context);
3166 if (ret) {
3167 weston_log("unw_init_local: %d\n", ret);
3168 return;
3169 }
3170
3171 ret = unw_step(&cursor);
3172 while (ret > 0) {
3173 ret = unw_get_proc_info(&cursor, &pip);
3174 if (ret) {
3175 weston_log("unw_get_proc_info: %d\n", ret);
3176 break;
3177 }
3178
3179 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3180 if (ret && ret != -UNW_ENOMEM) {
3181 if (ret != -UNW_EUNSPEC)
3182 weston_log("unw_get_proc_name: %d\n", ret);
3183 procname[0] = '?';
3184 procname[1] = 0;
3185 }
3186
3187 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3188 *dlinfo.dli_fname)
3189 filename = dlinfo.dli_fname;
3190 else
3191 filename = "?";
3192
3193 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3194 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3195
3196 ret = unw_step(&cursor);
3197 if (ret < 0)
3198 weston_log("unw_step: %d\n", ret);
3199 }
3200}
3201
3202#else
3203
3204static void
3205print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003206{
3207 void *buffer[32];
3208 int i, count;
3209 Dl_info info;
3210
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003211 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3212 for (i = 0; i < count; i++) {
3213 dladdr(buffer[i], &info);
3214 weston_log(" [%016lx] %s (%s)\n",
3215 (long) buffer[i],
3216 info.dli_sname ? info.dli_sname : "--",
3217 info.dli_fname);
3218 }
3219}
3220
3221#endif
3222
3223static void
3224on_segv_signal(int s, siginfo_t *siginfo, void *context)
3225{
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003226 /* This SIGSEGV handler will do a best-effort backtrace, and
3227 * then call the backend restore function, which will switch
3228 * back to the vt we launched from or ungrab X etc and then
3229 * raise SIGTRAP. If we run weston under gdb from X or a
3230 * different vt, and tell gdb "handle SIGSEGV nostop", this
3231 * will allow weston to switch back to gdb on crash and then
3232 * gdb will catch the crash with SIGTRAP. */
3233
Martin Minarik6d118362012-06-07 18:01:59 +02003234 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003235
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003236 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003237
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003238 segv_compositor->restore(segv_compositor);
3239
3240 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003241}
3242
3243
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003244static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003245load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003246{
3247 char path[PATH_MAX];
3248 void *module, *init;
3249
3250 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003251 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003252 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003253 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003254
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003255 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3256 if (module) {
3257 weston_log("Module '%s' already loaded\n", path);
3258 dlclose(module);
3259 return NULL;
3260 }
3261
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003262 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003263 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003264 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003265 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003266 return NULL;
3267 }
3268
3269 init = dlsym(module, entrypoint);
3270 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003271 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003272 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003273 return NULL;
3274 }
3275
3276 return init;
3277}
3278
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003279static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003280load_modules(struct weston_compositor *ec, const char *modules,
3281 int *argc, char *argv[], const char *config_file)
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003282{
3283 const char *p, *end;
3284 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003285 int (*module_init)(struct weston_compositor *ec,
3286 int *argc, char *argv[], const char *config_file);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003287
3288 if (modules == NULL)
3289 return 0;
3290
3291 p = modules;
3292 while (*p) {
3293 end = strchrnul(p, ',');
3294 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
3295 module_init = load_module(buffer, "module_init");
3296 if (module_init)
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003297 module_init(ec, argc, argv, config_file);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003298 p = end;
3299 while (*p == ',')
3300 p++;
3301
3302 }
3303
3304 return 0;
3305}
3306
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003307static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003308 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3309
3310static const char xdg_wrong_message[] =
3311 "fatal: environment variable XDG_RUNTIME_DIR\n"
3312 "is set to \"%s\", which is not a directory.\n";
3313
3314static const char xdg_wrong_mode_message[] =
3315 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3316 "correctly. Unix access mode must be 0700 but is %o,\n"
3317 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003318 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003319
3320static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003321 "Refer to your distribution on how to get it, or\n"
3322 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3323 "on how to implement it.\n";
3324
Martin Minarik37032f82012-06-18 20:15:18 +02003325static void
3326verify_xdg_runtime_dir(void)
3327{
3328 char *dir = getenv("XDG_RUNTIME_DIR");
3329 struct stat s;
3330
3331 if (!dir) {
3332 weston_log(xdg_error_message);
3333 weston_log_continue(xdg_detail_message);
3334 exit(EXIT_FAILURE);
3335 }
3336
3337 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3338 weston_log(xdg_wrong_message, dir);
3339 weston_log_continue(xdg_detail_message);
3340 exit(EXIT_FAILURE);
3341 }
3342
3343 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3344 weston_log(xdg_wrong_mode_message,
3345 dir, s.st_mode & 0777, s.st_uid);
3346 weston_log_continue(xdg_detail_message);
3347 }
3348}
3349
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003350static int
3351usage(int error_code)
3352{
3353 fprintf(stderr,
3354 "Usage: weston [OPTIONS]\n\n"
3355 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3356 "Weston supports multiple backends, and depending on which backend is in use\n"
3357 "different options will be accepted.\n\n"
3358
3359
3360 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003361 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003362 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3363 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3364 " -S, --socket=NAME\tName of socket to listen on\n"
3365 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003366 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003367 " --log==FILE\t\tLog to the given file\n"
3368 " -h, --help\t\tThis help message\n\n");
3369
3370 fprintf(stderr,
3371 "Options for drm-backend.so:\n\n"
3372 " --connector=ID\tBring up only this connector\n"
3373 " --seat=SEAT\t\tThe seat that weston should run on\n"
3374 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003375 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003376 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3377
3378 fprintf(stderr,
3379 "Options for x11-backend.so:\n\n"
3380 " --width=WIDTH\t\tWidth of X window\n"
3381 " --height=HEIGHT\tHeight of X window\n"
3382 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003383 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003384 " --output-count=COUNT\tCreate multiple outputs\n"
3385 " --no-input\t\tDont create input devices\n\n");
3386
3387 fprintf(stderr,
3388 "Options for wayland-backend.so:\n\n"
3389 " --width=WIDTH\t\tWidth of Wayland surface\n"
3390 " --height=HEIGHT\tHeight of Wayland surface\n"
3391 " --display=DISPLAY\tWayland display to connect to\n\n");
3392
3393 exit(error_code);
3394}
3395
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003396int main(int argc, char *argv[])
3397{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003398 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003399 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003400 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003401 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003402 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003403 struct sigaction segv_action;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003404 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003405 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003406 int *argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003407 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003408 char *backend = NULL;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003409 const char *modules = "desktop-shell.so", *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003410 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003411 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003412 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003413 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003414 int32_t version = 0;
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003415 char *config_file;
3416
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003417 const struct config_key core_config_keys[] = {
3418 { "modules", CONFIG_KEY_STRING, &modules },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003419 };
3420
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003421 const struct config_section cs[] = {
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003422 { "core",
3423 core_config_keys, ARRAY_LENGTH(core_config_keys) },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003424 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003425
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003426 const struct weston_option core_options[] = {
3427 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3428 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3429 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003430 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003431 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003432 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003433 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003434 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003435
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003436 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003437
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003438 if (help)
3439 usage(EXIT_SUCCESS);
3440
Scott Moreau12245142012-08-29 15:15:58 -06003441 if (version) {
3442 printf(PACKAGE_STRING "\n");
3443 return EXIT_SUCCESS;
3444 }
3445
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003446 weston_log_file_open(log);
3447
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003448 weston_log("%s\n"
3449 STAMP_SPACE "%s\n"
3450 STAMP_SPACE "Bug reports to: %s\n"
3451 STAMP_SPACE "Build: %s\n",
3452 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003453 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003454 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003455
Martin Minarik37032f82012-06-18 20:15:18 +02003456 verify_xdg_runtime_dir();
3457
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003458 display = wl_display_create();
3459
Tiago Vignatti2116b892011-08-08 05:52:59 -07003460 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003461 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3462 display);
3463 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3464 display);
3465 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3466 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003467
3468 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003469 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3470 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003471
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003472 if (!backend) {
3473 if (getenv("WAYLAND_DISPLAY"))
3474 backend = "wayland-backend.so";
3475 else if (getenv("DISPLAY"))
3476 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003477 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003478 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003479 }
3480
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003481 config_file = config_file_path("weston.ini");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003482 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003483
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003484 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003485 if (!backend_init)
3486 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003487
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003488 ec = backend_init(display, &argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003489 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003490 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003491 exit(EXIT_FAILURE);
3492 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003493
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003494 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3495 segv_action.sa_sigaction = on_segv_signal;
3496 sigemptyset(&segv_action.sa_mask);
3497 sigaction(SIGSEGV, &segv_action, NULL);
3498 segv_compositor = ec;
3499
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003500 ec->option_idle_time = idle_time;
3501 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003502
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003503 setenv("WAYLAND_DISPLAY", socket_name, 1);
3504
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003505 if (load_modules(ec, modules, &argc, argv, config_file) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003506 goto out;
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003507 if (load_modules(ec, option_modules, &argc, argv, config_file) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003508 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003509
Kristian Høgsberg142d5462013-02-21 21:10:36 -05003510 free(config_file);
3511
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003512 for (i = 1; i < argc; i++)
3513 weston_log("fatal: unhandled option: %s\n", argv[i]);
3514 if (argc > 1) {
3515 ret = EXIT_FAILURE;
3516 goto out;
3517 }
3518
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003519 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003520 weston_log("fatal: failed to add socket: %m\n");
3521 ret = EXIT_FAILURE;
3522 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003523 }
3524
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003525 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003526
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003527 wl_display_run(display);
3528
3529 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003530 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05003531 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003532
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003533 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003534
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003535 for (i = ARRAY_LENGTH(signals); i;)
3536 wl_event_source_remove(signals[--i]);
3537
Daniel Stone855028f2012-05-01 20:37:10 +01003538 weston_compositor_xkb_destroy(ec);
3539
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003540 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003541 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003542
Martin Minarik19e6f262012-06-07 13:08:46 +02003543 weston_log_file_close();
3544
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003545 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003546}