blob: 6aeaaee7ca4aae1af3b61437f12d27b414483e89 [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 Paalanen483243f2013-03-08 14:56:50 +0200306 wl_list_init(&surface->geometry.child_list);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200307 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200308 surface->transform.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500309
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300310 surface->pending.buffer_destroy_listener.notify =
311 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300312 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300313 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300314 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300315 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300316
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400317 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500318}
319
Alex Wu8811bf92012-02-28 18:07:54 +0800320WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500321weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200322 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500323{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100324 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500325}
326
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400327WL_EXPORT void
328weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200329 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200330{
331 if (surface->transform.enabled) {
332 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
333
334 weston_matrix_transform(&surface->transform.matrix, &v);
335
336 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200337 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700338 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200339 v.f[3]);
340 *x = 0;
341 *y = 0;
342 return;
343 }
344
345 *x = v.f[0] / v.f[3];
346 *y = v.f[1] / v.f[3];
347 } else {
348 *x = sx + surface->geometry.x;
349 *y = sy + surface->geometry.y;
350 }
351}
352
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500353WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200354weston_transformed_coord(int width, int height,
355 enum wl_output_transform transform,
356 float sx, float sy, float *bx, float *by)
357{
358 switch (transform) {
359 case WL_OUTPUT_TRANSFORM_NORMAL:
360 default:
361 *bx = sx;
362 *by = sy;
363 break;
364 case WL_OUTPUT_TRANSFORM_FLIPPED:
365 *bx = width - sx;
366 *by = sy;
367 break;
368 case WL_OUTPUT_TRANSFORM_90:
369 *bx = height - sy;
370 *by = sx;
371 break;
372 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
373 *bx = height - sy;
374 *by = width - sx;
375 break;
376 case WL_OUTPUT_TRANSFORM_180:
377 *bx = width - sx;
378 *by = height - sy;
379 break;
380 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
381 *bx = sx;
382 *by = height - sy;
383 break;
384 case WL_OUTPUT_TRANSFORM_270:
385 *bx = sy;
386 *by = width - sx;
387 break;
388 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
389 *bx = sy;
390 *by = sx;
391 break;
392 }
393}
394
395WL_EXPORT pixman_box32_t
396weston_transformed_rect(int width, int height,
397 enum wl_output_transform transform,
398 pixman_box32_t rect)
399{
400 float x1, x2, y1, y2;
401
402 pixman_box32_t ret;
403
404 weston_transformed_coord(width, height, transform,
405 rect.x1, rect.y1, &x1, &y1);
406 weston_transformed_coord(width, height, transform,
407 rect.x2, rect.y2, &x2, &y2);
408
409 if (x1 <= x2) {
410 ret.x1 = x1;
411 ret.x2 = x2;
412 } else {
413 ret.x1 = x2;
414 ret.x2 = x1;
415 }
416
417 if (y1 <= y2) {
418 ret.y1 = y1;
419 ret.y2 = y2;
420 } else {
421 ret.y1 = y2;
422 ret.y2 = y1;
423 }
424
425 return ret;
426}
427
428WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200429weston_surface_to_buffer_float(struct weston_surface *surface,
430 float sx, float sy, float *bx, float *by)
431{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200432 weston_transformed_coord(surface->geometry.width,
433 surface->geometry.height,
434 surface->buffer_transform,
435 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200436}
437
438WL_EXPORT pixman_box32_t
439weston_surface_to_buffer_rect(struct weston_surface *surface,
440 pixman_box32_t rect)
441{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200442 return weston_transformed_rect(surface->geometry.width,
443 surface->geometry.height,
444 surface->buffer_transform, rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200445}
446
447WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400448weston_surface_move_to_plane(struct weston_surface *surface,
449 struct weston_plane *plane)
450{
451 if (surface->plane == plane)
452 return;
453
454 weston_surface_damage_below(surface);
455 surface->plane = plane;
456 weston_surface_damage(surface);
457}
458
459WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500460weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200461{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500462 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200463
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500464 pixman_region32_init(&damage);
465 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
466 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400467 pixman_region32_union(&surface->plane->damage,
468 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500469 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200470}
471
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400472static struct wl_resource *
473find_resource_for_client(struct wl_list *list, struct wl_client *client)
474{
475 struct wl_resource *r;
476
477 wl_list_for_each(r, list, link) {
478 if (r->client == client)
479 return r;
480 }
481
482 return NULL;
483}
484
485static void
486weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
487{
488 uint32_t different = es->output_mask ^ mask;
489 uint32_t entered = mask & different;
490 uint32_t left = es->output_mask & different;
491 struct weston_output *output;
492 struct wl_resource *resource = NULL;
493 struct wl_client *client = es->surface.resource.client;
494
495 es->output_mask = mask;
496 if (es->surface.resource.client == NULL)
497 return;
498 if (different == 0)
499 return;
500
501 wl_list_for_each(output, &es->compositor->output_list, link) {
502 if (1 << output->id & different)
503 resource =
504 find_resource_for_client(&output->resource_list,
505 client);
506 if (resource == NULL)
507 continue;
508 if (1 << output->id & entered)
509 wl_surface_send_enter(&es->surface.resource, resource);
510 if (1 << output->id & left)
511 wl_surface_send_leave(&es->surface.resource, resource);
512 }
513}
514
515static void
516weston_surface_assign_output(struct weston_surface *es)
517{
518 struct weston_compositor *ec = es->compositor;
519 struct weston_output *output, *new_output;
520 pixman_region32_t region;
521 uint32_t max, area, mask;
522 pixman_box32_t *e;
523
524 new_output = NULL;
525 max = 0;
526 mask = 0;
527 pixman_region32_init(&region);
528 wl_list_for_each(output, &ec->output_list, link) {
529 pixman_region32_intersect(&region, &es->transform.boundingbox,
530 &output->region);
531
532 e = pixman_region32_extents(&region);
533 area = (e->x2 - e->x1) * (e->y2 - e->y1);
534
535 if (area > 0)
536 mask |= 1 << output->id;
537
538 if (area >= max) {
539 new_output = output;
540 max = area;
541 }
542 }
543 pixman_region32_fini(&region);
544
545 es->output = new_output;
546 weston_surface_update_output_mask(es, mask);
547}
548
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200549static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200550surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
551 int32_t width, int32_t height,
552 pixman_region32_t *bbox)
553{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200554 float min_x = HUGE_VALF, min_y = HUGE_VALF;
555 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200556 int32_t s[4][2] = {
557 { sx, sy },
558 { sx, sy + height },
559 { sx + width, sy },
560 { sx + width, sy + height }
561 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200562 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200563 int i;
564
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300565 if (width == 0 || height == 0) {
566 /* avoid rounding empty bbox to 1x1 */
567 pixman_region32_init(bbox);
568 return;
569 }
570
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200571 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200572 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400573 weston_surface_to_global_float(surface,
574 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200575 if (x < min_x)
576 min_x = x;
577 if (x > max_x)
578 max_x = x;
579 if (y < min_y)
580 min_y = y;
581 if (y > max_y)
582 max_y = y;
583 }
584
Pekka Paalanen219b9822012-02-08 15:38:37 +0200585 int_x = floorf(min_x);
586 int_y = floorf(min_y);
587 pixman_region32_init_rect(bbox, int_x, int_y,
588 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200589}
590
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200591static void
592weston_surface_update_transform_disable(struct weston_surface *surface)
593{
594 surface->transform.enabled = 0;
595
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200596 /* round off fractions when not transformed */
597 surface->geometry.x = roundf(surface->geometry.x);
598 surface->geometry.y = roundf(surface->geometry.y);
599
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500600 /* Otherwise identity matrix, but with x and y translation. */
601 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
602 surface->transform.position.matrix.d[12] = surface->geometry.x;
603 surface->transform.position.matrix.d[13] = surface->geometry.y;
604
605 surface->transform.matrix = surface->transform.position.matrix;
606
Kristian Høgsberg9bcaaeb2013-02-28 14:56:43 -0500607 surface->transform.inverse = surface->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500608 surface->transform.inverse.d[12] = -surface->geometry.x;
609 surface->transform.inverse.d[13] = -surface->geometry.y;
610
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200611 pixman_region32_init_rect(&surface->transform.boundingbox,
612 surface->geometry.x,
613 surface->geometry.y,
614 surface->geometry.width,
615 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500616
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400617 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500618 pixman_region32_copy(&surface->transform.opaque,
619 &surface->opaque);
620 pixman_region32_translate(&surface->transform.opaque,
621 surface->geometry.x,
622 surface->geometry.y);
623 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200624}
625
626static int
627weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200628{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200629 struct weston_surface *parent = surface->geometry.parent;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200630 struct weston_matrix *matrix = &surface->transform.matrix;
631 struct weston_matrix *inverse = &surface->transform.inverse;
632 struct weston_transform *tform;
633
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200634 surface->transform.enabled = 1;
635
636 /* Otherwise identity matrix, but with x and y translation. */
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300637 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200638 surface->transform.position.matrix.d[12] = surface->geometry.x;
639 surface->transform.position.matrix.d[13] = surface->geometry.y;
640
641 weston_matrix_init(matrix);
642 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
643 weston_matrix_multiply(matrix, &tform->matrix);
644
Pekka Paalanen483243f2013-03-08 14:56:50 +0200645 if (parent)
646 weston_matrix_multiply(matrix, &parent->transform.matrix);
647
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200648 if (weston_matrix_invert(inverse, matrix) < 0) {
649 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200650 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200651 " transformation not invertible.\n", surface);
652 return -1;
653 }
654
655 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
656 surface->geometry.height,
657 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500658
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200659 return 0;
660}
661
662WL_EXPORT void
663weston_surface_update_transform(struct weston_surface *surface)
664{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200665 struct weston_surface *parent = surface->geometry.parent;
666
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200667 if (!surface->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200668 return;
669
Pekka Paalanen483243f2013-03-08 14:56:50 +0200670 if (parent)
671 weston_surface_update_transform(parent);
672
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200673 surface->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200674
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500675 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200676
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200677 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500678 pixman_region32_fini(&surface->transform.opaque);
679 pixman_region32_init(&surface->transform.opaque);
680
Pekka Paalanencd403622012-01-25 13:37:39 +0200681 /* transform.position is always in transformation_list */
682 if (surface->geometry.transformation_list.next ==
683 &surface->transform.position.link &&
684 surface->geometry.transformation_list.prev ==
Pekka Paalanen483243f2013-03-08 14:56:50 +0200685 &surface->transform.position.link &&
686 !parent) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200687 weston_surface_update_transform_disable(surface);
688 } else {
689 if (weston_surface_update_transform_enable(surface) < 0)
690 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200691 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200692
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400693 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200694
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300695 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200696}
697
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200698WL_EXPORT void
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200699weston_surface_geometry_dirty(struct weston_surface *surface)
700{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200701 struct weston_surface *child;
702
703 /*
704 * The invariant: if surface->geometry.dirty, then all surfaces
705 * in surface->geometry.child_list have geometry.dirty too.
706 * Corollary: if not parent->geometry.dirty, then all ancestors
707 * are not dirty.
708 */
709
710 if (surface->transform.dirty)
711 return;
712
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200713 surface->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200714
715 wl_list_for_each(child, &surface->geometry.child_list,
716 geometry.parent_link)
717 weston_surface_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200718}
719
720WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100721weston_surface_to_global_fixed(struct weston_surface *surface,
722 wl_fixed_t sx, wl_fixed_t sy,
723 wl_fixed_t *x, wl_fixed_t *y)
724{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200725 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100726
727 weston_surface_to_global_float(surface,
728 wl_fixed_to_double(sx),
729 wl_fixed_to_double(sy),
730 &xf, &yf);
731 *x = wl_fixed_from_double(xf);
732 *y = wl_fixed_from_double(yf);
733}
734
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400735WL_EXPORT void
736weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200737 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200738{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200739 if (surface->transform.enabled) {
740 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
741
742 weston_matrix_transform(&surface->transform.inverse, &v);
743
744 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200745 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200746 "weston_surface_from_global(), divisor = %g\n",
747 v.f[3]);
748 *sx = 0;
749 *sy = 0;
750 return;
751 }
752
Pekka Paalanencd403622012-01-25 13:37:39 +0200753 *sx = v.f[0] / v.f[3];
754 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200755 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200756 *sx = x - surface->geometry.x;
757 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200758 }
759}
760
761WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100762weston_surface_from_global_fixed(struct weston_surface *surface,
763 wl_fixed_t x, wl_fixed_t y,
764 wl_fixed_t *sx, wl_fixed_t *sy)
765{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200766 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100767
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400768 weston_surface_from_global_float(surface,
769 wl_fixed_to_double(x),
770 wl_fixed_to_double(y),
771 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100772 *sx = wl_fixed_from_double(sxf);
773 *sy = wl_fixed_from_double(syf);
774}
775
776WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200777weston_surface_from_global(struct weston_surface *surface,
778 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
779{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200780 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200781
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400782 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200783 *sx = floorf(sxf);
784 *sy = floorf(syf);
785}
786
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400787WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400788weston_surface_schedule_repaint(struct weston_surface *surface)
789{
790 struct weston_output *output;
791
792 wl_list_for_each(output, &surface->compositor->output_list, link)
793 if (surface->output_mask & (1 << output->id))
794 weston_output_schedule_repaint(output);
795}
796
797WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500798weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500799{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400800 pixman_region32_union_rect(&surface->damage, &surface->damage,
801 0, 0, surface->geometry.width,
802 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200803
Kristian Høgsberg98238702012-08-03 16:29:12 -0400804 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500805}
806
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400807WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500808weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200809 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400810{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200811 surface->geometry.x = x;
812 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200813 surface->geometry.width = width;
814 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200815 weston_surface_geometry_dirty(surface);
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400816}
817
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200818WL_EXPORT void
819weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200820 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200821{
822 surface->geometry.x = x;
823 surface->geometry.y = y;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200824 weston_surface_geometry_dirty(surface);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200825}
826
Pekka Paalanen483243f2013-03-08 14:56:50 +0200827static void
828transform_parent_handle_parent_destroy(struct wl_listener *listener,
829 void *data)
830{
831 struct weston_surface *surface =
832 container_of(listener, struct weston_surface,
833 geometry.parent_destroy_listener);
834
835 weston_surface_set_transform_parent(surface, NULL);
836}
837
838WL_EXPORT void
839weston_surface_set_transform_parent(struct weston_surface *surface,
840 struct weston_surface *parent)
841{
842 if (surface->geometry.parent) {
843 wl_list_remove(&surface->geometry.parent_destroy_listener.link);
844 wl_list_remove(&surface->geometry.parent_link);
845 }
846
847 surface->geometry.parent = parent;
848
849 surface->geometry.parent_destroy_listener.notify =
850 transform_parent_handle_parent_destroy;
851 if (parent) {
852 wl_signal_add(&parent->surface.resource.destroy_signal,
853 &surface->geometry.parent_destroy_listener);
854 wl_list_insert(&parent->geometry.child_list,
855 &surface->geometry.parent_link);
856 }
857
858 weston_surface_geometry_dirty(surface);
859}
860
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300861WL_EXPORT int
862weston_surface_is_mapped(struct weston_surface *surface)
863{
864 if (surface->output)
865 return 1;
866 else
867 return 0;
868}
869
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200870WL_EXPORT int32_t
871weston_surface_buffer_width(struct weston_surface *surface)
872{
873 switch (surface->buffer_transform) {
874 case WL_OUTPUT_TRANSFORM_90:
875 case WL_OUTPUT_TRANSFORM_270:
876 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
877 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200878 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200879 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200880 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200881 }
882}
883
884WL_EXPORT int32_t
885weston_surface_buffer_height(struct weston_surface *surface)
886{
887 switch (surface->buffer_transform) {
888 case WL_OUTPUT_TRANSFORM_90:
889 case WL_OUTPUT_TRANSFORM_270:
890 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
891 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200892 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200893 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200894 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200895 }
896}
897
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400898WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500899weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500900{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400901 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500902
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400903 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500904
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400905 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500906}
907
Tiago Vignatti9d393522012-02-10 16:26:19 +0200908static struct weston_surface *
909weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100910 wl_fixed_t x, wl_fixed_t y,
911 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200912{
913 struct weston_surface *surface;
914
915 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100916 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500917 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100918 wl_fixed_to_int(*sx),
919 wl_fixed_to_int(*sy),
920 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200921 return surface;
922 }
923
924 return NULL;
925}
926
927static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400928weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500929{
Scott Moreau447013d2012-02-18 05:05:29 -0700930 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500931 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400932 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500933
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400934 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100935 return;
936
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400937 surface = weston_compositor_pick_surface(seat->compositor,
938 pointer->x,
939 pointer->y,
940 &pointer->current_x,
941 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500942
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400943 if (&surface->surface != pointer->current) {
944 interface = pointer->grab->interface;
945 pointer->current = &surface->surface;
946 interface->focus(pointer->grab, &surface->surface,
947 pointer->current_x,
948 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500949 }
950
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400951 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500952 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100953 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400954 pointer->x,
955 pointer->y,
956 &pointer->grab->x,
957 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500958}
959
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400960static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500961weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400962{
Daniel Stone37816df2012-05-16 18:45:18 +0100963 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400964
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500965 if (!compositor->focus)
966 return;
967
Daniel Stone37816df2012-05-16 18:45:18 +0100968 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400969 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400970}
971
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400972WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500973weston_surface_unmap(struct weston_surface *surface)
974{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100975 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500976
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500977 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500978 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500979 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500980
Daniel Stone4dab5db2012-05-30 16:31:53 +0100981 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100982 if (seat->seat.keyboard &&
983 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100984 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100985 if (seat->seat.pointer &&
986 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100987 wl_pointer_set_focus(seat->seat.pointer,
988 NULL,
989 wl_fixed_from_int(0),
990 wl_fixed_from_int(0));
991 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500992
Kristian Høgsberg98238702012-08-03 16:29:12 -0400993 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500994}
995
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400996struct weston_frame_callback {
997 struct wl_resource resource;
998 struct wl_list link;
999};
1000
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001001static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001002destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001003{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001004 struct weston_surface *surface =
1005 container_of(resource,
1006 struct weston_surface, surface.resource);
1007 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001008 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001009
Pekka Paalanen483243f2013-03-08 14:56:50 +02001010 assert(wl_list_empty(&surface->geometry.child_list));
1011
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001012 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001013 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001014
Pekka Paalanenbc106382012-10-10 12:49:31 +03001015 wl_list_for_each_safe(cb, next,
1016 &surface->pending.frame_callback_list, link)
1017 wl_resource_destroy(&cb->resource);
1018
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001019 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001020 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001021 pixman_region32_fini(&surface->pending.damage);
1022
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001023 if (surface->pending.buffer)
1024 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1025
Pekka Paalanende685b82012-12-04 15:58:12 +02001026 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001027
Kristian Høgsberg42263852012-09-06 21:59:29 -04001028 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001029
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001030 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001031 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001032 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001033 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001034 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001035
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001036 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
1037 wl_resource_destroy(&cb->resource);
1038
Pekka Paalanen483243f2013-03-08 14:56:50 +02001039 weston_surface_set_transform_parent(surface, NULL);
1040
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001041 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001042}
1043
Alex Wu8811bf92012-02-28 18:07:54 +08001044WL_EXPORT void
1045weston_surface_destroy(struct weston_surface *surface)
1046{
1047 /* Not a valid way to destroy a client surface */
1048 assert(surface->surface.resource.client == NULL);
1049
Kristian Høgsberg9a050af2012-06-07 18:17:42 -04001050 wl_signal_emit(&surface->surface.resource.destroy_signal,
1051 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001052 destroy_surface(&surface->surface.resource);
1053}
1054
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001055static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001056weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1057 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001058{
Pekka Paalanende685b82012-12-04 15:58:12 +02001059 struct weston_buffer_reference *ref =
1060 container_of(listener, struct weston_buffer_reference,
1061 destroy_listener);
1062
1063 assert((struct wl_buffer *)data == ref->buffer);
1064 ref->buffer = NULL;
1065}
1066
1067WL_EXPORT void
1068weston_buffer_reference(struct weston_buffer_reference *ref,
1069 struct wl_buffer *buffer)
1070{
1071 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001072 ref->buffer->busy_count--;
1073 if (ref->buffer->busy_count == 0) {
1074 assert(ref->buffer->resource.client != NULL);
1075 wl_resource_queue_event(&ref->buffer->resource,
1076 WL_BUFFER_RELEASE);
1077 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001078 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001079 }
1080
Pekka Paalanende685b82012-12-04 15:58:12 +02001081 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001082 buffer->busy_count++;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001083 wl_signal_add(&buffer->resource.destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001084 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001085 }
1086
Pekka Paalanende685b82012-12-04 15:58:12 +02001087 ref->buffer = buffer;
1088 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1089}
1090
1091static void
1092weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
1093{
1094 weston_buffer_reference(&surface->buffer_ref, buffer);
1095
Pekka Paalanena6421c42012-12-04 15:58:10 +02001096 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001097 if (weston_surface_is_mapped(surface))
1098 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001099 }
1100
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001101 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001102}
1103
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001104WL_EXPORT void
1105weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001106{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001107 wl_list_remove(&surface->layer_link);
1108 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001109 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001110 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001111}
1112
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001113WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001114weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001115{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001116 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001117
1118 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001119 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001120}
1121
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001122WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001123weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001124{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001125 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001126
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001127 pixman_region32_union(&compositor->primary_plane.damage,
1128 &compositor->primary_plane.damage,
1129 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001130 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001131}
1132
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001133static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001134surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001135 pixman_region32_t *opaque)
1136{
Pekka Paalanende685b82012-12-04 15:58:12 +02001137 if (surface->buffer_ref.buffer &&
1138 wl_buffer_is_shm(surface->buffer_ref.buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001139 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001140
1141 if (surface->transform.enabled) {
1142 pixman_box32_t *extents;
1143
1144 extents = pixman_region32_extents(&surface->damage);
1145 surface_compute_bbox(surface, extents->x1, extents->y1,
1146 extents->x2 - extents->x1,
1147 extents->y2 - extents->y1,
1148 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001149 pixman_region32_translate(&surface->damage,
1150 -surface->plane->x,
1151 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001152 } else {
1153 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001154 surface->geometry.x - surface->plane->x,
1155 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001156 }
1157
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001158 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001159 pixman_region32_union(&surface->plane->damage,
1160 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001161 empty_region(&surface->damage);
1162 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001163 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001164}
1165
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001166static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001167compositor_accumulate_damage(struct weston_compositor *ec)
1168{
1169 struct weston_plane *plane;
1170 struct weston_surface *es;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001171 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001172
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001173 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001174
1175 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001176 pixman_region32_copy(&plane->clip, &clip);
1177
1178 pixman_region32_init(&opaque);
1179
1180 wl_list_for_each(es, &ec->surface_list, link) {
1181 if (es->plane != plane)
1182 continue;
1183
1184 surface_accumulate_damage(es, &opaque);
1185 }
1186
1187 pixman_region32_union(&clip, &clip, &opaque);
1188 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001189 }
1190
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001191 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001192
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001193 wl_list_for_each(es, &ec->surface_list, link) {
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001194 /* Both the renderer and the backend have seen the buffer
1195 * by now. If renderer needs the buffer, it has its own
1196 * reference set. If the backend wants to keep the buffer
1197 * around for migrating the surface into a non-primary plane
1198 * later, keep_buffer is true. Otherwise, drop the core
1199 * reference now, and allow early buffer release. This enables
1200 * clients to use single-buffering.
1201 */
1202 if (!es->keep_buffer)
1203 weston_buffer_reference(&es->buffer_ref, NULL);
1204 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001205}
1206
1207static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001208weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001209{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001210 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001211 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001212 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001213 struct weston_animation *animation, *next;
1214 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001215 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001216 pixman_region32_t output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001217
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001218 weston_compositor_update_drag_surfaces(ec);
1219
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001220 /* Rebuild the surface list and update surface transforms up front. */
1221 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +02001222 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001223 wl_list_for_each(layer, &ec->layer_list, link) {
1224 wl_list_for_each(es, &layer->surface_list, layer_link) {
1225 weston_surface_update_transform(es);
1226 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +02001227 if (es->output == output) {
1228 wl_list_insert_list(&frame_callback_list,
1229 &es->frame_callback_list);
1230 wl_list_init(&es->frame_callback_list);
1231 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001232 }
1233 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001234
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001235 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001236 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001237 else
1238 wl_list_for_each(es, &ec->surface_list, link)
1239 weston_surface_move_to_plane(es, &ec->primary_plane);
1240
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001241 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001242
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001243 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001244 pixman_region32_intersect(&output_damage,
1245 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001246 pixman_region32_subtract(&output_damage,
1247 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001248
Scott Moreauccbf29d2012-02-22 14:21:41 -07001249 if (output->dirty)
1250 weston_output_update_matrix(output);
1251
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001252 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001253
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001254 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001255
Kristian Høgsbergef044142011-06-21 15:02:12 -04001256 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001257
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001258 weston_compositor_repick(ec);
1259 wl_event_loop_dispatch(ec->input_loop, 0);
1260
Jonas Ådahldb773762012-06-13 00:01:21 +02001261 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001262 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001263 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001264 }
1265
Scott Moreaud64cf212012-06-08 19:40:54 -06001266 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001267 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001268 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001269 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001270}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001271
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001272static int
1273weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001274{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001275 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001276
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001277 wl_event_loop_dispatch(compositor->input_loop, 0);
1278
1279 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001280}
1281
1282WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001283weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001284{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001285 struct weston_compositor *compositor = output->compositor;
1286 struct wl_event_loop *loop =
1287 wl_display_get_event_loop(compositor->wl_display);
1288 int fd;
1289
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001290 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001291 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001292 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001293 return;
1294 }
1295
1296 output->repaint_scheduled = 0;
1297 if (compositor->input_loop_source)
1298 return;
1299
1300 fd = wl_event_loop_get_fd(compositor->input_loop);
1301 compositor->input_loop_source =
1302 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1303 weston_compositor_read_input, compositor);
1304}
1305
1306static void
1307idle_repaint(void *data)
1308{
1309 struct weston_output *output = data;
1310
1311 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001312}
1313
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001314WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001315weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1316{
1317 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001318 if (below != NULL)
1319 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001320}
1321
1322WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001323weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001324{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001325 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001326 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001327
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001328 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001329 return;
1330
Kristian Høgsbergef044142011-06-21 15:02:12 -04001331 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001332 output->repaint_needed = 1;
1333 if (output->repaint_scheduled)
1334 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001335
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001336 wl_event_loop_add_idle(loop, idle_repaint, output);
1337 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001338
1339 if (compositor->input_loop_source) {
1340 wl_event_source_remove(compositor->input_loop_source);
1341 compositor->input_loop_source = NULL;
1342 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001343}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001344
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001345WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001346weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1347{
1348 struct weston_output *output;
1349
1350 wl_list_for_each(output, &compositor->output_list, link)
1351 weston_output_schedule_repaint(output);
1352}
1353
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001354static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001355surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001356{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001357 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001358}
1359
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001360static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001361surface_attach(struct wl_client *client,
1362 struct wl_resource *resource,
1363 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1364{
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001365 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001366 struct wl_buffer *buffer = NULL;
1367
1368 if (buffer_resource)
1369 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001370
Pekka Paalanende685b82012-12-04 15:58:12 +02001371 /* Attach, attach, without commit in between does not send
1372 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001373 if (surface->pending.buffer)
1374 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001375
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001376 surface->pending.sx = sx;
1377 surface->pending.sy = sy;
1378 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001379 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001380 if (buffer) {
1381 wl_signal_add(&buffer->resource.destroy_signal,
1382 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001383 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001384}
1385
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001386static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001387surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001388 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001389 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001390{
Pekka Paalanen8e159182012-10-10 12:49:25 +03001391 struct weston_surface *surface = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001392
Pekka Paalanen8e159182012-10-10 12:49:25 +03001393 pixman_region32_union_rect(&surface->pending.damage,
1394 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001395 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001396}
1397
Kristian Høgsberg33418202011-08-16 23:01:28 -04001398static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001399destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001400{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001401 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001402
1403 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001404 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001405}
1406
1407static void
1408surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001409 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001410{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001411 struct weston_frame_callback *cb;
Pekka Paalanenbc106382012-10-10 12:49:31 +03001412 struct weston_surface *surface = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001413
1414 cb = malloc(sizeof *cb);
1415 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001416 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001417 return;
1418 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001419
Kristian Høgsberg33418202011-08-16 23:01:28 -04001420 cb->resource.object.interface = &wl_callback_interface;
1421 cb->resource.object.id = callback;
1422 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001423 cb->resource.client = client;
1424 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001425
1426 wl_client_add_resource(client, &cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001427 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001428}
1429
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001430static void
1431surface_set_opaque_region(struct wl_client *client,
1432 struct wl_resource *resource,
1433 struct wl_resource *region_resource)
1434{
1435 struct weston_surface *surface = resource->data;
1436 struct weston_region *region;
1437
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001438 if (region_resource) {
1439 region = region_resource->data;
Pekka Paalanen512dde82012-10-10 12:49:27 +03001440 pixman_region32_copy(&surface->pending.opaque,
1441 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001442 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001443 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001444 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001445}
1446
1447static void
1448surface_set_input_region(struct wl_client *client,
1449 struct wl_resource *resource,
1450 struct wl_resource *region_resource)
1451{
1452 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001453 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001454
1455 if (region_resource) {
1456 region = region_resource->data;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001457 pixman_region32_copy(&surface->pending.input,
1458 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001459 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001460 pixman_region32_fini(&surface->pending.input);
1461 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001462 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001463}
1464
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001465static void
1466surface_commit(struct wl_client *client, struct wl_resource *resource)
1467{
1468 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001469 pixman_region32_t opaque;
Giulio Camuffo184df502013-02-21 11:29:21 +01001470 int buffer_width = 0;
1471 int buffer_height = 0;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001472
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001473 /* wl_surface.set_buffer_rotation */
1474 surface->buffer_transform = surface->pending.buffer_transform;
1475
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001476 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001477 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001478 weston_surface_attach(surface, surface->pending.buffer);
1479
Giulio Camuffo184df502013-02-21 11:29:21 +01001480 if (surface->buffer_ref.buffer) {
1481 buffer_width = weston_surface_buffer_width(surface);
1482 buffer_height = weston_surface_buffer_height(surface);
1483 }
1484
1485 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001486 surface->configure(surface,
1487 surface->pending.sx, surface->pending.sy,
1488 buffer_width, buffer_height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001489
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001490 if (surface->pending.buffer)
1491 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1492 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001493 surface->pending.sx = 0;
1494 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001495 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001496
1497 /* wl_surface.damage */
1498 pixman_region32_union(&surface->damage, &surface->damage,
1499 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001500 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1501 0, 0,
1502 surface->geometry.width,
1503 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001504 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001505
1506 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001507 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001508 surface->geometry.width,
1509 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001510 pixman_region32_intersect(&opaque,
1511 &opaque, &surface->pending.opaque);
1512
1513 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1514 pixman_region32_copy(&surface->opaque, &opaque);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001515 weston_surface_geometry_dirty(surface);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001516 }
1517
1518 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001519
1520 /* wl_surface.set_input_region */
1521 pixman_region32_fini(&surface->input);
1522 pixman_region32_init_rect(&surface->input, 0, 0,
1523 surface->geometry.width,
1524 surface->geometry.height);
1525 pixman_region32_intersect(&surface->input,
1526 &surface->input, &surface->pending.input);
1527
Pekka Paalanenbc106382012-10-10 12:49:31 +03001528 /* wl_surface.frame */
1529 wl_list_insert_list(&surface->frame_callback_list,
1530 &surface->pending.frame_callback_list);
1531 wl_list_init(&surface->pending.frame_callback_list);
1532
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001533 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001534}
1535
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001536static void
1537surface_set_buffer_transform(struct wl_client *client,
1538 struct wl_resource *resource, int transform)
1539{
1540 struct weston_surface *surface = resource->data;
1541
1542 surface->pending.buffer_transform = transform;
1543}
1544
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001545static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001546 surface_destroy,
1547 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001548 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001549 surface_frame,
1550 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001551 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001552 surface_commit,
1553 surface_set_buffer_transform
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001554};
1555
1556static void
1557compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001558 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001559{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001560 struct weston_compositor *ec = resource->data;
1561 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001562
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001563 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001564 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001565 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001566 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001567 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001568
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001569 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001570
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001571 surface->surface.resource.object.id = id;
1572 surface->surface.resource.object.interface = &wl_surface_interface;
1573 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001574 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001575 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001576
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001577 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001578}
1579
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001580static void
1581destroy_region(struct wl_resource *resource)
1582{
1583 struct weston_region *region =
1584 container_of(resource, struct weston_region, resource);
1585
1586 pixman_region32_fini(&region->region);
1587 free(region);
1588}
1589
1590static void
1591region_destroy(struct wl_client *client, struct wl_resource *resource)
1592{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001593 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001594}
1595
1596static void
1597region_add(struct wl_client *client, struct wl_resource *resource,
1598 int32_t x, int32_t y, int32_t width, int32_t height)
1599{
1600 struct weston_region *region = resource->data;
1601
1602 pixman_region32_union_rect(&region->region, &region->region,
1603 x, y, width, height);
1604}
1605
1606static void
1607region_subtract(struct wl_client *client, struct wl_resource *resource,
1608 int32_t x, int32_t y, int32_t width, int32_t height)
1609{
1610 struct weston_region *region = resource->data;
1611 pixman_region32_t rect;
1612
1613 pixman_region32_init_rect(&rect, x, y, width, height);
1614 pixman_region32_subtract(&region->region, &region->region, &rect);
1615 pixman_region32_fini(&rect);
1616}
1617
1618static const struct wl_region_interface region_interface = {
1619 region_destroy,
1620 region_add,
1621 region_subtract
1622};
1623
1624static void
1625compositor_create_region(struct wl_client *client,
1626 struct wl_resource *resource, uint32_t id)
1627{
1628 struct weston_region *region;
1629
1630 region = malloc(sizeof *region);
1631 if (region == NULL) {
1632 wl_resource_post_no_memory(resource);
1633 return;
1634 }
1635
1636 region->resource.destroy = destroy_region;
1637
1638 region->resource.object.id = id;
1639 region->resource.object.interface = &wl_region_interface;
1640 region->resource.object.implementation =
1641 (void (**)(void)) &region_interface;
1642 region->resource.data = region;
1643
1644 pixman_region32_init(&region->region);
1645
1646 wl_client_add_resource(client, &region->resource);
1647}
1648
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001649static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001650 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001651 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001652};
1653
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001654static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02001655weston_compositor_dpms(struct weston_compositor *compositor,
1656 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001657{
1658 struct weston_output *output;
1659
1660 wl_list_for_each(output, &compositor->output_list, link)
1661 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02001662 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001663}
1664
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001665WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02001666weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001667{
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02001668 switch (compositor->state) {
1669 case WESTON_COMPOSITOR_SLEEPING:
1670 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
1671 /* fall through */
1672 case WESTON_COMPOSITOR_IDLE:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02001673 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02001674 /* fall through */
1675 default:
1676 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1677 wl_event_source_timer_update(compositor->idle_source,
1678 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001679 }
1680}
1681
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02001682WL_EXPORT void
1683weston_compositor_sleep(struct weston_compositor *compositor)
1684{
1685 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
1686 return;
1687
1688 compositor->state = WESTON_COMPOSITOR_SLEEPING;
1689 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
1690}
1691
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001692static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001693weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001694{
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02001695 weston_compositor_wake(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001696 compositor->idle_inhibit++;
1697}
1698
1699static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001700weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001701{
1702 compositor->idle_inhibit--;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02001703 weston_compositor_wake(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001704}
1705
1706static int
1707idle_handler(void *data)
1708{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001709 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001710
1711 if (compositor->idle_inhibit)
1712 return 1;
1713
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02001714 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02001715 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001716
1717 return 1;
1718}
1719
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001720WL_EXPORT void
1721weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1722{
1723 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001724 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001725 plane->x = x;
1726 plane->y = y;
1727}
1728
1729WL_EXPORT void
1730weston_plane_release(struct weston_plane *plane)
1731{
1732 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001733 pixman_region32_fini(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001734}
1735
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001736WL_EXPORT void
1737weston_compositor_stack_plane(struct weston_compositor *ec,
1738 struct weston_plane *plane,
1739 struct weston_plane *above)
1740{
1741 if (above)
1742 wl_list_insert(above->link.prev, &plane->link);
1743 else
1744 wl_list_insert(&ec->plane_list, &plane->link);
1745}
1746
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001747static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001748weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001749
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001750static void
Daniel Stone37816df2012-05-16 18:45:18 +01001751clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001752{
Daniel Stone37816df2012-05-16 18:45:18 +01001753 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001754 struct weston_output *output, *prev = NULL;
1755 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001756
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001757 x = wl_fixed_to_int(*fx);
1758 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001759 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1760 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001761
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001762 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001763 if (pixman_region32_contains_point(&output->region,
1764 x, y, NULL))
1765 valid = 1;
1766 if (pixman_region32_contains_point(&output->region,
1767 old_x, old_y, NULL))
1768 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001769 }
Daniel Stone37816df2012-05-16 18:45:18 +01001770
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001771 if (!valid) {
1772 if (x < prev->x)
1773 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001774 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001775 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001776 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001777 if (y < prev->y)
1778 *fy = wl_fixed_from_int(prev->y);
1779 else if (y >= prev->y + prev->current->height)
1780 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001781 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001782 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001783}
1784
Kristian Høgsberge2464fd2013-02-25 16:49:29 -05001785static void
Kristian Høgsberg068b61c2013-02-25 17:04:47 -05001786move_pointer(struct weston_seat *seat, wl_fixed_t dx, wl_fixed_t dy)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001787{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001788 struct weston_compositor *ec = seat->compositor;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001789 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge2464fd2013-02-25 16:49:29 -05001790 struct weston_output *output;
Kristian Høgsberg068b61c2013-02-25 17:04:47 -05001791 wl_fixed_t x, y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001792 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001793
Kristian Høgsberg068b61c2013-02-25 17:04:47 -05001794 x = pointer->x + dx;
1795 y = pointer->y + dy;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001796 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001797
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001798 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001799
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001800 pointer->x = x;
1801 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001802
1803 ix = wl_fixed_to_int(x);
1804 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001805
Scott Moreauccbf29d2012-02-22 14:21:41 -07001806 wl_list_for_each(output, &ec->output_list, link)
1807 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001808 pixman_region32_contains_point(&output->region,
1809 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06001810 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001811
Daniel Stone37816df2012-05-16 18:45:18 +01001812 weston_device_repick(seat);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001813
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001814 if (seat->sprite) {
1815 weston_surface_set_position(seat->sprite,
1816 ix - seat->hotspot_x,
1817 iy - seat->hotspot_y);
1818 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001819 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001820}
1821
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001822WL_EXPORT void
Kristian Høgsberge2464fd2013-02-25 16:49:29 -05001823notify_motion(struct weston_seat *seat,
Kristian Høgsberg068b61c2013-02-25 17:04:47 -05001824 uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
Kristian Høgsberge2464fd2013-02-25 16:49:29 -05001825{
1826 const struct wl_pointer_grab_interface *interface;
1827 struct weston_compositor *ec = seat->compositor;
1828 struct wl_pointer *pointer = seat->seat.pointer;
1829
1830 weston_compositor_wake(ec);
1831
Kristian Høgsberg068b61c2013-02-25 17:04:47 -05001832 move_pointer(seat, dx, dy);
Kristian Høgsberge2464fd2013-02-25 16:49:29 -05001833
1834 interface = pointer->grab->interface;
1835 interface->motion(pointer->grab, time,
1836 pointer->grab->x, pointer->grab->y);
1837}
1838
1839WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001840weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001841 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001842{
Daniel Stone37816df2012-05-16 18:45:18 +01001843 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001844
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001845 if (seat->seat.keyboard) {
1846 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1847 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04001848 }
1849
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001850 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001851}
1852
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001853WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001854notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001855 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001856{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001857 struct weston_compositor *compositor = seat->compositor;
1858 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001859 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001860 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001861 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001862
Daniel Stone4dbadb12012-05-30 16:31:51 +01001863 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001864 if (compositor->ping_handler && focus)
1865 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001866 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001867 if (pointer->button_count == 0) {
1868 pointer->grab_button = button;
1869 pointer->grab_time = time;
1870 pointer->grab_x = pointer->x;
1871 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001872 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001873 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001874 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001875 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001876 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001877 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001878
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001879 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001880 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001881
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001882 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001883
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001884 if (pointer->button_count == 1)
1885 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001886 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001887}
1888
Scott Moreau210d0792012-03-22 10:47:01 -06001889WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001890notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01001891 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001892{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001893 struct weston_compositor *compositor = seat->compositor;
1894 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001895 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001896 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001897 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1898
1899 if (compositor->ping_handler && focus)
1900 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001901
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02001902 weston_compositor_wake(compositor);
Scott Moreau210d0792012-03-22 10:47:01 -06001903
Rune K. Svendsen14b2fe72013-03-07 21:50:00 +01001904 if (!value)
1905 return;
1906
1907 if (weston_compositor_run_axis_binding(compositor, seat,
1908 time, axis, value))
Scott Moreau6a3633d2012-03-20 08:47:59 -06001909 return;
1910
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001911 if (pointer->focus_resource)
1912 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01001913 value);
Scott Moreau210d0792012-03-22 10:47:01 -06001914}
1915
Daniel Stone05d58682012-06-22 13:21:38 +01001916WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001917notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001918{
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001919 struct wl_keyboard *keyboard = &seat->keyboard.keyboard;
Daniel Stone05d58682012-06-22 13:21:38 +01001920 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01001921 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001922 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01001923 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01001924 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01001925
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001926 /* Serialize and update our internal state, checking to see if it's
1927 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01001928 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1929 XKB_STATE_DEPRESSED);
1930 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1931 XKB_STATE_LATCHED);
1932 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1933 XKB_STATE_LOCKED);
1934 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01001935 XKB_STATE_EFFECTIVE);
1936
Daniel Stone71c38772012-06-22 13:21:30 +01001937 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
1938 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
1939 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
1940 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01001941 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01001942
Daniel Stone71c38772012-06-22 13:21:30 +01001943 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
1944 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
1945 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
1946 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001947
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001948 /* And update the modifier_state for bindings. */
1949 mods_lookup = mods_depressed | mods_latched;
1950 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01001951 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001952 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01001953 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001954 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01001955 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001956 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04001957 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
1958 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01001959
Daniel Stone8c8164f2012-05-30 16:31:45 +01001960 /* Finally, notify the compositor that LEDs have changed. */
1961 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001962 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001963 leds |= LED_NUM_LOCK;
1964 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001965 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001966 leds |= LED_CAPS_LOCK;
1967 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001968 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001969 leds |= LED_SCROLL_LOCK;
1970 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01001971 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01001972 seat->xkb_state.leds = leds;
1973
Daniel Stone05d58682012-06-22 13:21:38 +01001974 if (changed) {
1975 grab->interface->modifiers(grab,
1976 serial,
1977 keyboard->modifiers.mods_depressed,
1978 keyboard->modifiers.mods_latched,
1979 keyboard->modifiers.mods_locked,
1980 keyboard->modifiers.group);
1981 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001982}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001983
Daniel Stone05d58682012-06-22 13:21:38 +01001984static void
1985update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001986 enum wl_keyboard_key_state state)
1987{
1988 enum xkb_key_direction direction;
1989
1990 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1991 direction = XKB_KEY_DOWN;
1992 else
1993 direction = XKB_KEY_UP;
1994
1995 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1996 * broken keycode system, which starts at 8. */
1997 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1998
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001999 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002000}
2001
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002002WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002003notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01002004 enum wl_keyboard_key_state state,
2005 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002006{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002007 struct weston_compositor *compositor = seat->compositor;
Jan Arne Petersena75a7892013-01-16 21:26:50 +01002008 struct weston_keyboard *keyboard = &seat->keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01002009 struct weston_surface *focus =
Jan Arne Petersena75a7892013-01-16 21:26:50 +01002010 (struct weston_surface *) keyboard->keyboard.focus;
2011 struct wl_keyboard_grab *grab = keyboard->keyboard.grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002012 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002013 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002014
Daniel Stonec9785ea2012-05-30 16:31:52 +01002015 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002016 if (compositor->ping_handler && focus)
2017 compositor->ping_handler(focus, serial);
2018
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002019 weston_compositor_idle_inhibit(compositor);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01002020 keyboard->keyboard.grab_key = key;
2021 keyboard->keyboard.grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07002022 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002023 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07002024 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002025
Jan Arne Petersena75a7892013-01-16 21:26:50 +01002026 end = keyboard->keyboard.keys.data + keyboard->keyboard.keys.size;
2027 for (k = keyboard->keyboard.keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01002028 if (*k == key) {
2029 /* Ignore server-generated repeats. */
2030 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2031 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002032 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01002033 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002034 }
Jan Arne Petersena75a7892013-01-16 21:26:50 +01002035 keyboard->keyboard.keys.size = (void *) end - keyboard->keyboard.keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002036 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jan Arne Petersena75a7892013-01-16 21:26:50 +01002037 k = wl_array_add(&keyboard->keyboard.keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002038 *k = key;
2039 }
Ray Strodee96dcb82008-12-20 02:00:49 -05002040
Jan Arne Petersena75a7892013-01-16 21:26:50 +01002041 if (grab == &keyboard->keyboard.default_grab ||
2042 grab == &keyboard->input_method_grab) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002043 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002044 state);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01002045 grab = keyboard->keyboard.grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06002046 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05002047
Daniel Stone7ace3902012-05-30 16:31:40 +01002048 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01002049
2050 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002051 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01002052 wl_display_get_serial(compositor->wl_display),
2053 key,
2054 state);
2055 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002056}
2057
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002058WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002059notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01002060 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002061{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002062 struct weston_compositor *compositor = seat->compositor;
2063 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002064
2065 if (output) {
Kristian Høgsberg00fbbe62012-10-23 13:04:09 -04002066 clip_pointer_motion(seat, &x, &y);
Daniel Stone37816df2012-05-16 18:45:18 +01002067 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002068 x - pointer->x,
2069 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002070
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002071 pointer->x = x;
2072 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002073 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002074 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002075 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002076 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03002077 /* FIXME: We should call wl_pointer_set_focus(seat,
2078 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002079 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002080}
2081
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002082static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002083destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002084{
Daniel Stone37816df2012-05-16 18:45:18 +01002085 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002086
Daniel Stone37816df2012-05-16 18:45:18 +01002087 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002088 saved_kbd_focus_listener);
2089
Daniel Stone37816df2012-05-16 18:45:18 +01002090 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002091}
2092
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002093WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002094notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002095 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002096{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002097 struct weston_compositor *compositor = seat->compositor;
2098 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04002099 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002100 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05002101
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002102 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002103 wl_array_copy(&keyboard->keys, keys);
2104 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002105 weston_compositor_idle_inhibit(compositor);
2106 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002107 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002108 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002109 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002110
Daniel Stoneef172672012-06-22 13:21:32 +01002111 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002112 wl_array_for_each(k, &keyboard->keys) {
2113 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01002114 WL_KEYBOARD_KEY_STATE_PRESSED);
2115 }
2116
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002117 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002118
2119 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002120 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2121 wl_keyboard_set_focus(keyboard, surface);
2122 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002123 }
2124}
2125
2126WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002127notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01002128{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002129 struct weston_compositor *compositor = seat->compositor;
2130 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002131 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002132
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002133 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002134 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002135 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002136 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002137 WL_KEYBOARD_KEY_STATE_RELEASED);
2138 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002139
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002140 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002141
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002142 if (keyboard->focus) {
2143 seat->saved_kbd_focus = keyboard->focus;
2144 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01002145 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002146 wl_signal_add(&keyboard->focus->resource.destroy_signal,
2147 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002148 }
2149
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002150 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002151 /* FIXME: We really need keyboard grab cancel here to
2152 * let the grab shut down properly. As it is we leak
2153 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002154 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002155}
2156
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002157static void
Daniel Stone37816df2012-05-16 18:45:18 +01002158touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002159{
Daniel Stone37816df2012-05-16 18:45:18 +01002160 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002161 struct wl_resource *resource;
2162
Pekka Paalanen56464252012-07-31 13:21:08 +03002163 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002164 return;
2165
Pekka Paalanen56464252012-07-31 13:21:08 +03002166 if (seat->touch->focus_resource)
2167 wl_list_remove(&seat->touch->focus_listener.link);
2168 seat->touch->focus = NULL;
2169 seat->touch->focus_resource = NULL;
2170
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002171 if (surface) {
2172 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01002173 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04002174 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002175 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02002176 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002177 return;
2178 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002179
Daniel Stone37816df2012-05-16 18:45:18 +01002180 seat->touch->focus = surface;
2181 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03002182 wl_signal_add(&resource->destroy_signal,
2183 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002184 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002185}
2186
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002187/**
2188 * notify_touch - emulates button touches and notifies surfaces accordingly.
2189 *
2190 * It assumes always the correct cycle sequence until it gets here: touch_down
2191 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2192 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002193 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002194 */
2195WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002196notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002197 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002198{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002199 struct weston_compositor *ec = seat->compositor;
2200 struct wl_touch *touch = seat->seat.touch;
Matt Roper47c1b982012-10-10 16:56:53 -07002201 struct wl_touch_grab *grab = touch->grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002202 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002203 wl_fixed_t sx, sy;
Matt Roper47c1b982012-10-10 16:56:53 -07002204
2205 /* Update grab's global coordinates. */
2206 touch->grab_x = x;
2207 touch->grab_y = y;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002208
2209 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01002210 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002211 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002212
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002213 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002214
2215 /* the first finger down picks the surface, and all further go
2216 * to that surface for the remainder of the touch session i.e.
2217 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002218 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002219 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002220 touch_set_focus(seat, &es->surface);
2221 } else if (touch->focus) {
2222 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002223 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Rob Bradford26e009c2012-12-05 18:47:05 +00002224 } else {
2225 /* Unexpected condition: We have non-initial touch but
2226 * there is no focused surface.
2227 */
2228 weston_log("touch event received with %d points down"
2229 "but no surface focused\n", seat->num_tp);
2230 return;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002231 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002232
Matt Roper47c1b982012-10-10 16:56:53 -07002233 grab->interface->down(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002234 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002235 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002236 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002237 if (!es)
2238 break;
2239
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002240 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Matt Roper47c1b982012-10-10 16:56:53 -07002241 grab->interface->motion(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002242 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002243 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002244 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002245 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002246
Matt Roper47c1b982012-10-10 16:56:53 -07002247 grab->interface->up(grab, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002248 if (seat->num_tp == 0)
2249 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002250 break;
2251 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002252}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002253
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04002254static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002255pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
2256{
2257 struct weston_seat *seat = container_of(listener, struct weston_seat,
2258 sprite_destroy_listener);
2259
2260 seat->sprite = NULL;
2261}
2262
2263static void
2264pointer_cursor_surface_configure(struct weston_surface *es,
Giulio Camuffo184df502013-02-21 11:29:21 +01002265 int32_t dx, int32_t dy, int32_t width, int32_t height)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002266{
2267 struct weston_seat *seat = es->private;
2268 int x, y;
2269
Giulio Camuffo184df502013-02-21 11:29:21 +01002270 if (width == 0)
2271 return;
2272
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002273 assert(es == seat->sprite);
2274
2275 seat->hotspot_x -= dx;
2276 seat->hotspot_y -= dy;
2277
2278 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
2279 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
2280
2281 weston_surface_configure(seat->sprite, x, y,
Giulio Camuffo184df502013-02-21 11:29:21 +01002282 width, height);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002283
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002284 empty_region(&es->pending.input);
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03002285
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002286 if (!weston_surface_is_mapped(es)) {
2287 wl_list_insert(&es->compositor->cursor_layer.surface_list,
2288 &es->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002289 weston_surface_update_transform(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002290 }
2291}
2292
2293static void
2294pointer_unmap_sprite(struct weston_seat *seat)
2295{
2296 if (weston_surface_is_mapped(seat->sprite))
2297 weston_surface_unmap(seat->sprite);
2298
2299 wl_list_remove(&seat->sprite_destroy_listener.link);
2300 seat->sprite->configure = NULL;
2301 seat->sprite->private = NULL;
2302 seat->sprite = NULL;
2303}
2304
2305static void
2306pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2307 uint32_t serial, struct wl_resource *surface_resource,
2308 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002309{
Daniel Stone37816df2012-05-16 18:45:18 +01002310 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002311 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002312
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002313 if (surface_resource)
Pekka Paalanen6c71ee12012-10-10 12:49:30 +03002314 surface = surface_resource->data;
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002315
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04002316 if (seat->seat.pointer->focus == NULL)
2317 return;
2318 if (seat->seat.pointer->focus->resource.client != client)
2319 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002320 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002321 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002322
2323 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002324 if (surface->configure) {
2325 wl_resource_post_error(&surface->surface.resource,
2326 WL_DISPLAY_ERROR_INVALID_OBJECT,
2327 "surface->configure already "
2328 "set");
2329 return;
2330 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002331 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002332
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002333 if (seat->sprite)
2334 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002335
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002336 if (!surface)
2337 return;
2338
2339 wl_signal_add(&surface->surface.resource.destroy_signal,
2340 &seat->sprite_destroy_listener);
2341
2342 surface->configure = pointer_cursor_surface_configure;
2343 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002344 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002345 seat->hotspot_x = x;
2346 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002347
Pekka Paalanende685b82012-12-04 15:58:12 +02002348 if (surface->buffer_ref.buffer)
Giulio Camuffo184df502013-02-21 11:29:21 +01002349 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
2350 weston_surface_buffer_height(surface));
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002351}
2352
Daniel Stone37816df2012-05-16 18:45:18 +01002353static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002354 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002355};
2356
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002357static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002358handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002359{
Daniel Stone37816df2012-05-16 18:45:18 +01002360 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002361
Daniel Stone37816df2012-05-16 18:45:18 +01002362 seat = container_of(listener, struct weston_seat,
2363 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002364
Daniel Stone37816df2012-05-16 18:45:18 +01002365 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002366}
2367
Casey Dahlin9074db52012-04-19 22:50:09 -04002368static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002369{
2370 wl_list_remove(&resource->link);
2371 free(resource);
2372}
2373
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002374static void
Daniel Stone37816df2012-05-16 18:45:18 +01002375seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2376 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002377{
Daniel Stone37816df2012-05-16 18:45:18 +01002378 struct weston_seat *seat = resource->data;
2379 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002380
Daniel Stone37816df2012-05-16 18:45:18 +01002381 if (!seat->seat.pointer)
2382 return;
2383
2384 cr = wl_client_add_object(client, &wl_pointer_interface,
2385 &pointer_interface, id, seat);
2386 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002387 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002388
2389 if (seat->seat.pointer->focus &&
2390 seat->seat.pointer->focus->resource.client == client) {
2391 struct weston_surface *surface;
2392 wl_fixed_t sx, sy;
2393
2394 surface = (struct weston_surface *) seat->seat.pointer->focus;
2395 weston_surface_from_global_fixed(surface,
2396 seat->seat.pointer->x,
2397 seat->seat.pointer->y,
2398 &sx,
2399 &sy);
2400 wl_pointer_set_focus(seat->seat.pointer,
2401 seat->seat.pointer->focus,
2402 sx,
2403 sy);
2404 }
Daniel Stone37816df2012-05-16 18:45:18 +01002405}
2406
2407static void
2408seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2409 uint32_t id)
2410{
2411 struct weston_seat *seat = resource->data;
2412 struct wl_resource *cr;
2413
2414 if (!seat->seat.keyboard)
2415 return;
2416
2417 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2418 seat);
2419 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002420 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002421
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002422 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2423 seat->xkb_info.keymap_fd,
2424 seat->xkb_info.keymap_size);
2425
Daniel Stone17b13bd2012-05-30 16:31:39 +01002426 if (seat->seat.keyboard->focus &&
2427 seat->seat.keyboard->focus->resource.client == client) {
2428 wl_keyboard_set_focus(seat->seat.keyboard,
2429 seat->seat.keyboard->focus);
Rafal Mielniczukdb790572013-02-17 15:55:55 +01002430 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone17b13bd2012-05-30 16:31:39 +01002431 }
Daniel Stone37816df2012-05-16 18:45:18 +01002432}
2433
2434static void
2435seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2436 uint32_t id)
2437{
2438 struct weston_seat *seat = resource->data;
2439 struct wl_resource *cr;
2440
2441 if (!seat->seat.touch)
2442 return;
2443
2444 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2445 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002446 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002447}
2448
2449static const struct wl_seat_interface seat_interface = {
2450 seat_get_pointer,
2451 seat_get_keyboard,
2452 seat_get_touch,
2453};
2454
2455static void
2456bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2457{
2458 struct wl_seat *seat = data;
2459 struct wl_resource *resource;
2460 enum wl_seat_capability caps = 0;
2461
2462 resource = wl_client_add_object(client, &wl_seat_interface,
2463 &seat_interface, id, data);
2464 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002465 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002466
2467 if (seat->pointer)
2468 caps |= WL_SEAT_CAPABILITY_POINTER;
2469 if (seat->keyboard)
2470 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2471 if (seat->touch)
2472 caps |= WL_SEAT_CAPABILITY_TOUCH;
2473
2474 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002475}
2476
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002477static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002478device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002479{
Daniel Stone37816df2012-05-16 18:45:18 +01002480 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002481
Daniel Stone37816df2012-05-16 18:45:18 +01002482 seat = container_of(listener, struct weston_seat,
2483 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002484
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002485 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002486}
2487
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002488static int
2489weston_compositor_xkb_init(struct weston_compositor *ec,
2490 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002491{
Daniel Stonee379da92012-05-30 16:32:04 +01002492 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002493 ec->xkb_context = xkb_context_new(0);
2494 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002495 weston_log("failed to create XKB context\n");
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002496 return -1;
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002497 }
Daniel Stone994679a2012-05-30 16:31:43 +01002498 }
2499
2500 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002501 ec->xkb_names = *names;
2502 if (!ec->xkb_names.rules)
2503 ec->xkb_names.rules = strdup("evdev");
2504 if (!ec->xkb_names.model)
2505 ec->xkb_names.model = strdup("pc105");
2506 if (!ec->xkb_names.layout)
2507 ec->xkb_names.layout = strdup("us");
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002508
2509 return 0;
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002510}
2511
Daniel Stonee379da92012-05-30 16:32:04 +01002512static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2513{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002514 if (xkb_info->keymap)
2515 xkb_map_unref(xkb_info->keymap);
2516
2517 if (xkb_info->keymap_area)
2518 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2519 if (xkb_info->keymap_fd >= 0)
2520 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002521}
2522
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002523static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2524{
Daniel Stonee379da92012-05-30 16:32:04 +01002525 free((char *) ec->xkb_names.rules);
2526 free((char *) ec->xkb_names.model);
2527 free((char *) ec->xkb_names.layout);
2528 free((char *) ec->xkb_names.variant);
2529 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002530
Daniel Stonee379da92012-05-30 16:32:04 +01002531 xkb_info_destroy(&ec->xkb_info);
2532 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002533}
2534
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002535static int
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002536weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002537{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002538 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002539
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002540 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2541 XKB_MOD_NAME_SHIFT);
2542 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2543 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002544 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2545 XKB_MOD_NAME_CTRL);
2546 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2547 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002548 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2549 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002550 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2551 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002552 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002553
2554 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2555 XKB_LED_NAME_NUM);
2556 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2557 XKB_LED_NAME_CAPS);
2558 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2559 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002560
2561 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2562 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002563 weston_log("failed to get string version of keymap\n");
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002564 return -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002565 }
2566 xkb_info->keymap_size = strlen(keymap_str) + 1;
2567
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002568 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002569 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002570 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002571 (unsigned long) xkb_info->keymap_size);
2572 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002573 }
2574
2575 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2576 PROT_READ | PROT_WRITE,
2577 MAP_SHARED, xkb_info->keymap_fd, 0);
2578 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002579 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002580 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002581 goto err_dev_zero;
2582 }
2583 strcpy(xkb_info->keymap_area, keymap_str);
2584 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002585
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002586 return 0;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002587
2588err_dev_zero:
2589 close(xkb_info->keymap_fd);
2590 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002591err_keymap_str:
2592 free(keymap_str);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002593 return -1;
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002594}
2595
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002596static int
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002597weston_compositor_build_global_keymap(struct weston_compositor *ec)
2598{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002599 if (ec->xkb_info.keymap != NULL)
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002600 return 0;
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002601
Daniel Stonee379da92012-05-30 16:32:04 +01002602 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002603 &ec->xkb_names,
2604 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002605 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002606 weston_log("failed to compile global XKB keymap\n");
2607 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002608 "options %s\n",
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002609 ec->xkb_names.rules, ec->xkb_names.model,
2610 ec->xkb_names.layout, ec->xkb_names.variant,
2611 ec->xkb_names.options);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002612 return -1;
Daniel Stone994679a2012-05-30 16:31:43 +01002613 }
2614
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002615 if (weston_xkb_info_new_keymap(&ec->xkb_info) < 0)
2616 return -1;
2617
2618 return 0;
Daniel Stone994679a2012-05-30 16:31:43 +01002619}
2620
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002621WL_EXPORT int
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002622weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002623{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002624 if (seat->has_keyboard)
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002625 return 0;
Daniel Stone994679a2012-05-30 16:31:43 +01002626
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002627 if (keymap != NULL) {
2628 seat->xkb_info.keymap = xkb_map_ref(keymap);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002629 if (weston_xkb_info_new_keymap(&seat->xkb_info) < 0)
2630 return -1;
2631 } else {
2632 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
2633 return -1;
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002634 seat->xkb_info = seat->compositor->xkb_info;
2635 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2636 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002637
2638 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2639 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002640 weston_log("failed to initialise XKB state\n");
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002641 return -1;
Daniel Stoned65b9092012-05-30 16:32:05 +01002642 }
2643
Daniel Stone71c38772012-06-22 13:21:30 +01002644 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002645
Jan Arne Petersena75a7892013-01-16 21:26:50 +01002646 wl_keyboard_init(&seat->keyboard.keyboard);
2647 wl_seat_set_keyboard(&seat->seat, &seat->keyboard.keyboard);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002648
2649 seat->has_keyboard = 1;
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002650
2651 return 0;
Daniel Stone994679a2012-05-30 16:31:43 +01002652}
2653
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002654WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002655weston_seat_init_pointer(struct weston_seat *seat)
2656{
2657 if (seat->has_pointer)
2658 return;
2659
2660 wl_pointer_init(&seat->pointer);
2661 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2662
2663 seat->has_pointer = 1;
2664}
2665
2666WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002667weston_seat_init_touch(struct weston_seat *seat)
2668{
2669 if (seat->has_touch)
2670 return;
2671
2672 wl_touch_init(&seat->touch);
2673 wl_seat_set_touch(&seat->seat, &seat->touch);
2674
2675 seat->has_touch = 1;
2676}
2677
2678WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002679weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002680{
Daniel Stone37816df2012-05-16 18:45:18 +01002681 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002682 seat->has_pointer = 0;
2683 seat->has_keyboard = 0;
2684 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002685
Daniel Stone37816df2012-05-16 18:45:18 +01002686 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2687 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002688
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002689 seat->sprite = NULL;
2690 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002691
Daniel Stone37816df2012-05-16 18:45:18 +01002692 seat->compositor = ec;
2693 seat->hotspot_x = 16;
2694 seat->hotspot_y = 16;
2695 seat->modifier_state = 0;
2696 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002697
Daniel Stone37816df2012-05-16 18:45:18 +01002698 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002699 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002700
Daniel Stone37816df2012-05-16 18:45:18 +01002701 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002702
Daniel Stone37816df2012-05-16 18:45:18 +01002703 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2704 wl_signal_add(&seat->seat.drag_icon_signal,
2705 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002706
2707 clipboard_create(seat);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002708 wl_signal_emit(&ec->seat_created_signal, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002709}
2710
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002711WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002712weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002713{
Daniel Stone37816df2012-05-16 18:45:18 +01002714 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002715 /* The global object is destroyed at wl_display_destroy() time. */
2716
Daniel Stone37816df2012-05-16 18:45:18 +01002717 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002718 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002719
Daniel Stone74419a22012-05-30 16:32:02 +01002720 if (seat->xkb_state.state != NULL)
2721 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01002722 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002723
Daniel Stone37816df2012-05-16 18:45:18 +01002724 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002725}
2726
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002727static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002728drag_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 +03002729{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002730 empty_region(&es->pending.input);
2731
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002732 weston_surface_configure(es,
2733 es->geometry.x + sx, es->geometry.y + sy,
Giulio Camuffo184df502013-02-21 11:29:21 +01002734 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002735}
2736
2737static int
Daniel Stone37816df2012-05-16 18:45:18 +01002738device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002739 struct weston_surface *surface)
2740{
Daniel Stone37816df2012-05-16 18:45:18 +01002741 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002742
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002743 if (surface->configure) {
2744 wl_resource_post_error(&surface->surface.resource,
2745 WL_DISPLAY_ERROR_INVALID_OBJECT,
2746 "surface->configure already set");
2747 return 0;
2748 }
2749
Daniel Stone37816df2012-05-16 18:45:18 +01002750 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002751
Daniel Stone37816df2012-05-16 18:45:18 +01002752 weston_surface_set_position(ws->drag_surface,
2753 wl_fixed_to_double(seat->pointer->x),
2754 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002755
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002756 surface->configure = drag_surface_configure;
2757
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002758 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002759 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002760
2761 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002762}
2763
2764static void
Daniel Stone37816df2012-05-16 18:45:18 +01002765device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002766{
Ander Conselvan de Oliveira5fd55802012-10-11 14:06:19 +03002767 if (weston_surface_is_mapped(seat->drag_surface))
2768 weston_surface_unmap(seat->drag_surface);
2769
Daniel Stone37816df2012-05-16 18:45:18 +01002770 seat->drag_surface->configure = NULL;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002771 empty_region(&seat->drag_surface->pending.input);
Daniel Stone37816df2012-05-16 18:45:18 +01002772 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2773 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002774}
2775
2776static void
Daniel Stone37816df2012-05-16 18:45:18 +01002777device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002778{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002779 struct wl_list *list;
2780
Daniel Stone37816df2012-05-16 18:45:18 +01002781 if (weston_surface_is_mapped(seat->drag_surface) ||
Pekka Paalanende685b82012-12-04 15:58:12 +02002782 !seat->drag_surface->buffer_ref.buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002783 return;
2784
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002785 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2786 list = &seat->sprite->layer_link;
2787 else
2788 list = &seat->compositor->cursor_layer.surface_list;
2789
2790 wl_list_insert(list, &seat->drag_surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002791 weston_surface_update_transform(seat->drag_surface);
Daniel Stone37816df2012-05-16 18:45:18 +01002792 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002793}
2794
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002795static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002796weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01002797 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002798{
2799 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002800
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002801 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002802 return;
2803
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002804 if (seat->drag_surface && seat->seat.drag_surface &&
2805 (&seat->drag_surface->surface.resource !=
2806 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002807 /* between calls to this funcion we got a new drag_surface */
2808 surface_changed = 1;
2809
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002810 if (!seat->seat.drag_surface || surface_changed) {
2811 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002812 if (!surface_changed)
2813 return;
2814 }
2815
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002816 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002817 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002818 seat->seat.drag_surface;
2819 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002820 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002821 }
2822
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002823 /* the client may not have attached a buffer to the drag surface
2824 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002825 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002826
2827 if (!dx && !dy)
2828 return;
2829
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002830 weston_surface_set_position(seat->drag_surface,
2831 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
2832 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002833}
2834
2835WL_EXPORT void
2836weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2837{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002838 struct weston_seat *seat;
2839
2840 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002841 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002842}
2843
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002844static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002845bind_output(struct wl_client *client,
2846 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002847{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002848 struct weston_output *output = data;
2849 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002850 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002851
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002852 resource = wl_client_add_object(client,
2853 &wl_output_interface, NULL, id, data);
2854
Casey Dahlin9074db52012-04-19 22:50:09 -04002855 wl_list_insert(&output->resource_list, &resource->link);
2856 resource->destroy = unbind_resource;
2857
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002858 wl_output_send_geometry(resource,
2859 output->x,
2860 output->y,
2861 output->mm_width,
2862 output->mm_height,
2863 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002864 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002865 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002866
2867 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002868 wl_output_send_mode(resource,
2869 mode->flags,
2870 mode->width,
2871 mode->height,
2872 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002873 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002874}
2875
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002876WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002877weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002878{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002879 struct weston_compositor *c = output->compositor;
2880
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002881 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002882 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002883 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002884
2885 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002886}
2887
Scott Moreau1bad5db2012-08-18 01:04:05 -06002888static void
2889weston_output_compute_transform(struct weston_output *output)
2890{
2891 struct weston_matrix transform;
2892 int flip;
2893
2894 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002895 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002896
2897 switch(output->transform) {
2898 case WL_OUTPUT_TRANSFORM_FLIPPED:
2899 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2900 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2901 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002902 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002903 flip = -1;
2904 break;
2905 default:
2906 flip = 1;
2907 break;
2908 }
2909
2910 switch(output->transform) {
2911 case WL_OUTPUT_TRANSFORM_NORMAL:
2912 case WL_OUTPUT_TRANSFORM_FLIPPED:
2913 transform.d[0] = flip;
2914 transform.d[1] = 0;
2915 transform.d[4] = 0;
2916 transform.d[5] = 1;
2917 break;
2918 case WL_OUTPUT_TRANSFORM_90:
2919 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2920 transform.d[0] = 0;
2921 transform.d[1] = -flip;
2922 transform.d[4] = 1;
2923 transform.d[5] = 0;
2924 break;
2925 case WL_OUTPUT_TRANSFORM_180:
2926 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2927 transform.d[0] = -flip;
2928 transform.d[1] = 0;
2929 transform.d[4] = 0;
2930 transform.d[5] = -1;
2931 break;
2932 case WL_OUTPUT_TRANSFORM_270:
2933 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2934 transform.d[0] = 0;
2935 transform.d[1] = flip;
2936 transform.d[4] = -1;
2937 transform.d[5] = 0;
2938 break;
2939 default:
2940 break;
2941 }
2942
2943 weston_matrix_multiply(&output->matrix, &transform);
2944}
2945
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002946WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002947weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002948{
Scott Moreau850ca422012-05-21 15:21:25 -06002949 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002950 struct weston_matrix camera;
2951 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002952
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002953 weston_matrix_init(&output->matrix);
2954 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002955 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2956 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002957
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002958 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002959 2.0 / (output->width + output->border.left + output->border.right),
2960 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2961
2962 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002963
Scott Moreauccbf29d2012-02-22 14:21:41 -07002964 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002965 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002966 weston_matrix_init(&camera);
2967 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002968 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002969 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002970 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002971 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002972 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002973 weston_matrix_multiply(&output->matrix, &modelview);
2974 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002975
Scott Moreauccbf29d2012-02-22 14:21:41 -07002976 output->dirty = 0;
2977}
2978
Scott Moreau1bad5db2012-08-18 01:04:05 -06002979static void
2980weston_output_transform_init(struct weston_output *output, uint32_t transform)
2981{
2982 output->transform = transform;
2983
2984 switch (transform) {
2985 case WL_OUTPUT_TRANSFORM_90:
2986 case WL_OUTPUT_TRANSFORM_270:
2987 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2988 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2989 /* Swap width and height */
2990 output->width = output->current->height;
2991 output->height = output->current->width;
2992 break;
2993 case WL_OUTPUT_TRANSFORM_NORMAL:
2994 case WL_OUTPUT_TRANSFORM_180:
2995 case WL_OUTPUT_TRANSFORM_FLIPPED:
2996 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2997 output->width = output->current->width;
2998 output->height = output->current->height;
2999 break;
3000 default:
3001 break;
3002 }
3003}
3004
Scott Moreauccbf29d2012-02-22 14:21:41 -07003005WL_EXPORT void
3006weston_output_move(struct weston_output *output, int x, int y)
3007{
3008 output->x = x;
3009 output->y = y;
3010
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003011 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003012 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003013 output->width,
3014 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003015}
3016
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003017WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003018weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003019 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003020{
3021 output->compositor = c;
3022 output->x = x;
3023 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05003024 output->border.top = 0;
3025 output->border.bottom = 0;
3026 output->border.left = 0;
3027 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003028 output->mm_width = width;
3029 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003030 output->dirty = 1;
3031
Scott Moreau1bad5db2012-08-18 01:04:05 -06003032 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06003033 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003034
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003035 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003036 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003037
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003038 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003039 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003040 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003041
Casey Dahlin58ba1372012-04-19 22:50:08 -04003042 output->id = ffs(~output->compositor->output_id_pool) - 1;
3043 output->compositor->output_id_pool |= 1 << output->id;
3044
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003045 output->global =
3046 wl_display_add_global(c->wl_display, &wl_output_interface,
3047 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003048}
3049
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003050static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003051compositor_bind(struct wl_client *client,
3052 void *data, uint32_t version, uint32_t id)
3053{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003054 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003055
3056 wl_client_add_object(client, &wl_compositor_interface,
3057 &compositor_interface, id, compositor);
3058}
3059
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003060static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003061log_uname(void)
3062{
3063 struct utsname usys;
3064
3065 uname(&usys);
3066
3067 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3068 usys.version, usys.machine);
3069}
3070
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003071WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003072weston_environment_get_fd(const char *env)
3073{
3074 char *e, *end;
3075 int fd, flags;
3076
3077 e = getenv(env);
3078 if (!e)
3079 return -1;
3080 fd = strtol(e, &end, 0);
3081 if (*end != '\0')
3082 return -1;
3083
3084 flags = fcntl(fd, F_GETFD);
3085 if (flags == -1)
3086 return -1;
3087
3088 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3089 unsetenv(env);
3090
3091 return fd;
3092}
3093
3094WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003095weston_compositor_init(struct weston_compositor *ec,
3096 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003097 int *argc, char *argv[],
Daniel Stonec1be8e52012-06-01 11:14:02 -04003098 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003099{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003100 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003101 struct xkb_rule_names xkb_names;
3102 const struct config_key keyboard_config_keys[] = {
3103 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
3104 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
3105 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
3106 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
3107 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
3108 };
3109 const struct config_section cs[] = {
3110 { "keyboard",
3111 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
3112 };
3113
3114 memset(&xkb_names, 0, sizeof(xkb_names));
3115 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003116
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003117 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003118 wl_signal_init(&ec->destroy_signal);
3119 wl_signal_init(&ec->activate_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003120 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003121 wl_signal_init(&ec->idle_signal);
3122 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003123 wl_signal_init(&ec->show_input_panel_signal);
3124 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003125 wl_signal_init(&ec->seat_created_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01003126 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003127
Casey Dahlin58ba1372012-04-19 22:50:08 -04003128 ec->output_id_pool = 0;
3129
Kristian Høgsberga8873122011-11-23 10:39:34 -05003130 if (!wl_display_add_global(display, &wl_compositor_interface,
3131 ec, compositor_bind))
3132 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003133
Daniel Stone725c2c32012-06-22 14:04:36 +01003134 wl_list_init(&ec->surface_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003135 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003136 wl_list_init(&ec->layer_list);
3137 wl_list_init(&ec->seat_list);
3138 wl_list_init(&ec->output_list);
3139 wl_list_init(&ec->key_binding_list);
3140 wl_list_init(&ec->button_binding_list);
3141 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003142 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003143
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003144 weston_plane_init(&ec->primary_plane, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003145 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003146
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003147 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3148 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003149
3150 ec->ping_handler = NULL;
3151
3152 screenshooter_create(ec);
3153 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003154 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003155
3156 wl_data_device_manager_init(ec->wl_display);
3157
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003158 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003159
Daniel Stone725c2c32012-06-22 14:04:36 +01003160 loop = wl_display_get_event_loop(ec->wl_display);
3161 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3162 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3163
3164 ec->input_loop = wl_event_loop_create();
3165
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003166 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3167 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3168
3169 weston_compositor_schedule_repaint(ec);
3170
Daniel Stone725c2c32012-06-22 14:04:36 +01003171 return 0;
3172}
3173
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003174WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003175weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003176{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003177 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003178
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003179 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003180 if (ec->input_loop_source)
3181 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003182
Matt Roper361d2ad2011-08-29 13:52:23 -07003183 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003184 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003185 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003186
Daniel Stone325fc2d2012-05-30 16:31:58 +01003187 weston_binding_list_destroy_all(&ec->key_binding_list);
3188 weston_binding_list_destroy_all(&ec->button_binding_list);
3189 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003190 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003191
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003192 weston_plane_release(&ec->primary_plane);
3193
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003194 wl_array_release(&ec->vertices);
3195 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05003196 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003197
3198 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07003199}
3200
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003201WL_EXPORT void
3202weston_version(int *major, int *minor, int *micro)
3203{
3204 *major = WESTON_VERSION_MAJOR;
3205 *minor = WESTON_VERSION_MINOR;
3206 *micro = WESTON_VERSION_MICRO;
3207}
3208
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003209static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003210{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003211 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003212
Martin Minarik6d118362012-06-07 18:01:59 +02003213 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003214 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003215
3216 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003217}
3218
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003219#ifdef HAVE_LIBUNWIND
3220
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003221static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003222print_backtrace(void)
3223{
3224 unw_cursor_t cursor;
3225 unw_context_t context;
3226 unw_word_t off;
3227 unw_proc_info_t pip;
3228 int ret, i = 0;
3229 char procname[256];
3230 const char *filename;
3231 Dl_info dlinfo;
3232
3233 pip.unwind_info = NULL;
3234 ret = unw_getcontext(&context);
3235 if (ret) {
3236 weston_log("unw_getcontext: %d\n", ret);
3237 return;
3238 }
3239
3240 ret = unw_init_local(&cursor, &context);
3241 if (ret) {
3242 weston_log("unw_init_local: %d\n", ret);
3243 return;
3244 }
3245
3246 ret = unw_step(&cursor);
3247 while (ret > 0) {
3248 ret = unw_get_proc_info(&cursor, &pip);
3249 if (ret) {
3250 weston_log("unw_get_proc_info: %d\n", ret);
3251 break;
3252 }
3253
3254 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3255 if (ret && ret != -UNW_ENOMEM) {
3256 if (ret != -UNW_EUNSPEC)
3257 weston_log("unw_get_proc_name: %d\n", ret);
3258 procname[0] = '?';
3259 procname[1] = 0;
3260 }
3261
3262 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3263 *dlinfo.dli_fname)
3264 filename = dlinfo.dli_fname;
3265 else
3266 filename = "?";
3267
3268 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3269 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3270
3271 ret = unw_step(&cursor);
3272 if (ret < 0)
3273 weston_log("unw_step: %d\n", ret);
3274 }
3275}
3276
3277#else
3278
3279static void
3280print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003281{
3282 void *buffer[32];
3283 int i, count;
3284 Dl_info info;
3285
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003286 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3287 for (i = 0; i < count; i++) {
3288 dladdr(buffer[i], &info);
3289 weston_log(" [%016lx] %s (%s)\n",
3290 (long) buffer[i],
3291 info.dli_sname ? info.dli_sname : "--",
3292 info.dli_fname);
3293 }
3294}
3295
3296#endif
3297
3298static void
3299on_segv_signal(int s, siginfo_t *siginfo, void *context)
3300{
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003301 /* This SIGSEGV handler will do a best-effort backtrace, and
3302 * then call the backend restore function, which will switch
3303 * back to the vt we launched from or ungrab X etc and then
3304 * raise SIGTRAP. If we run weston under gdb from X or a
3305 * different vt, and tell gdb "handle SIGSEGV nostop", this
3306 * will allow weston to switch back to gdb on crash and then
3307 * gdb will catch the crash with SIGTRAP. */
3308
Martin Minarik6d118362012-06-07 18:01:59 +02003309 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003310
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003311 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003312
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003313 segv_compositor->restore(segv_compositor);
3314
3315 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003316}
3317
3318
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003319static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003320load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003321{
3322 char path[PATH_MAX];
3323 void *module, *init;
3324
3325 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003326 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003327 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003328 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003329
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003330 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3331 if (module) {
3332 weston_log("Module '%s' already loaded\n", path);
3333 dlclose(module);
3334 return NULL;
3335 }
3336
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003337 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003338 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003339 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003340 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003341 return NULL;
3342 }
3343
3344 init = dlsym(module, entrypoint);
3345 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003346 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003347 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003348 return NULL;
3349 }
3350
3351 return init;
3352}
3353
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003354static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003355load_modules(struct weston_compositor *ec, const char *modules,
3356 int *argc, char *argv[], const char *config_file)
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003357{
3358 const char *p, *end;
3359 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003360 int (*module_init)(struct weston_compositor *ec,
3361 int *argc, char *argv[], const char *config_file);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003362
3363 if (modules == NULL)
3364 return 0;
3365
3366 p = modules;
3367 while (*p) {
3368 end = strchrnul(p, ',');
3369 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
3370 module_init = load_module(buffer, "module_init");
3371 if (module_init)
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003372 module_init(ec, argc, argv, config_file);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003373 p = end;
3374 while (*p == ',')
3375 p++;
3376
3377 }
3378
3379 return 0;
3380}
3381
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003382static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003383 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3384
3385static const char xdg_wrong_message[] =
3386 "fatal: environment variable XDG_RUNTIME_DIR\n"
3387 "is set to \"%s\", which is not a directory.\n";
3388
3389static const char xdg_wrong_mode_message[] =
3390 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3391 "correctly. Unix access mode must be 0700 but is %o,\n"
3392 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003393 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003394
3395static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003396 "Refer to your distribution on how to get it, or\n"
3397 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3398 "on how to implement it.\n";
3399
Martin Minarik37032f82012-06-18 20:15:18 +02003400static void
3401verify_xdg_runtime_dir(void)
3402{
3403 char *dir = getenv("XDG_RUNTIME_DIR");
3404 struct stat s;
3405
3406 if (!dir) {
3407 weston_log(xdg_error_message);
3408 weston_log_continue(xdg_detail_message);
3409 exit(EXIT_FAILURE);
3410 }
3411
3412 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3413 weston_log(xdg_wrong_message, dir);
3414 weston_log_continue(xdg_detail_message);
3415 exit(EXIT_FAILURE);
3416 }
3417
3418 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3419 weston_log(xdg_wrong_mode_message,
3420 dir, s.st_mode & 0777, s.st_uid);
3421 weston_log_continue(xdg_detail_message);
3422 }
3423}
3424
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003425static int
3426usage(int error_code)
3427{
3428 fprintf(stderr,
3429 "Usage: weston [OPTIONS]\n\n"
3430 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3431 "Weston supports multiple backends, and depending on which backend is in use\n"
3432 "different options will be accepted.\n\n"
3433
3434
3435 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003436 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003437 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3438 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3439 " -S, --socket=NAME\tName of socket to listen on\n"
3440 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003441 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003442 " --log==FILE\t\tLog to the given file\n"
3443 " -h, --help\t\tThis help message\n\n");
3444
3445 fprintf(stderr,
3446 "Options for drm-backend.so:\n\n"
3447 " --connector=ID\tBring up only this connector\n"
3448 " --seat=SEAT\t\tThe seat that weston should run on\n"
3449 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003450 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003451 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3452
3453 fprintf(stderr,
3454 "Options for x11-backend.so:\n\n"
3455 " --width=WIDTH\t\tWidth of X window\n"
3456 " --height=HEIGHT\tHeight of X window\n"
3457 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003458 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003459 " --output-count=COUNT\tCreate multiple outputs\n"
3460 " --no-input\t\tDont create input devices\n\n");
3461
3462 fprintf(stderr,
3463 "Options for wayland-backend.so:\n\n"
3464 " --width=WIDTH\t\tWidth of Wayland surface\n"
3465 " --height=HEIGHT\tHeight of Wayland surface\n"
3466 " --display=DISPLAY\tWayland display to connect to\n\n");
3467
3468 exit(error_code);
3469}
3470
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003471int main(int argc, char *argv[])
3472{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003473 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003474 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003475 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003476 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003477 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003478 struct sigaction segv_action;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003479 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003480 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003481 int *argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003482 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003483 char *backend = NULL;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003484 const char *modules = "desktop-shell.so", *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003485 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003486 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003487 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003488 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003489 int32_t version = 0;
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003490 char *config_file;
3491
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003492 const struct config_key core_config_keys[] = {
3493 { "modules", CONFIG_KEY_STRING, &modules },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003494 };
3495
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003496 const struct config_section cs[] = {
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003497 { "core",
3498 core_config_keys, ARRAY_LENGTH(core_config_keys) },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003499 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003500
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003501 const struct weston_option core_options[] = {
3502 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3503 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3504 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003505 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003506 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003507 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003508 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003509 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003510
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003511 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003512
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003513 if (help)
3514 usage(EXIT_SUCCESS);
3515
Scott Moreau12245142012-08-29 15:15:58 -06003516 if (version) {
3517 printf(PACKAGE_STRING "\n");
3518 return EXIT_SUCCESS;
3519 }
3520
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003521 weston_log_file_open(log);
3522
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003523 weston_log("%s\n"
3524 STAMP_SPACE "%s\n"
3525 STAMP_SPACE "Bug reports to: %s\n"
3526 STAMP_SPACE "Build: %s\n",
3527 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003528 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003529 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003530
Martin Minarik37032f82012-06-18 20:15:18 +02003531 verify_xdg_runtime_dir();
3532
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003533 display = wl_display_create();
3534
Tiago Vignatti2116b892011-08-08 05:52:59 -07003535 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003536 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3537 display);
3538 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3539 display);
3540 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3541 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003542
3543 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003544 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3545 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003546
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003547 if (!backend) {
3548 if (getenv("WAYLAND_DISPLAY"))
3549 backend = "wayland-backend.so";
3550 else if (getenv("DISPLAY"))
3551 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003552 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003553 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003554 }
3555
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003556 config_file = config_file_path("weston.ini");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003557 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003558
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003559 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003560 if (!backend_init)
3561 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003562
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003563 ec = backend_init(display, &argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003564 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003565 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003566 exit(EXIT_FAILURE);
3567 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003568
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003569 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3570 segv_action.sa_sigaction = on_segv_signal;
3571 sigemptyset(&segv_action.sa_mask);
3572 sigaction(SIGSEGV, &segv_action, NULL);
3573 segv_compositor = ec;
3574
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003575 ec->option_idle_time = idle_time;
3576 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003577
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003578 setenv("WAYLAND_DISPLAY", socket_name, 1);
3579
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003580 if (load_modules(ec, modules, &argc, argv, config_file) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003581 goto out;
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003582 if (load_modules(ec, option_modules, &argc, argv, config_file) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003583 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003584
Kristian Høgsberg142d5462013-02-21 21:10:36 -05003585 free(config_file);
3586
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003587 for (i = 1; i < argc; i++)
3588 weston_log("fatal: unhandled option: %s\n", argv[i]);
3589 if (argc > 1) {
3590 ret = EXIT_FAILURE;
3591 goto out;
3592 }
3593
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003594 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003595 weston_log("fatal: failed to add socket: %m\n");
3596 ret = EXIT_FAILURE;
3597 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003598 }
3599
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003600 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003601
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003602 wl_display_run(display);
3603
3604 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003605 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05003606 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003607
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003608 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003609
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003610 for (i = ARRAY_LENGTH(signals); i;)
3611 wl_event_source_remove(signals[--i]);
3612
Daniel Stone855028f2012-05-01 20:37:10 +01003613 weston_compositor_xkb_destroy(ec);
3614
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003615 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003616 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003617
Martin Minarik19e6f262012-06-07 13:08:46 +02003618 weston_log_file_close();
3619
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003620 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003621}