blob: 5c19d92fb89e1daa95d53352592c1fd48df35ad3 [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øgsberg5ee1a602008-12-11 23:18:45 -050061
Kristian Høgsberg27da5382011-06-21 17:32:25 -040062static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040063static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040064
65static int
66sigchld_handler(int signal_number, void *data)
67{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050068 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040069 int status;
70 pid_t pid;
71
Bill Spitzak027b9622012-03-17 15:22:03 -070072 pid = waitpid(-1, &status, WNOHANG);
73 if (!pid)
74 return 1;
75
Kristian Høgsberg27da5382011-06-21 17:32:25 -040076 wl_list_for_each(p, &child_process_list, link) {
77 if (p->pid == pid)
78 break;
79 }
80
81 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020082 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040083 return 1;
84 }
85
86 wl_list_remove(&p->link);
87 p->cleanup(p, status);
88
89 return 1;
90}
91
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020092static void
93weston_output_transform_init(struct weston_output *output, uint32_t transform);
94
Alex Wu2dda6042012-04-17 17:20:47 +080095WL_EXPORT int
96weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode)
97{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020098 struct weston_seat *seat;
99 pixman_region32_t old_output_region;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200100 int ret;
101
Alex Wu2dda6042012-04-17 17:20:47 +0800102 if (!output->switch_mode)
103 return -1;
104
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200105 ret = output->switch_mode(output, mode);
106 if (ret < 0)
107 return ret;
108
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200109 pixman_region32_init(&old_output_region);
110 pixman_region32_copy(&old_output_region, &output->region);
111
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200112 /* Update output region and transformation matrix */
113 weston_output_transform_init(output, output->transform);
114
115 pixman_region32_init(&output->previous_damage);
116 pixman_region32_init_rect(&output->region, output->x, output->y,
117 output->width, output->height);
118
119 weston_output_update_matrix(output);
120
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200121 /* If a pointer falls outside the outputs new geometry, move it to its
122 * lower-right corner */
123 wl_list_for_each(seat, &output->compositor->seat_list, link) {
124 struct wl_pointer *pointer = seat->seat.pointer;
125 int32_t x, y;
126
127 if (!pointer)
128 continue;
129
130 x = wl_fixed_to_int(pointer->x);
131 y = wl_fixed_to_int(pointer->y);
132
133 if (!pixman_region32_contains_point(&old_output_region,
134 x, y, NULL) ||
135 pixman_region32_contains_point(&output->region,
136 x, y, NULL))
137 continue;
138
139 if (x >= output->x + output->width)
140 x = output->x + output->width - 1;
141 if (y >= output->y + output->height)
142 y = output->y + output->height - 1;
143
144 pointer->x = wl_fixed_from_int(x);
145 pointer->y = wl_fixed_from_int(y);
146 }
147
148 pixman_region32_fini(&old_output_region);
149
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200150 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800151}
152
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400153WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500154weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400155{
156 wl_list_insert(&child_process_list, &process->link);
157}
158
Benjamin Franzke06286262011-05-06 19:12:33 +0200159static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200160child_client_exec(int sockfd, const char *path)
161{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500162 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200163 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200164 sigset_t allsigs;
165
166 /* do not give our signal mask to the new process */
167 sigfillset(&allsigs);
168 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200169
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500170 /* Launch clients as the user. */
171 seteuid(getuid());
172
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500173 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
174 * non-CLOEXEC fd to pass through exec. */
175 clientfd = dup(sockfd);
176 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200177 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500178 return;
179 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200180
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500181 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200182 setenv("WAYLAND_SOCKET", s, 1);
183
184 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200185 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200186 path);
187}
188
189WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500190weston_client_launch(struct weston_compositor *compositor,
191 struct weston_process *proc,
192 const char *path,
193 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200194{
195 int sv[2];
196 pid_t pid;
197 struct wl_client *client;
198
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300199 weston_log("launching '%s'\n", path);
200
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300201 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200202 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200203 "socketpair failed while launching '%s': %m\n",
204 path);
205 return NULL;
206 }
207
208 pid = fork();
209 if (pid == -1) {
210 close(sv[0]);
211 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200212 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200213 "fork failed while launching '%s': %m\n", path);
214 return NULL;
215 }
216
217 if (pid == 0) {
218 child_client_exec(sv[1], path);
219 exit(-1);
220 }
221
222 close(sv[1]);
223
224 client = wl_client_create(compositor->wl_display, sv[0]);
225 if (!client) {
226 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200227 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200228 "wl_client_create failed while launching '%s'.\n",
229 path);
230 return NULL;
231 }
232
233 proc->pid = pid;
234 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500235 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200236
237 return client;
238}
239
240static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300241surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
242{
243 struct weston_surface *surface =
244 container_of(listener, struct weston_surface,
245 pending.buffer_destroy_listener);
246
247 surface->pending.buffer = NULL;
248}
249
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200250static void
251empty_region(pixman_region32_t *region)
252{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300253 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200254 pixman_region32_init(region);
255}
256
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300257static void
258region_init_infinite(pixman_region32_t *region)
259{
260 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
261 UINT32_MAX, UINT32_MAX);
262}
263
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500264WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500265weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500266{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500267 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400268
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200269 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400270 if (surface == NULL)
271 return NULL;
272
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400273 wl_signal_init(&surface->surface.resource.destroy_signal);
Kristian Høgsberg48891542012-02-23 17:38:33 -0500274
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500275 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500276 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500277
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400278 surface->surface.resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400279
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500280 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400281 surface->alpha = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400282
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100283 if (compositor->renderer->create_surface(surface) < 0) {
284 free(surface);
285 return NULL;
286 }
287
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200288 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
289 surface->pending.buffer_transform = surface->buffer_transform;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400290 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400291 surface->plane = &compositor->primary_plane;
Benjamin Franzke06286262011-05-06 19:12:33 +0200292
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400293 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500294 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500295 pixman_region32_init(&surface->clip);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300296 region_init_infinite(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200297 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500298 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400299
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200300 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200301 wl_list_insert(&surface->geometry.transformation_list,
302 &surface->transform.position.link);
303 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200304 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200305 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500306
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300307 surface->pending.buffer_destroy_listener.notify =
308 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300309 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300310 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300311 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300312 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300313
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400314 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500315}
316
Alex Wu8811bf92012-02-28 18:07:54 +0800317WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500318weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200319 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500320{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100321 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500322}
323
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400324WL_EXPORT void
325weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200326 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200327{
328 if (surface->transform.enabled) {
329 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
330
331 weston_matrix_transform(&surface->transform.matrix, &v);
332
333 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200334 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700335 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200336 v.f[3]);
337 *x = 0;
338 *y = 0;
339 return;
340 }
341
342 *x = v.f[0] / v.f[3];
343 *y = v.f[1] / v.f[3];
344 } else {
345 *x = sx + surface->geometry.x;
346 *y = sy + surface->geometry.y;
347 }
348}
349
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500350WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200351weston_surface_to_buffer_float(struct weston_surface *surface,
352 float sx, float sy, float *bx, float *by)
353{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200354 weston_transformed_coord(surface->geometry.width,
355 surface->geometry.height,
356 surface->buffer_transform,
357 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200358}
359
360WL_EXPORT pixman_box32_t
361weston_surface_to_buffer_rect(struct weston_surface *surface,
362 pixman_box32_t rect)
363{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200364 return weston_transformed_rect(surface->geometry.width,
365 surface->geometry.height,
366 surface->buffer_transform, rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200367}
368
369WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400370weston_surface_move_to_plane(struct weston_surface *surface,
371 struct weston_plane *plane)
372{
373 if (surface->plane == plane)
374 return;
375
376 weston_surface_damage_below(surface);
377 surface->plane = plane;
378 weston_surface_damage(surface);
379}
380
381WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500382weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200383{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500384 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200385
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500386 pixman_region32_init(&damage);
387 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
388 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400389 pixman_region32_union(&surface->plane->damage,
390 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500391 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200392}
393
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400394static struct wl_resource *
395find_resource_for_client(struct wl_list *list, struct wl_client *client)
396{
397 struct wl_resource *r;
398
399 wl_list_for_each(r, list, link) {
400 if (r->client == client)
401 return r;
402 }
403
404 return NULL;
405}
406
407static void
408weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
409{
410 uint32_t different = es->output_mask ^ mask;
411 uint32_t entered = mask & different;
412 uint32_t left = es->output_mask & different;
413 struct weston_output *output;
414 struct wl_resource *resource = NULL;
415 struct wl_client *client = es->surface.resource.client;
416
417 es->output_mask = mask;
418 if (es->surface.resource.client == NULL)
419 return;
420 if (different == 0)
421 return;
422
423 wl_list_for_each(output, &es->compositor->output_list, link) {
424 if (1 << output->id & different)
425 resource =
426 find_resource_for_client(&output->resource_list,
427 client);
428 if (resource == NULL)
429 continue;
430 if (1 << output->id & entered)
431 wl_surface_send_enter(&es->surface.resource, resource);
432 if (1 << output->id & left)
433 wl_surface_send_leave(&es->surface.resource, resource);
434 }
435}
436
437static void
438weston_surface_assign_output(struct weston_surface *es)
439{
440 struct weston_compositor *ec = es->compositor;
441 struct weston_output *output, *new_output;
442 pixman_region32_t region;
443 uint32_t max, area, mask;
444 pixman_box32_t *e;
445
446 new_output = NULL;
447 max = 0;
448 mask = 0;
449 pixman_region32_init(&region);
450 wl_list_for_each(output, &ec->output_list, link) {
451 pixman_region32_intersect(&region, &es->transform.boundingbox,
452 &output->region);
453
454 e = pixman_region32_extents(&region);
455 area = (e->x2 - e->x1) * (e->y2 - e->y1);
456
457 if (area > 0)
458 mask |= 1 << output->id;
459
460 if (area >= max) {
461 new_output = output;
462 max = area;
463 }
464 }
465 pixman_region32_fini(&region);
466
467 es->output = new_output;
468 weston_surface_update_output_mask(es, mask);
469}
470
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200471static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200472surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
473 int32_t width, int32_t height,
474 pixman_region32_t *bbox)
475{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200476 float min_x = HUGE_VALF, min_y = HUGE_VALF;
477 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200478 int32_t s[4][2] = {
479 { sx, sy },
480 { sx, sy + height },
481 { sx + width, sy },
482 { sx + width, sy + height }
483 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200484 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200485 int i;
486
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300487 if (width == 0 || height == 0) {
488 /* avoid rounding empty bbox to 1x1 */
489 pixman_region32_init(bbox);
490 return;
491 }
492
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200493 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200494 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400495 weston_surface_to_global_float(surface,
496 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200497 if (x < min_x)
498 min_x = x;
499 if (x > max_x)
500 max_x = x;
501 if (y < min_y)
502 min_y = y;
503 if (y > max_y)
504 max_y = y;
505 }
506
Pekka Paalanen219b9822012-02-08 15:38:37 +0200507 int_x = floorf(min_x);
508 int_y = floorf(min_y);
509 pixman_region32_init_rect(bbox, int_x, int_y,
510 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200511}
512
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200513static void
514weston_surface_update_transform_disable(struct weston_surface *surface)
515{
516 surface->transform.enabled = 0;
517
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200518 /* round off fractions when not transformed */
519 surface->geometry.x = roundf(surface->geometry.x);
520 surface->geometry.y = roundf(surface->geometry.y);
521
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200522 pixman_region32_init_rect(&surface->transform.boundingbox,
523 surface->geometry.x,
524 surface->geometry.y,
525 surface->geometry.width,
526 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500527
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400528 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500529 pixman_region32_copy(&surface->transform.opaque,
530 &surface->opaque);
531 pixman_region32_translate(&surface->transform.opaque,
532 surface->geometry.x,
533 surface->geometry.y);
534 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200535}
536
537static int
538weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200539{
540 struct weston_matrix *matrix = &surface->transform.matrix;
541 struct weston_matrix *inverse = &surface->transform.inverse;
542 struct weston_transform *tform;
543
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200544 surface->transform.enabled = 1;
545
546 /* Otherwise identity matrix, but with x and y translation. */
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300547 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200548 surface->transform.position.matrix.d[12] = surface->geometry.x;
549 surface->transform.position.matrix.d[13] = surface->geometry.y;
550
551 weston_matrix_init(matrix);
552 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
553 weston_matrix_multiply(matrix, &tform->matrix);
554
555 if (weston_matrix_invert(inverse, matrix) < 0) {
556 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200557 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200558 " transformation not invertible.\n", surface);
559 return -1;
560 }
561
562 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
563 surface->geometry.height,
564 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500565
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200566 return 0;
567}
568
569WL_EXPORT void
570weston_surface_update_transform(struct weston_surface *surface)
571{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200572 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200573 return;
574
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200575 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200576
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500577 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200578
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200579 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500580 pixman_region32_fini(&surface->transform.opaque);
581 pixman_region32_init(&surface->transform.opaque);
582
Pekka Paalanencd403622012-01-25 13:37:39 +0200583 /* transform.position is always in transformation_list */
584 if (surface->geometry.transformation_list.next ==
585 &surface->transform.position.link &&
586 surface->geometry.transformation_list.prev ==
587 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200588 weston_surface_update_transform_disable(surface);
589 } else {
590 if (weston_surface_update_transform_enable(surface) < 0)
591 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200592 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200593
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400594 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200595
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300596 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200597}
598
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200599WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100600weston_surface_to_global_fixed(struct weston_surface *surface,
601 wl_fixed_t sx, wl_fixed_t sy,
602 wl_fixed_t *x, wl_fixed_t *y)
603{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200604 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100605
606 weston_surface_to_global_float(surface,
607 wl_fixed_to_double(sx),
608 wl_fixed_to_double(sy),
609 &xf, &yf);
610 *x = wl_fixed_from_double(xf);
611 *y = wl_fixed_from_double(yf);
612}
613
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400614WL_EXPORT void
615weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200616 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200617{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200618 if (surface->transform.enabled) {
619 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
620
621 weston_matrix_transform(&surface->transform.inverse, &v);
622
623 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200624 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200625 "weston_surface_from_global(), divisor = %g\n",
626 v.f[3]);
627 *sx = 0;
628 *sy = 0;
629 return;
630 }
631
Pekka Paalanencd403622012-01-25 13:37:39 +0200632 *sx = v.f[0] / v.f[3];
633 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200634 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200635 *sx = x - surface->geometry.x;
636 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200637 }
638}
639
640WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100641weston_surface_from_global_fixed(struct weston_surface *surface,
642 wl_fixed_t x, wl_fixed_t y,
643 wl_fixed_t *sx, wl_fixed_t *sy)
644{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200645 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100646
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400647 weston_surface_from_global_float(surface,
648 wl_fixed_to_double(x),
649 wl_fixed_to_double(y),
650 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100651 *sx = wl_fixed_from_double(sxf);
652 *sy = wl_fixed_from_double(syf);
653}
654
655WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200656weston_surface_from_global(struct weston_surface *surface,
657 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
658{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200659 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200660
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400661 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200662 *sx = floorf(sxf);
663 *sy = floorf(syf);
664}
665
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400666WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400667weston_surface_schedule_repaint(struct weston_surface *surface)
668{
669 struct weston_output *output;
670
671 wl_list_for_each(output, &surface->compositor->output_list, link)
672 if (surface->output_mask & (1 << output->id))
673 weston_output_schedule_repaint(output);
674}
675
676WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500677weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500678{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400679 pixman_region32_union_rect(&surface->damage, &surface->damage,
680 0, 0, surface->geometry.width,
681 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200682
Kristian Høgsberg98238702012-08-03 16:29:12 -0400683 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500684}
685
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400686WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500687weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200688 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400689{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200690 surface->geometry.x = x;
691 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200692 surface->geometry.width = width;
693 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200694 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400695}
696
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200697WL_EXPORT void
698weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200699 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200700{
701 surface->geometry.x = x;
702 surface->geometry.y = y;
703 surface->geometry.dirty = 1;
704}
705
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300706WL_EXPORT int
707weston_surface_is_mapped(struct weston_surface *surface)
708{
709 if (surface->output)
710 return 1;
711 else
712 return 0;
713}
714
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200715WL_EXPORT int32_t
716weston_surface_buffer_width(struct weston_surface *surface)
717{
718 switch (surface->buffer_transform) {
719 case WL_OUTPUT_TRANSFORM_90:
720 case WL_OUTPUT_TRANSFORM_270:
721 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
722 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200723 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200724 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200725 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200726 }
727}
728
729WL_EXPORT int32_t
730weston_surface_buffer_height(struct weston_surface *surface)
731{
732 switch (surface->buffer_transform) {
733 case WL_OUTPUT_TRANSFORM_90:
734 case WL_OUTPUT_TRANSFORM_270:
735 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
736 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200737 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200738 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200739 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200740 }
741}
742
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400743WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500744weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500745{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400746 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500747
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400748 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500749
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400750 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500751}
752
Tiago Vignatti9d393522012-02-10 16:26:19 +0200753static struct weston_surface *
754weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100755 wl_fixed_t x, wl_fixed_t y,
756 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200757{
758 struct weston_surface *surface;
759
760 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100761 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500762 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100763 wl_fixed_to_int(*sx),
764 wl_fixed_to_int(*sy),
765 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200766 return surface;
767 }
768
769 return NULL;
770}
771
772static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400773weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500774{
Scott Moreau447013d2012-02-18 05:05:29 -0700775 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500776 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400777 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500778
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400779 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100780 return;
781
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400782 surface = weston_compositor_pick_surface(seat->compositor,
783 pointer->x,
784 pointer->y,
785 &pointer->current_x,
786 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500787
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400788 if (&surface->surface != pointer->current) {
789 interface = pointer->grab->interface;
790 pointer->current = &surface->surface;
791 interface->focus(pointer->grab, &surface->surface,
792 pointer->current_x,
793 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500794 }
795
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400796 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500797 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100798 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400799 pointer->x,
800 pointer->y,
801 &pointer->grab->x,
802 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500803}
804
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400805static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500806weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400807{
Daniel Stone37816df2012-05-16 18:45:18 +0100808 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400809
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500810 if (!compositor->focus)
811 return;
812
Daniel Stone37816df2012-05-16 18:45:18 +0100813 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400814 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400815}
816
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400817WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500818weston_surface_unmap(struct weston_surface *surface)
819{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100820 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500821
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500822 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500823 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500824 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500825
Daniel Stone4dab5db2012-05-30 16:31:53 +0100826 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100827 if (seat->seat.keyboard &&
828 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100829 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100830 if (seat->seat.pointer &&
831 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100832 wl_pointer_set_focus(seat->seat.pointer,
833 NULL,
834 wl_fixed_from_int(0),
835 wl_fixed_from_int(0));
836 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500837
Kristian Høgsberg98238702012-08-03 16:29:12 -0400838 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500839}
840
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400841struct weston_frame_callback {
842 struct wl_resource resource;
843 struct wl_list link;
844};
845
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500846static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400847destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500848{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500849 struct weston_surface *surface =
850 container_of(resource,
851 struct weston_surface, surface.resource);
852 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400853 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400854
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300855 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500856 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400857
Pekka Paalanenbc106382012-10-10 12:49:31 +0300858 wl_list_for_each_safe(cb, next,
859 &surface->pending.frame_callback_list, link)
860 wl_resource_destroy(&cb->resource);
861
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300862 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300863 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +0300864 pixman_region32_fini(&surface->pending.damage);
865
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300866 if (surface->pending.buffer)
867 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
868
Pekka Paalanende685b82012-12-04 15:58:12 +0200869 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400870
Kristian Høgsberg42263852012-09-06 21:59:29 -0400871 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200872
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200873 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200874 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500875 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500876 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300877 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200878
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400879 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
880 wl_resource_destroy(&cb->resource);
881
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400882 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500883}
884
Alex Wu8811bf92012-02-28 18:07:54 +0800885WL_EXPORT void
886weston_surface_destroy(struct weston_surface *surface)
887{
888 /* Not a valid way to destroy a client surface */
889 assert(surface->surface.resource.client == NULL);
890
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400891 wl_signal_emit(&surface->surface.resource.destroy_signal,
892 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800893 destroy_surface(&surface->surface.resource);
894}
895
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100896static void
Pekka Paalanende685b82012-12-04 15:58:12 +0200897weston_buffer_reference_handle_destroy(struct wl_listener *listener,
898 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100899{
Pekka Paalanende685b82012-12-04 15:58:12 +0200900 struct weston_buffer_reference *ref =
901 container_of(listener, struct weston_buffer_reference,
902 destroy_listener);
903
904 assert((struct wl_buffer *)data == ref->buffer);
905 ref->buffer = NULL;
906}
907
908WL_EXPORT void
909weston_buffer_reference(struct weston_buffer_reference *ref,
910 struct wl_buffer *buffer)
911{
912 if (ref->buffer && buffer != ref->buffer) {
913 weston_buffer_post_release(ref->buffer);
914 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300915 }
916
Pekka Paalanende685b82012-12-04 15:58:12 +0200917 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400918 buffer->busy_count++;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300919 wl_signal_add(&buffer->resource.destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +0200920 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +0200921 }
922
Pekka Paalanende685b82012-12-04 15:58:12 +0200923 ref->buffer = buffer;
924 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
925}
926
927static void
928weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
929{
930 weston_buffer_reference(&surface->buffer_ref, buffer);
931
Pekka Paalanena6421c42012-12-04 15:58:10 +0200932 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300933 if (weston_surface_is_mapped(surface))
934 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300935 }
936
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300937 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100938}
939
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500940WL_EXPORT void
941weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400942{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500943 wl_list_remove(&surface->layer_link);
944 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500945 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500946 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400947}
948
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400949WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500950weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400951{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500952 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400953
954 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500955 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400956}
957
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500958WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500959weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +0200960{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400961 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200962 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200963
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400964 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -0500965 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200966}
967
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400968WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500969weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400970{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500971 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400972
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400973 pixman_region32_union(&compositor->primary_plane.damage,
974 &compositor->primary_plane.damage,
975 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400976 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400977}
978
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400979static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500980fade_frame(struct weston_animation *animation,
981 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400982{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500983 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400984 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500985 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500986 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400987
Scott Moreau94b0b0c2012-06-14 01:01:56 -0600988 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -0600989 compositor->fade.spring.timestamp = msecs;
990
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500991 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500992 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500993 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
994 compositor->fade.spring.current);
995 weston_surface_damage(surface);
996
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500997 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400998 compositor->fade.spring.current =
999 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001000 wl_list_remove(&animation->link);
1001 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001002
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001003 if (compositor->fade.spring.current < 0.001) {
1004 destroy_surface(&surface->surface.resource);
1005 compositor->fade.surface = NULL;
1006 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001007 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001008 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001009 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001010 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001011}
1012
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001013static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001014surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001015 pixman_region32_t *opaque)
1016{
Pekka Paalanende685b82012-12-04 15:58:12 +02001017 if (surface->buffer_ref.buffer &&
1018 wl_buffer_is_shm(surface->buffer_ref.buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001019 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001020
1021 if (surface->transform.enabled) {
1022 pixman_box32_t *extents;
1023
1024 extents = pixman_region32_extents(&surface->damage);
1025 surface_compute_bbox(surface, extents->x1, extents->y1,
1026 extents->x2 - extents->x1,
1027 extents->y2 - extents->y1,
1028 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001029 pixman_region32_translate(&surface->damage,
1030 -surface->plane->x,
1031 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001032 } else {
1033 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001034 surface->geometry.x - surface->plane->x,
1035 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001036 }
1037
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001038 pixman_region32_subtract(&surface->damage,
1039 &surface->damage, &surface->plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001040 pixman_region32_union(&surface->plane->damage,
1041 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001042 empty_region(&surface->damage);
1043 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001044 pixman_region32_union(&surface->plane->opaque,
1045 &surface->plane->opaque,
1046 &surface->transform.opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001047 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001048}
1049
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001050static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001051weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001052{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001053 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001054 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001055 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001056 struct weston_animation *animation, *next;
1057 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001058 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001059 pixman_region32_t opaque, output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001060
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001061 weston_compositor_update_drag_surfaces(ec);
1062
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001063 /* Rebuild the surface list and update surface transforms up front. */
1064 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +02001065 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001066 wl_list_for_each(layer, &ec->layer_list, link) {
1067 wl_list_for_each(es, &layer->surface_list, layer_link) {
1068 weston_surface_update_transform(es);
1069 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +02001070 if (es->output == output) {
1071 wl_list_insert_list(&frame_callback_list,
1072 &es->frame_callback_list);
1073 wl_list_init(&es->frame_callback_list);
1074 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001075 }
1076 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001077
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001078 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001079 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001080 else
1081 wl_list_for_each(es, &ec->surface_list, link)
1082 weston_surface_move_to_plane(es, &ec->primary_plane);
1083
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001084 pixman_region32_init(&opaque);
1085
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001086 pixman_region32_fini(&ec->primary_plane.opaque);
1087 pixman_region32_init(&ec->primary_plane.opaque);
1088
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001089 wl_list_for_each(es, &ec->surface_list, link) {
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001090 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001091
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001092 /* Both the renderer and the backend have seen the buffer
1093 * by now. If renderer needs the buffer, it has its own
1094 * reference set. If the backend wants to keep the buffer
1095 * around for migrating the surface into a non-primary plane
1096 * later, keep_buffer is true. Otherwise, drop the core
1097 * reference now, and allow early buffer release. This enables
1098 * clients to use single-buffering.
1099 */
1100 if (!es->keep_buffer)
1101 weston_buffer_reference(&es->buffer_ref, NULL);
1102 }
1103
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -04001104 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001105
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001106 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001107 pixman_region32_intersect(&output_damage,
1108 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001109
Scott Moreauccbf29d2012-02-22 14:21:41 -07001110 if (output->dirty)
1111 weston_output_update_matrix(output);
1112
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001113 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001114
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001115 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001116
Kristian Høgsbergef044142011-06-21 15:02:12 -04001117 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001118
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001119 weston_compositor_repick(ec);
1120 wl_event_loop_dispatch(ec->input_loop, 0);
1121
Jonas Ådahldb773762012-06-13 00:01:21 +02001122 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001123 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001124 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001125 }
1126
Scott Moreaud64cf212012-06-08 19:40:54 -06001127 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001128 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001129 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001130 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001131}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001132
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001133static int
1134weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001135{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001136 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001137
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001138 wl_event_loop_dispatch(compositor->input_loop, 0);
1139
1140 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001141}
1142
1143WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001144weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001145{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001146 struct weston_compositor *compositor = output->compositor;
1147 struct wl_event_loop *loop =
1148 wl_display_get_event_loop(compositor->wl_display);
1149 int fd;
1150
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001151 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001152 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001153 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001154 return;
1155 }
1156
1157 output->repaint_scheduled = 0;
1158 if (compositor->input_loop_source)
1159 return;
1160
1161 fd = wl_event_loop_get_fd(compositor->input_loop);
1162 compositor->input_loop_source =
1163 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1164 weston_compositor_read_input, compositor);
1165}
1166
1167static void
1168idle_repaint(void *data)
1169{
1170 struct weston_output *output = data;
1171
1172 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001173}
1174
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001175WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001176weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1177{
1178 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001179 if (below != NULL)
1180 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001181}
1182
1183WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001184weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001185{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001186 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001187 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001188
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001189 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001190 return;
1191
Kristian Høgsbergef044142011-06-21 15:02:12 -04001192 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001193 output->repaint_needed = 1;
1194 if (output->repaint_scheduled)
1195 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001196
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001197 wl_event_loop_add_idle(loop, idle_repaint, output);
1198 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001199
1200 if (compositor->input_loop_source) {
1201 wl_event_source_remove(compositor->input_loop_source);
1202 compositor->input_loop_source = NULL;
1203 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001204}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001205
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001206WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001207weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1208{
1209 struct weston_output *output;
1210
1211 wl_list_for_each(output, &compositor->output_list, link)
1212 weston_output_schedule_repaint(output);
1213}
1214
1215WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001216weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001217{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001218 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001219 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001220
Scott Moreau9d1b1122012-06-08 19:40:53 -06001221 output = container_of(compositor->output_list.next,
1222 struct weston_output, link);
1223
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001224 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001225 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001226 return;
1227
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001228 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001229 surface = weston_surface_create(compositor);
John Kåre Alsakere2e3d072012-10-12 12:25:09 +02001230 if (!surface)
1231 return;
1232
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001233 weston_surface_configure(surface, 0, 0, 8192, 8192);
Ander Conselvan de Oliveira4c65da52013-02-13 16:06:37 +02001234 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0 - tint);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001235 wl_list_insert(&compositor->fade_layer.surface_list,
1236 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03001237 weston_surface_update_transform(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001238 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001239 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001240 }
1241
1242 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001243 if (wl_list_empty(&compositor->fade.animation.link)) {
1244 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001245 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001246 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001247 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001248}
1249
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001250static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001251surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001252{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001253 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001254}
1255
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001256static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001257surface_attach(struct wl_client *client,
1258 struct wl_resource *resource,
1259 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1260{
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001261 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001262 struct wl_buffer *buffer = NULL;
1263
1264 if (buffer_resource)
1265 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001266
Pekka Paalanende685b82012-12-04 15:58:12 +02001267 /* Attach, attach, without commit in between does not send
1268 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001269 if (surface->pending.buffer)
1270 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001271
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001272 surface->pending.sx = sx;
1273 surface->pending.sy = sy;
1274 surface->pending.buffer = buffer;
1275 if (buffer) {
1276 wl_signal_add(&buffer->resource.destroy_signal,
1277 &surface->pending.buffer_destroy_listener);
1278 surface->pending.remove_contents = 0;
1279 } else {
1280 surface->pending.remove_contents = 1;
1281 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001282}
1283
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001284static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001285surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001286 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001287 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001288{
Pekka Paalanen8e159182012-10-10 12:49:25 +03001289 struct weston_surface *surface = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001290
Pekka Paalanen8e159182012-10-10 12:49:25 +03001291 pixman_region32_union_rect(&surface->pending.damage,
1292 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001293 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001294}
1295
Kristian Høgsberg33418202011-08-16 23:01:28 -04001296static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001297destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001298{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001299 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001300
1301 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001302 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001303}
1304
1305static void
1306surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001307 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001308{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001309 struct weston_frame_callback *cb;
Pekka Paalanenbc106382012-10-10 12:49:31 +03001310 struct weston_surface *surface = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001311
1312 cb = malloc(sizeof *cb);
1313 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001314 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001315 return;
1316 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001317
Kristian Høgsberg33418202011-08-16 23:01:28 -04001318 cb->resource.object.interface = &wl_callback_interface;
1319 cb->resource.object.id = callback;
1320 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001321 cb->resource.client = client;
1322 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001323
1324 wl_client_add_resource(client, &cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001325 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001326}
1327
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001328static void
1329surface_set_opaque_region(struct wl_client *client,
1330 struct wl_resource *resource,
1331 struct wl_resource *region_resource)
1332{
1333 struct weston_surface *surface = resource->data;
1334 struct weston_region *region;
1335
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001336 if (region_resource) {
1337 region = region_resource->data;
Pekka Paalanen512dde82012-10-10 12:49:27 +03001338 pixman_region32_copy(&surface->pending.opaque,
1339 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001340 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001341 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001342 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001343}
1344
1345static void
1346surface_set_input_region(struct wl_client *client,
1347 struct wl_resource *resource,
1348 struct wl_resource *region_resource)
1349{
1350 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001351 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001352
1353 if (region_resource) {
1354 region = region_resource->data;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001355 pixman_region32_copy(&surface->pending.input,
1356 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001357 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001358 pixman_region32_fini(&surface->pending.input);
1359 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001360 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001361}
1362
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001363static int
1364surface_pending_buffer_has_different_size(struct weston_surface *surface)
1365{
1366 int width, height;
1367
1368 switch (surface->pending.buffer_transform) {
1369 case WL_OUTPUT_TRANSFORM_90:
1370 case WL_OUTPUT_TRANSFORM_270:
1371 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1372 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1373 height = surface->pending.buffer->width;
1374 width = surface->pending.buffer->height;
1375 break;
1376 default:
1377 width = surface->pending.buffer->width;
1378 height = surface->pending.buffer->height;
1379 }
1380
1381 if (width == surface->geometry.width &&
1382 height == surface->geometry.height)
1383 return 0;
1384 else
1385 return 1;
1386}
1387
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001388static void
1389surface_commit(struct wl_client *client, struct wl_resource *resource)
1390{
1391 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001392 pixman_region32_t opaque;
1393
1394 if (surface->pending.sx || surface->pending.sy ||
1395 (surface->pending.buffer &&
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001396 surface_pending_buffer_has_different_size(surface)))
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001397 surface->geometry.dirty = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001398
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001399 /* wl_surface.set_buffer_rotation */
1400 surface->buffer_transform = surface->pending.buffer_transform;
1401
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001402 /* wl_surface.attach */
1403 if (surface->pending.buffer || surface->pending.remove_contents)
1404 weston_surface_attach(surface, surface->pending.buffer);
1405
Pekka Paalanende685b82012-12-04 15:58:12 +02001406 if (surface->buffer_ref.buffer && surface->configure)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001407 surface->configure(surface, surface->pending.sx,
1408 surface->pending.sy);
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001409 surface->pending.sx = 0;
1410 surface->pending.sy = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001411
1412 /* wl_surface.damage */
1413 pixman_region32_union(&surface->damage, &surface->damage,
1414 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001415 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1416 0, 0,
1417 surface->geometry.width,
1418 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001419 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001420
1421 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001422 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001423 surface->geometry.width,
1424 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001425 pixman_region32_intersect(&opaque,
1426 &opaque, &surface->pending.opaque);
1427
1428 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1429 pixman_region32_copy(&surface->opaque, &opaque);
1430 surface->geometry.dirty = 1;
1431 }
1432
1433 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001434
1435 /* wl_surface.set_input_region */
1436 pixman_region32_fini(&surface->input);
1437 pixman_region32_init_rect(&surface->input, 0, 0,
1438 surface->geometry.width,
1439 surface->geometry.height);
1440 pixman_region32_intersect(&surface->input,
1441 &surface->input, &surface->pending.input);
1442
Pekka Paalanenbc106382012-10-10 12:49:31 +03001443 /* wl_surface.frame */
1444 wl_list_insert_list(&surface->frame_callback_list,
1445 &surface->pending.frame_callback_list);
1446 wl_list_init(&surface->pending.frame_callback_list);
1447
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001448 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001449}
1450
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001451static void
1452surface_set_buffer_transform(struct wl_client *client,
1453 struct wl_resource *resource, int transform)
1454{
1455 struct weston_surface *surface = resource->data;
1456
1457 surface->pending.buffer_transform = transform;
1458}
1459
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001460static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001461 surface_destroy,
1462 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001463 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001464 surface_frame,
1465 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001466 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001467 surface_commit,
1468 surface_set_buffer_transform
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001469};
1470
1471static void
1472compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001473 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001474{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001475 struct weston_compositor *ec = resource->data;
1476 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001477
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001478 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001479 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001480 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001481 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001482 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001483
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001484 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001485
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001486 surface->surface.resource.object.id = id;
1487 surface->surface.resource.object.interface = &wl_surface_interface;
1488 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001489 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001490 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001491
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001492 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001493}
1494
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001495static void
1496destroy_region(struct wl_resource *resource)
1497{
1498 struct weston_region *region =
1499 container_of(resource, struct weston_region, resource);
1500
1501 pixman_region32_fini(&region->region);
1502 free(region);
1503}
1504
1505static void
1506region_destroy(struct wl_client *client, struct wl_resource *resource)
1507{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001508 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001509}
1510
1511static void
1512region_add(struct wl_client *client, struct wl_resource *resource,
1513 int32_t x, int32_t y, int32_t width, int32_t height)
1514{
1515 struct weston_region *region = resource->data;
1516
1517 pixman_region32_union_rect(&region->region, &region->region,
1518 x, y, width, height);
1519}
1520
1521static void
1522region_subtract(struct wl_client *client, struct wl_resource *resource,
1523 int32_t x, int32_t y, int32_t width, int32_t height)
1524{
1525 struct weston_region *region = resource->data;
1526 pixman_region32_t rect;
1527
1528 pixman_region32_init_rect(&rect, x, y, width, height);
1529 pixman_region32_subtract(&region->region, &region->region, &rect);
1530 pixman_region32_fini(&rect);
1531}
1532
1533static const struct wl_region_interface region_interface = {
1534 region_destroy,
1535 region_add,
1536 region_subtract
1537};
1538
1539static void
1540compositor_create_region(struct wl_client *client,
1541 struct wl_resource *resource, uint32_t id)
1542{
1543 struct weston_region *region;
1544
1545 region = malloc(sizeof *region);
1546 if (region == NULL) {
1547 wl_resource_post_no_memory(resource);
1548 return;
1549 }
1550
1551 region->resource.destroy = destroy_region;
1552
1553 region->resource.object.id = id;
1554 region->resource.object.interface = &wl_region_interface;
1555 region->resource.object.implementation =
1556 (void (**)(void)) &region_interface;
1557 region->resource.data = region;
1558
1559 pixman_region32_init(&region->region);
1560
1561 wl_client_add_resource(client, &region->resource);
1562}
1563
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001564static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001565 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001566 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001567};
1568
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001569WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001570weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001571{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001572 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1573 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001574
1575 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001576 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001577}
1578
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001579static void
1580weston_compositor_dpms_on(struct weston_compositor *compositor)
1581{
1582 struct weston_output *output;
1583
1584 wl_list_for_each(output, &compositor->output_list, link)
1585 if (output->set_dpms)
1586 output->set_dpms(output, WESTON_DPMS_ON);
1587}
1588
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001589WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001590weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001591{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001592 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1593 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001594 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001595 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001596 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001597 }
1598}
1599
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001600static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001601weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001602{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001603 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001604 compositor->idle_inhibit++;
1605}
1606
1607static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001608weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001609{
1610 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001611 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001612}
1613
1614static int
1615idle_handler(void *data)
1616{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001617 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001618
1619 if (compositor->idle_inhibit)
1620 return 1;
1621
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001622 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001623
1624 return 1;
1625}
1626
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001627WL_EXPORT void
1628weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1629{
1630 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001631 pixman_region32_init(&plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001632 plane->x = x;
1633 plane->y = y;
1634}
1635
1636WL_EXPORT void
1637weston_plane_release(struct weston_plane *plane)
1638{
1639 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001640 pixman_region32_fini(&plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001641}
1642
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001643static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001644weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001645
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001646static void
Daniel Stone37816df2012-05-16 18:45:18 +01001647clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001648{
Daniel Stone37816df2012-05-16 18:45:18 +01001649 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001650 struct weston_output *output, *prev = NULL;
1651 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001652
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001653 x = wl_fixed_to_int(*fx);
1654 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001655 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1656 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001657
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001658 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001659 if (pixman_region32_contains_point(&output->region,
1660 x, y, NULL))
1661 valid = 1;
1662 if (pixman_region32_contains_point(&output->region,
1663 old_x, old_y, NULL))
1664 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001665 }
Daniel Stone37816df2012-05-16 18:45:18 +01001666
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001667 if (!valid) {
1668 if (x < prev->x)
1669 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001670 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001671 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001672 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001673 if (y < prev->y)
1674 *fy = wl_fixed_from_int(prev->y);
1675 else if (y >= prev->y + prev->current->height)
1676 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001677 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001678 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001679}
1680
1681WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001682notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001683{
1684 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001685 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001686 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001687 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001688 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001689
1690 weston_compositor_activity(ec);
1691
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001692 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001693
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001694 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001695
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001696 pointer->x = x;
1697 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001698
1699 ix = wl_fixed_to_int(x);
1700 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001701
Scott Moreauccbf29d2012-02-22 14:21:41 -07001702 wl_list_for_each(output, &ec->output_list, link)
1703 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001704 pixman_region32_contains_point(&output->region,
1705 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06001706 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001707
Daniel Stone37816df2012-05-16 18:45:18 +01001708 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001709 interface = pointer->grab->interface;
1710 interface->motion(pointer->grab, time,
1711 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001712
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001713 if (seat->sprite) {
1714 weston_surface_set_position(seat->sprite,
1715 ix - seat->hotspot_x,
1716 iy - seat->hotspot_y);
1717 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001718 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001719}
1720
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001721WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001722weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001723 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001724{
Daniel Stone37816df2012-05-16 18:45:18 +01001725 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001726
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001727 if (seat->seat.keyboard) {
1728 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1729 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04001730 }
1731
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001732 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001733}
1734
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001735WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001736notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001737 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001738{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001739 struct weston_compositor *compositor = seat->compositor;
1740 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001741 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001742 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001743 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001744
Daniel Stone4dbadb12012-05-30 16:31:51 +01001745 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001746 if (compositor->ping_handler && focus)
1747 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001748 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001749 if (pointer->button_count == 0) {
1750 pointer->grab_button = button;
1751 pointer->grab_time = time;
1752 pointer->grab_x = pointer->x;
1753 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001754 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001755 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001756 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001757 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001758 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001759 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001760
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001761 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001762 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001763
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001764 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001765
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001766 if (pointer->button_count == 1)
1767 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001768 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001769}
1770
Scott Moreau210d0792012-03-22 10:47:01 -06001771WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001772notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01001773 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001774{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001775 struct weston_compositor *compositor = seat->compositor;
1776 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001777 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001778 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001779 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1780
1781 if (compositor->ping_handler && focus)
1782 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001783
1784 weston_compositor_activity(compositor);
1785
Scott Moreau6a3633d2012-03-20 08:47:59 -06001786 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001787 weston_compositor_run_axis_binding(compositor, seat,
1788 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06001789 else
1790 return;
1791
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001792 if (pointer->focus_resource)
1793 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01001794 value);
Scott Moreau210d0792012-03-22 10:47:01 -06001795}
1796
Daniel Stone05d58682012-06-22 13:21:38 +01001797WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001798notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001799{
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001800 struct wl_keyboard *keyboard = &seat->keyboard.keyboard;
Daniel Stone05d58682012-06-22 13:21:38 +01001801 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01001802 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001803 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01001804 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01001805 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01001806
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001807 /* Serialize and update our internal state, checking to see if it's
1808 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01001809 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1810 XKB_STATE_DEPRESSED);
1811 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1812 XKB_STATE_LATCHED);
1813 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1814 XKB_STATE_LOCKED);
1815 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01001816 XKB_STATE_EFFECTIVE);
1817
Daniel Stone71c38772012-06-22 13:21:30 +01001818 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
1819 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
1820 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
1821 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01001822 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01001823
Daniel Stone71c38772012-06-22 13:21:30 +01001824 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
1825 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
1826 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
1827 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001828
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001829 /* And update the modifier_state for bindings. */
1830 mods_lookup = mods_depressed | mods_latched;
1831 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01001832 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001833 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01001834 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001835 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01001836 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001837 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04001838 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
1839 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01001840
Daniel Stone8c8164f2012-05-30 16:31:45 +01001841 /* Finally, notify the compositor that LEDs have changed. */
1842 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001843 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001844 leds |= LED_NUM_LOCK;
1845 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001846 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001847 leds |= LED_CAPS_LOCK;
1848 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001849 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001850 leds |= LED_SCROLL_LOCK;
1851 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01001852 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01001853 seat->xkb_state.leds = leds;
1854
Daniel Stone05d58682012-06-22 13:21:38 +01001855 if (changed) {
1856 grab->interface->modifiers(grab,
1857 serial,
1858 keyboard->modifiers.mods_depressed,
1859 keyboard->modifiers.mods_latched,
1860 keyboard->modifiers.mods_locked,
1861 keyboard->modifiers.group);
1862 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001863}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001864
Daniel Stone05d58682012-06-22 13:21:38 +01001865static void
1866update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001867 enum wl_keyboard_key_state state)
1868{
1869 enum xkb_key_direction direction;
1870
1871 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1872 direction = XKB_KEY_DOWN;
1873 else
1874 direction = XKB_KEY_UP;
1875
1876 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1877 * broken keycode system, which starts at 8. */
1878 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1879
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001880 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001881}
1882
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001883WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001884notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001885 enum wl_keyboard_key_state state,
1886 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001887{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001888 struct weston_compositor *compositor = seat->compositor;
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001889 struct weston_keyboard *keyboard = &seat->keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01001890 struct weston_surface *focus =
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001891 (struct weston_surface *) keyboard->keyboard.focus;
1892 struct wl_keyboard_grab *grab = keyboard->keyboard.grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001893 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001894 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001895
Daniel Stonec9785ea2012-05-30 16:31:52 +01001896 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001897 if (compositor->ping_handler && focus)
1898 compositor->ping_handler(focus, serial);
1899
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001900 weston_compositor_idle_inhibit(compositor);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001901 keyboard->keyboard.grab_key = key;
1902 keyboard->keyboard.grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07001903 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001904 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001905 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001906
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001907 end = keyboard->keyboard.keys.data + keyboard->keyboard.keys.size;
1908 for (k = keyboard->keyboard.keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01001909 if (*k == key) {
1910 /* Ignore server-generated repeats. */
1911 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1912 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001913 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01001914 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001915 }
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001916 keyboard->keyboard.keys.size = (void *) end - keyboard->keyboard.keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001917 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001918 k = wl_array_add(&keyboard->keyboard.keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001919 *k = key;
1920 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001921
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001922 if (grab == &keyboard->keyboard.default_grab ||
1923 grab == &keyboard->input_method_grab) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001924 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001925 state);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01001926 grab = keyboard->keyboard.grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06001927 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001928
Daniel Stone7ace3902012-05-30 16:31:40 +01001929 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01001930
1931 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001932 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01001933 wl_display_get_serial(compositor->wl_display),
1934 key,
1935 state);
1936 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001937}
1938
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001939WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001940notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01001941 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001942{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001943 struct weston_compositor *compositor = seat->compositor;
1944 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001945
1946 if (output) {
Kristian Høgsberg00fbbe62012-10-23 13:04:09 -04001947 clip_pointer_motion(seat, &x, &y);
Daniel Stone37816df2012-05-16 18:45:18 +01001948 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001949 x - pointer->x,
1950 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001951
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001952 pointer->x = x;
1953 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001954 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001955 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001956 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001957 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03001958 /* FIXME: We should call wl_pointer_set_focus(seat,
1959 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001960 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001961}
1962
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001963static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001964destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001965{
Daniel Stone37816df2012-05-16 18:45:18 +01001966 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001967
Daniel Stone37816df2012-05-16 18:45:18 +01001968 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001969 saved_kbd_focus_listener);
1970
Daniel Stone37816df2012-05-16 18:45:18 +01001971 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001972}
1973
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001974WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001975notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001976 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001977{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001978 struct weston_compositor *compositor = seat->compositor;
1979 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001980 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001981 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001982
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001983 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001984 wl_array_copy(&keyboard->keys, keys);
1985 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001986 weston_compositor_idle_inhibit(compositor);
1987 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001988 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001989 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001990 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001991
Daniel Stoneef172672012-06-22 13:21:32 +01001992 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001993 wl_array_for_each(k, &keyboard->keys) {
1994 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01001995 WL_KEYBOARD_KEY_STATE_PRESSED);
1996 }
1997
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001998 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001999
2000 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002001 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2002 wl_keyboard_set_focus(keyboard, surface);
2003 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002004 }
2005}
2006
2007WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002008notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01002009{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002010 struct weston_compositor *compositor = seat->compositor;
2011 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002012 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002013
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002014 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002015 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002016 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002017 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002018 WL_KEYBOARD_KEY_STATE_RELEASED);
2019 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002020
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002021 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002022
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002023 if (keyboard->focus) {
2024 seat->saved_kbd_focus = keyboard->focus;
2025 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01002026 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002027 wl_signal_add(&keyboard->focus->resource.destroy_signal,
2028 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002029 }
2030
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002031 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002032 /* FIXME: We really need keyboard grab cancel here to
2033 * let the grab shut down properly. As it is we leak
2034 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002035 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002036}
2037
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002038static void
Daniel Stone37816df2012-05-16 18:45:18 +01002039touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002040{
Daniel Stone37816df2012-05-16 18:45:18 +01002041 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002042 struct wl_resource *resource;
2043
Pekka Paalanen56464252012-07-31 13:21:08 +03002044 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002045 return;
2046
Pekka Paalanen56464252012-07-31 13:21:08 +03002047 if (seat->touch->focus_resource)
2048 wl_list_remove(&seat->touch->focus_listener.link);
2049 seat->touch->focus = NULL;
2050 seat->touch->focus_resource = NULL;
2051
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002052 if (surface) {
2053 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01002054 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04002055 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002056 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02002057 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002058 return;
2059 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002060
Daniel Stone37816df2012-05-16 18:45:18 +01002061 seat->touch->focus = surface;
2062 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03002063 wl_signal_add(&resource->destroy_signal,
2064 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002065 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002066}
2067
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002068/**
2069 * notify_touch - emulates button touches and notifies surfaces accordingly.
2070 *
2071 * It assumes always the correct cycle sequence until it gets here: touch_down
2072 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2073 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002074 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002075 */
2076WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002077notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002078 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002079{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002080 struct weston_compositor *ec = seat->compositor;
2081 struct wl_touch *touch = seat->seat.touch;
Matt Roper47c1b982012-10-10 16:56:53 -07002082 struct wl_touch_grab *grab = touch->grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002083 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002084 wl_fixed_t sx, sy;
Matt Roper47c1b982012-10-10 16:56:53 -07002085
2086 /* Update grab's global coordinates. */
2087 touch->grab_x = x;
2088 touch->grab_y = y;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002089
2090 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01002091 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002092 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002093
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002094 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002095
2096 /* the first finger down picks the surface, and all further go
2097 * to that surface for the remainder of the touch session i.e.
2098 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002099 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002100 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002101 touch_set_focus(seat, &es->surface);
2102 } else if (touch->focus) {
2103 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002104 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Rob Bradford26e009c2012-12-05 18:47:05 +00002105 } else {
2106 /* Unexpected condition: We have non-initial touch but
2107 * there is no focused surface.
2108 */
2109 weston_log("touch event received with %d points down"
2110 "but no surface focused\n", seat->num_tp);
2111 return;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002112 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002113
Matt Roper47c1b982012-10-10 16:56:53 -07002114 grab->interface->down(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002115 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002116 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002117 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002118 if (!es)
2119 break;
2120
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002121 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Matt Roper47c1b982012-10-10 16:56:53 -07002122 grab->interface->motion(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002123 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002124 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002125 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002126 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002127
Matt Roper47c1b982012-10-10 16:56:53 -07002128 grab->interface->up(grab, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002129 if (seat->num_tp == 0)
2130 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002131 break;
2132 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002133}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002134
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04002135static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002136pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
2137{
2138 struct weston_seat *seat = container_of(listener, struct weston_seat,
2139 sprite_destroy_listener);
2140
2141 seat->sprite = NULL;
2142}
2143
2144static void
2145pointer_cursor_surface_configure(struct weston_surface *es,
2146 int32_t dx, int32_t dy)
2147{
2148 struct weston_seat *seat = es->private;
2149 int x, y;
2150
2151 assert(es == seat->sprite);
2152
2153 seat->hotspot_x -= dx;
2154 seat->hotspot_y -= dy;
2155
2156 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
2157 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
2158
2159 weston_surface_configure(seat->sprite, x, y,
Pekka Paalanende685b82012-12-04 15:58:12 +02002160 es->buffer_ref.buffer->width,
2161 es->buffer_ref.buffer->height);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002162
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002163 empty_region(&es->pending.input);
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03002164
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002165 if (!weston_surface_is_mapped(es)) {
2166 wl_list_insert(&es->compositor->cursor_layer.surface_list,
2167 &es->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002168 weston_surface_update_transform(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002169 }
2170}
2171
2172static void
2173pointer_unmap_sprite(struct weston_seat *seat)
2174{
2175 if (weston_surface_is_mapped(seat->sprite))
2176 weston_surface_unmap(seat->sprite);
2177
2178 wl_list_remove(&seat->sprite_destroy_listener.link);
2179 seat->sprite->configure = NULL;
2180 seat->sprite->private = NULL;
2181 seat->sprite = NULL;
2182}
2183
2184static void
2185pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2186 uint32_t serial, struct wl_resource *surface_resource,
2187 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002188{
Daniel Stone37816df2012-05-16 18:45:18 +01002189 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002190 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002191
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002192 if (surface_resource)
Pekka Paalanen6c71ee12012-10-10 12:49:30 +03002193 surface = surface_resource->data;
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002194
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04002195 if (seat->seat.pointer->focus == NULL)
2196 return;
2197 if (seat->seat.pointer->focus->resource.client != client)
2198 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002199 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002200 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002201
2202 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002203 if (surface->configure) {
2204 wl_resource_post_error(&surface->surface.resource,
2205 WL_DISPLAY_ERROR_INVALID_OBJECT,
2206 "surface->configure already "
2207 "set");
2208 return;
2209 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002210 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002211
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002212 if (seat->sprite)
2213 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002214
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002215 if (!surface)
2216 return;
2217
2218 wl_signal_add(&surface->surface.resource.destroy_signal,
2219 &seat->sprite_destroy_listener);
2220
2221 surface->configure = pointer_cursor_surface_configure;
2222 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002223 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002224 seat->hotspot_x = x;
2225 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002226
Pekka Paalanende685b82012-12-04 15:58:12 +02002227 if (surface->buffer_ref.buffer)
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04002228 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002229}
2230
Daniel Stone37816df2012-05-16 18:45:18 +01002231static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002232 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002233};
2234
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002235static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002236handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002237{
Daniel Stone37816df2012-05-16 18:45:18 +01002238 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002239
Daniel Stone37816df2012-05-16 18:45:18 +01002240 seat = container_of(listener, struct weston_seat,
2241 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002242
Daniel Stone37816df2012-05-16 18:45:18 +01002243 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002244}
2245
Casey Dahlin9074db52012-04-19 22:50:09 -04002246static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002247{
2248 wl_list_remove(&resource->link);
2249 free(resource);
2250}
2251
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002252static void
Daniel Stone37816df2012-05-16 18:45:18 +01002253seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2254 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002255{
Daniel Stone37816df2012-05-16 18:45:18 +01002256 struct weston_seat *seat = resource->data;
2257 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002258
Daniel Stone37816df2012-05-16 18:45:18 +01002259 if (!seat->seat.pointer)
2260 return;
2261
2262 cr = wl_client_add_object(client, &wl_pointer_interface,
2263 &pointer_interface, id, seat);
2264 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002265 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002266
2267 if (seat->seat.pointer->focus &&
2268 seat->seat.pointer->focus->resource.client == client) {
2269 struct weston_surface *surface;
2270 wl_fixed_t sx, sy;
2271
2272 surface = (struct weston_surface *) seat->seat.pointer->focus;
2273 weston_surface_from_global_fixed(surface,
2274 seat->seat.pointer->x,
2275 seat->seat.pointer->y,
2276 &sx,
2277 &sy);
2278 wl_pointer_set_focus(seat->seat.pointer,
2279 seat->seat.pointer->focus,
2280 sx,
2281 sy);
2282 }
Daniel Stone37816df2012-05-16 18:45:18 +01002283}
2284
2285static void
2286seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2287 uint32_t id)
2288{
2289 struct weston_seat *seat = resource->data;
2290 struct wl_resource *cr;
2291
2292 if (!seat->seat.keyboard)
2293 return;
2294
2295 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2296 seat);
2297 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002298 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002299
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002300 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2301 seat->xkb_info.keymap_fd,
2302 seat->xkb_info.keymap_size);
2303
Daniel Stone17b13bd2012-05-30 16:31:39 +01002304 if (seat->seat.keyboard->focus &&
2305 seat->seat.keyboard->focus->resource.client == client) {
2306 wl_keyboard_set_focus(seat->seat.keyboard,
2307 seat->seat.keyboard->focus);
2308 }
Daniel Stone37816df2012-05-16 18:45:18 +01002309}
2310
2311static void
2312seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2313 uint32_t id)
2314{
2315 struct weston_seat *seat = resource->data;
2316 struct wl_resource *cr;
2317
2318 if (!seat->seat.touch)
2319 return;
2320
2321 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2322 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002323 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002324}
2325
2326static const struct wl_seat_interface seat_interface = {
2327 seat_get_pointer,
2328 seat_get_keyboard,
2329 seat_get_touch,
2330};
2331
2332static void
2333bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2334{
2335 struct wl_seat *seat = data;
2336 struct wl_resource *resource;
2337 enum wl_seat_capability caps = 0;
2338
2339 resource = wl_client_add_object(client, &wl_seat_interface,
2340 &seat_interface, id, data);
2341 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002342 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002343
2344 if (seat->pointer)
2345 caps |= WL_SEAT_CAPABILITY_POINTER;
2346 if (seat->keyboard)
2347 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2348 if (seat->touch)
2349 caps |= WL_SEAT_CAPABILITY_TOUCH;
2350
2351 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002352}
2353
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002354static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002355device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002356{
Daniel Stone37816df2012-05-16 18:45:18 +01002357 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002358
Daniel Stone37816df2012-05-16 18:45:18 +01002359 seat = container_of(listener, struct weston_seat,
2360 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002361
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002362 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002363}
2364
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002365static void weston_compositor_xkb_init(struct weston_compositor *ec,
2366 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002367{
Daniel Stonee379da92012-05-30 16:32:04 +01002368 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002369 ec->xkb_context = xkb_context_new(0);
2370 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002371 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002372 exit(1);
2373 }
Daniel Stone994679a2012-05-30 16:31:43 +01002374 }
2375
2376 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002377 ec->xkb_names = *names;
2378 if (!ec->xkb_names.rules)
2379 ec->xkb_names.rules = strdup("evdev");
2380 if (!ec->xkb_names.model)
2381 ec->xkb_names.model = strdup("pc105");
2382 if (!ec->xkb_names.layout)
2383 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002384}
2385
Daniel Stonee379da92012-05-30 16:32:04 +01002386static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2387{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002388 if (xkb_info->keymap)
2389 xkb_map_unref(xkb_info->keymap);
2390
2391 if (xkb_info->keymap_area)
2392 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2393 if (xkb_info->keymap_fd >= 0)
2394 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002395}
2396
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002397static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2398{
Daniel Stonee379da92012-05-30 16:32:04 +01002399 free((char *) ec->xkb_names.rules);
2400 free((char *) ec->xkb_names.model);
2401 free((char *) ec->xkb_names.layout);
2402 free((char *) ec->xkb_names.variant);
2403 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002404
Daniel Stonee379da92012-05-30 16:32:04 +01002405 xkb_info_destroy(&ec->xkb_info);
2406 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002407}
2408
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002409static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002410weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002411{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002412 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002413
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002414 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2415 XKB_MOD_NAME_SHIFT);
2416 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2417 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002418 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2419 XKB_MOD_NAME_CTRL);
2420 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2421 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002422 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2423 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002424 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2425 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002426 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002427
2428 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2429 XKB_LED_NAME_NUM);
2430 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2431 XKB_LED_NAME_CAPS);
2432 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2433 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002434
2435 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2436 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002437 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002438 exit(EXIT_FAILURE);
2439 }
2440 xkb_info->keymap_size = strlen(keymap_str) + 1;
2441
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002442 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002443 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002444 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002445 (unsigned long) xkb_info->keymap_size);
2446 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002447 }
2448
2449 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2450 PROT_READ | PROT_WRITE,
2451 MAP_SHARED, xkb_info->keymap_fd, 0);
2452 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002453 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002454 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002455 goto err_dev_zero;
2456 }
2457 strcpy(xkb_info->keymap_area, keymap_str);
2458 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002459
2460 return;
2461
2462err_dev_zero:
2463 close(xkb_info->keymap_fd);
2464 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002465err_keymap_str:
2466 free(keymap_str);
2467 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002468}
2469
2470static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002471weston_compositor_build_global_keymap(struct weston_compositor *ec)
2472{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002473 if (ec->xkb_info.keymap != NULL)
2474 return;
2475
Daniel Stonee379da92012-05-30 16:32:04 +01002476 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002477 &ec->xkb_names,
2478 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002479 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002480 weston_log("failed to compile global XKB keymap\n");
2481 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002482 "options %s",
2483 ec->xkb_names.rules, ec->xkb_names.model,
2484 ec->xkb_names.layout, ec->xkb_names.variant,
2485 ec->xkb_names.options);
2486 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002487 }
2488
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002489 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002490}
2491
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002492WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002493weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002494{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002495 if (seat->has_keyboard)
2496 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002497
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002498 if (keymap != NULL) {
2499 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002500 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002501 }
2502 else {
2503 weston_compositor_build_global_keymap(seat->compositor);
2504 seat->xkb_info = seat->compositor->xkb_info;
2505 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2506 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002507
2508 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2509 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002510 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01002511 exit(1);
2512 }
2513
Daniel Stone71c38772012-06-22 13:21:30 +01002514 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002515
Jan Arne Petersena75a7892013-01-16 21:26:50 +01002516 wl_keyboard_init(&seat->keyboard.keyboard);
2517 wl_seat_set_keyboard(&seat->seat, &seat->keyboard.keyboard);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002518
2519 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01002520}
2521
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002522WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002523weston_seat_init_pointer(struct weston_seat *seat)
2524{
2525 if (seat->has_pointer)
2526 return;
2527
2528 wl_pointer_init(&seat->pointer);
2529 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2530
2531 seat->has_pointer = 1;
2532}
2533
2534WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002535weston_seat_init_touch(struct weston_seat *seat)
2536{
2537 if (seat->has_touch)
2538 return;
2539
2540 wl_touch_init(&seat->touch);
2541 wl_seat_set_touch(&seat->seat, &seat->touch);
2542
2543 seat->has_touch = 1;
2544}
2545
2546WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002547weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002548{
Daniel Stone37816df2012-05-16 18:45:18 +01002549 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002550 seat->has_pointer = 0;
2551 seat->has_keyboard = 0;
2552 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002553
Daniel Stone37816df2012-05-16 18:45:18 +01002554 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2555 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002556
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002557 seat->sprite = NULL;
2558 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002559
Daniel Stone37816df2012-05-16 18:45:18 +01002560 seat->compositor = ec;
2561 seat->hotspot_x = 16;
2562 seat->hotspot_y = 16;
2563 seat->modifier_state = 0;
2564 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002565
Daniel Stone37816df2012-05-16 18:45:18 +01002566 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002567 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002568
Daniel Stone37816df2012-05-16 18:45:18 +01002569 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002570
Daniel Stone37816df2012-05-16 18:45:18 +01002571 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2572 wl_signal_add(&seat->seat.drag_icon_signal,
2573 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002574
2575 clipboard_create(seat);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002576 wl_signal_emit(&ec->seat_created_signal, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002577}
2578
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002579WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002580weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002581{
Daniel Stone37816df2012-05-16 18:45:18 +01002582 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002583 /* The global object is destroyed at wl_display_destroy() time. */
2584
Daniel Stone37816df2012-05-16 18:45:18 +01002585 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002586 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002587
Daniel Stone74419a22012-05-30 16:32:02 +01002588 if (seat->xkb_state.state != NULL)
2589 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01002590 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002591
Daniel Stone37816df2012-05-16 18:45:18 +01002592 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002593}
2594
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002595static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002596drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2597{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002598 empty_region(&es->pending.input);
2599
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002600 weston_surface_configure(es,
2601 es->geometry.x + sx, es->geometry.y + sy,
Pekka Paalanende685b82012-12-04 15:58:12 +02002602 es->buffer_ref.buffer->width,
2603 es->buffer_ref.buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002604}
2605
2606static int
Daniel Stone37816df2012-05-16 18:45:18 +01002607device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002608 struct weston_surface *surface)
2609{
Daniel Stone37816df2012-05-16 18:45:18 +01002610 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002611
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002612 if (surface->configure) {
2613 wl_resource_post_error(&surface->surface.resource,
2614 WL_DISPLAY_ERROR_INVALID_OBJECT,
2615 "surface->configure already set");
2616 return 0;
2617 }
2618
Daniel Stone37816df2012-05-16 18:45:18 +01002619 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002620
Daniel Stone37816df2012-05-16 18:45:18 +01002621 weston_surface_set_position(ws->drag_surface,
2622 wl_fixed_to_double(seat->pointer->x),
2623 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002624
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002625 surface->configure = drag_surface_configure;
2626
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002627 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002628 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002629
2630 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002631}
2632
2633static void
Daniel Stone37816df2012-05-16 18:45:18 +01002634device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002635{
Ander Conselvan de Oliveira5fd55802012-10-11 14:06:19 +03002636 if (weston_surface_is_mapped(seat->drag_surface))
2637 weston_surface_unmap(seat->drag_surface);
2638
Daniel Stone37816df2012-05-16 18:45:18 +01002639 seat->drag_surface->configure = NULL;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002640 empty_region(&seat->drag_surface->pending.input);
Daniel Stone37816df2012-05-16 18:45:18 +01002641 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2642 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002643}
2644
2645static void
Daniel Stone37816df2012-05-16 18:45:18 +01002646device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002647{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002648 struct wl_list *list;
2649
Daniel Stone37816df2012-05-16 18:45:18 +01002650 if (weston_surface_is_mapped(seat->drag_surface) ||
Pekka Paalanende685b82012-12-04 15:58:12 +02002651 !seat->drag_surface->buffer_ref.buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002652 return;
2653
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002654 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2655 list = &seat->sprite->layer_link;
2656 else
2657 list = &seat->compositor->cursor_layer.surface_list;
2658
2659 wl_list_insert(list, &seat->drag_surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002660 weston_surface_update_transform(seat->drag_surface);
Daniel Stone37816df2012-05-16 18:45:18 +01002661 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002662}
2663
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002664static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002665weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01002666 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002667{
2668 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002669
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002670 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002671 return;
2672
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002673 if (seat->drag_surface && seat->seat.drag_surface &&
2674 (&seat->drag_surface->surface.resource !=
2675 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002676 /* between calls to this funcion we got a new drag_surface */
2677 surface_changed = 1;
2678
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002679 if (!seat->seat.drag_surface || surface_changed) {
2680 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002681 if (!surface_changed)
2682 return;
2683 }
2684
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002685 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002686 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002687 seat->seat.drag_surface;
2688 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002689 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002690 }
2691
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002692 /* the client may not have attached a buffer to the drag surface
2693 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002694 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002695
2696 if (!dx && !dy)
2697 return;
2698
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002699 weston_surface_set_position(seat->drag_surface,
2700 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
2701 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002702}
2703
2704WL_EXPORT void
2705weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2706{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002707 struct weston_seat *seat;
2708
2709 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002710 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002711}
2712
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002713static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002714bind_output(struct wl_client *client,
2715 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002716{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002717 struct weston_output *output = data;
2718 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002719 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002720
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002721 resource = wl_client_add_object(client,
2722 &wl_output_interface, NULL, id, data);
2723
Casey Dahlin9074db52012-04-19 22:50:09 -04002724 wl_list_insert(&output->resource_list, &resource->link);
2725 resource->destroy = unbind_resource;
2726
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002727 wl_output_send_geometry(resource,
2728 output->x,
2729 output->y,
2730 output->mm_width,
2731 output->mm_height,
2732 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002733 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002734 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002735
2736 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002737 wl_output_send_mode(resource,
2738 mode->flags,
2739 mode->width,
2740 mode->height,
2741 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002742 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002743}
2744
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002745WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002746weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002747{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002748 struct weston_compositor *c = output->compositor;
2749
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002750 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002751 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002752 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002753
2754 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002755}
2756
Scott Moreau1bad5db2012-08-18 01:04:05 -06002757static void
2758weston_output_compute_transform(struct weston_output *output)
2759{
2760 struct weston_matrix transform;
2761 int flip;
2762
2763 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002764 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002765
2766 switch(output->transform) {
2767 case WL_OUTPUT_TRANSFORM_FLIPPED:
2768 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2769 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2770 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002771 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002772 flip = -1;
2773 break;
2774 default:
2775 flip = 1;
2776 break;
2777 }
2778
2779 switch(output->transform) {
2780 case WL_OUTPUT_TRANSFORM_NORMAL:
2781 case WL_OUTPUT_TRANSFORM_FLIPPED:
2782 transform.d[0] = flip;
2783 transform.d[1] = 0;
2784 transform.d[4] = 0;
2785 transform.d[5] = 1;
2786 break;
2787 case WL_OUTPUT_TRANSFORM_90:
2788 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2789 transform.d[0] = 0;
2790 transform.d[1] = -flip;
2791 transform.d[4] = 1;
2792 transform.d[5] = 0;
2793 break;
2794 case WL_OUTPUT_TRANSFORM_180:
2795 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2796 transform.d[0] = -flip;
2797 transform.d[1] = 0;
2798 transform.d[4] = 0;
2799 transform.d[5] = -1;
2800 break;
2801 case WL_OUTPUT_TRANSFORM_270:
2802 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2803 transform.d[0] = 0;
2804 transform.d[1] = flip;
2805 transform.d[4] = -1;
2806 transform.d[5] = 0;
2807 break;
2808 default:
2809 break;
2810 }
2811
2812 weston_matrix_multiply(&output->matrix, &transform);
2813}
2814
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002815WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002816weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002817{
Scott Moreau850ca422012-05-21 15:21:25 -06002818 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002819 struct weston_matrix camera;
2820 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002821
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002822 weston_matrix_init(&output->matrix);
2823 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002824 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2825 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002826
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002827 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002828 2.0 / (output->width + output->border.left + output->border.right),
2829 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2830
2831 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002832
Scott Moreauccbf29d2012-02-22 14:21:41 -07002833 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002834 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002835 weston_matrix_init(&camera);
2836 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002837 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002838 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002839 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002840 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002841 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002842 weston_matrix_multiply(&output->matrix, &modelview);
2843 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002844
Scott Moreauccbf29d2012-02-22 14:21:41 -07002845 output->dirty = 0;
2846}
2847
Scott Moreau1bad5db2012-08-18 01:04:05 -06002848static void
2849weston_output_transform_init(struct weston_output *output, uint32_t transform)
2850{
2851 output->transform = transform;
2852
2853 switch (transform) {
2854 case WL_OUTPUT_TRANSFORM_90:
2855 case WL_OUTPUT_TRANSFORM_270:
2856 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2857 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2858 /* Swap width and height */
2859 output->width = output->current->height;
2860 output->height = output->current->width;
2861 break;
2862 case WL_OUTPUT_TRANSFORM_NORMAL:
2863 case WL_OUTPUT_TRANSFORM_180:
2864 case WL_OUTPUT_TRANSFORM_FLIPPED:
2865 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2866 output->width = output->current->width;
2867 output->height = output->current->height;
2868 break;
2869 default:
2870 break;
2871 }
2872}
2873
Scott Moreauccbf29d2012-02-22 14:21:41 -07002874WL_EXPORT void
2875weston_output_move(struct weston_output *output, int x, int y)
2876{
2877 output->x = x;
2878 output->y = y;
2879
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002880 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002881 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002882 output->width,
2883 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002884}
2885
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002886WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002887weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002888 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002889{
2890 output->compositor = c;
2891 output->x = x;
2892 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002893 output->border.top = 0;
2894 output->border.bottom = 0;
2895 output->border.left = 0;
2896 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002897 output->mm_width = width;
2898 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002899 output->dirty = 1;
2900
Scott Moreau1bad5db2012-08-18 01:04:05 -06002901 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06002902 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002903
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002904 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002905 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002906
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002907 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002908 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002909 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002910
Casey Dahlin58ba1372012-04-19 22:50:08 -04002911 output->id = ffs(~output->compositor->output_id_pool) - 1;
2912 output->compositor->output_id_pool |= 1 << output->id;
2913
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002914 output->global =
2915 wl_display_add_global(c->wl_display, &wl_output_interface,
2916 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002917}
2918
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002919static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002920compositor_bind(struct wl_client *client,
2921 void *data, uint32_t version, uint32_t id)
2922{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002923 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002924
2925 wl_client_add_object(client, &wl_compositor_interface,
2926 &compositor_interface, id, compositor);
2927}
2928
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002929static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002930log_uname(void)
2931{
2932 struct utsname usys;
2933
2934 uname(&usys);
2935
2936 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2937 usys.version, usys.machine);
2938}
2939
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002940WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002941weston_compositor_init(struct weston_compositor *ec,
2942 struct wl_display *display,
2943 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04002944 char *argv[],
2945 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002946{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002947 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002948 struct xkb_rule_names xkb_names;
2949 const struct config_key keyboard_config_keys[] = {
2950 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2951 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2952 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2953 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2954 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2955 };
2956 const struct config_section cs[] = {
2957 { "keyboard",
2958 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
2959 };
2960
2961 memset(&xkb_names, 0, sizeof(xkb_names));
2962 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002963
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002964 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002965 wl_signal_init(&ec->destroy_signal);
2966 wl_signal_init(&ec->activate_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002967 wl_signal_init(&ec->kill_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002968 wl_signal_init(&ec->lock_signal);
2969 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002970 wl_signal_init(&ec->show_input_panel_signal);
2971 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002972 wl_signal_init(&ec->seat_created_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002973 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002974
Casey Dahlin58ba1372012-04-19 22:50:08 -04002975 ec->output_id_pool = 0;
2976
Kristian Høgsberga8873122011-11-23 10:39:34 -05002977 if (!wl_display_add_global(display, &wl_compositor_interface,
2978 ec, compositor_bind))
2979 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002980
Daniel Stone725c2c32012-06-22 14:04:36 +01002981 wl_list_init(&ec->surface_list);
2982 wl_list_init(&ec->layer_list);
2983 wl_list_init(&ec->seat_list);
2984 wl_list_init(&ec->output_list);
2985 wl_list_init(&ec->key_binding_list);
2986 wl_list_init(&ec->button_binding_list);
2987 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002988 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002989 wl_list_init(&ec->fade.animation.link);
2990
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002991 weston_plane_init(&ec->primary_plane, 0, 0);
2992
Daniel Stone725c2c32012-06-22 14:04:36 +01002993 weston_compositor_xkb_init(ec, &xkb_names);
2994
2995 ec->ping_handler = NULL;
2996
2997 screenshooter_create(ec);
2998 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002999 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003000
3001 wl_data_device_manager_init(ec->wl_display);
3002
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003003 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003004
Daniel Stone725c2c32012-06-22 14:04:36 +01003005 loop = wl_display_get_event_loop(ec->wl_display);
3006 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3007 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3008
3009 ec->input_loop = wl_event_loop_create();
3010
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003011 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
3012 ec->fade.animation.frame = fade_frame;
3013
3014 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3015 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3016
3017 weston_compositor_schedule_repaint(ec);
3018
Daniel Stone725c2c32012-06-22 14:04:36 +01003019 return 0;
3020}
3021
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003022WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003023weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003024{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003025 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003026
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003027 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003028 if (ec->input_loop_source)
3029 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003030
Matt Roper361d2ad2011-08-29 13:52:23 -07003031 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003032 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003033 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003034
Daniel Stone325fc2d2012-05-30 16:31:58 +01003035 weston_binding_list_destroy_all(&ec->key_binding_list);
3036 weston_binding_list_destroy_all(&ec->button_binding_list);
3037 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003038 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003039
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003040 weston_plane_release(&ec->primary_plane);
3041
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003042 wl_array_release(&ec->vertices);
3043 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05003044 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003045
3046 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07003047}
3048
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003049static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003050{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003051 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003052
Martin Minarik6d118362012-06-07 18:01:59 +02003053 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003054 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003055
3056 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003057}
3058
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003059#ifdef HAVE_LIBUNWIND
3060
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003061static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003062print_backtrace(void)
3063{
3064 unw_cursor_t cursor;
3065 unw_context_t context;
3066 unw_word_t off;
3067 unw_proc_info_t pip;
3068 int ret, i = 0;
3069 char procname[256];
3070 const char *filename;
3071 Dl_info dlinfo;
3072
3073 pip.unwind_info = NULL;
3074 ret = unw_getcontext(&context);
3075 if (ret) {
3076 weston_log("unw_getcontext: %d\n", ret);
3077 return;
3078 }
3079
3080 ret = unw_init_local(&cursor, &context);
3081 if (ret) {
3082 weston_log("unw_init_local: %d\n", ret);
3083 return;
3084 }
3085
3086 ret = unw_step(&cursor);
3087 while (ret > 0) {
3088 ret = unw_get_proc_info(&cursor, &pip);
3089 if (ret) {
3090 weston_log("unw_get_proc_info: %d\n", ret);
3091 break;
3092 }
3093
3094 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3095 if (ret && ret != -UNW_ENOMEM) {
3096 if (ret != -UNW_EUNSPEC)
3097 weston_log("unw_get_proc_name: %d\n", ret);
3098 procname[0] = '?';
3099 procname[1] = 0;
3100 }
3101
3102 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3103 *dlinfo.dli_fname)
3104 filename = dlinfo.dli_fname;
3105 else
3106 filename = "?";
3107
3108 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3109 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3110
3111 ret = unw_step(&cursor);
3112 if (ret < 0)
3113 weston_log("unw_step: %d\n", ret);
3114 }
3115}
3116
3117#else
3118
3119static void
3120print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003121{
3122 void *buffer[32];
3123 int i, count;
3124 Dl_info info;
3125
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003126 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3127 for (i = 0; i < count; i++) {
3128 dladdr(buffer[i], &info);
3129 weston_log(" [%016lx] %s (%s)\n",
3130 (long) buffer[i],
3131 info.dli_sname ? info.dli_sname : "--",
3132 info.dli_fname);
3133 }
3134}
3135
3136#endif
3137
3138static void
3139on_segv_signal(int s, siginfo_t *siginfo, void *context)
3140{
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003141 /* This SIGSEGV handler will do a best-effort backtrace, and
3142 * then call the backend restore function, which will switch
3143 * back to the vt we launched from or ungrab X etc and then
3144 * raise SIGTRAP. If we run weston under gdb from X or a
3145 * different vt, and tell gdb "handle SIGSEGV nostop", this
3146 * will allow weston to switch back to gdb on crash and then
3147 * gdb will catch the crash with SIGTRAP. */
3148
Martin Minarik6d118362012-06-07 18:01:59 +02003149 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003150
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003151 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003152
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003153 segv_compositor->restore(segv_compositor);
3154
3155 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003156}
3157
3158
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003159static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003160load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003161{
3162 char path[PATH_MAX];
3163 void *module, *init;
3164
3165 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003166 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003167 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003168 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003169
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003170 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3171 if (module) {
3172 weston_log("Module '%s' already loaded\n", path);
3173 dlclose(module);
3174 return NULL;
3175 }
3176
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003177 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003178 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003179 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003180 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003181 return NULL;
3182 }
3183
3184 init = dlsym(module, entrypoint);
3185 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003186 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003187 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003188 return NULL;
3189 }
3190
3191 return init;
3192}
3193
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003194static int
3195load_modules(struct weston_compositor *ec, const char *modules)
3196{
3197 const char *p, *end;
3198 char buffer[256];
3199 int (*module_init)(struct weston_compositor *ec);
3200
3201 if (modules == NULL)
3202 return 0;
3203
3204 p = modules;
3205 while (*p) {
3206 end = strchrnul(p, ',');
3207 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
3208 module_init = load_module(buffer, "module_init");
3209 if (module_init)
3210 module_init(ec);
3211 p = end;
3212 while (*p == ',')
3213 p++;
3214
3215 }
3216
3217 return 0;
3218}
3219
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003220static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003221 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3222
3223static const char xdg_wrong_message[] =
3224 "fatal: environment variable XDG_RUNTIME_DIR\n"
3225 "is set to \"%s\", which is not a directory.\n";
3226
3227static const char xdg_wrong_mode_message[] =
3228 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3229 "correctly. Unix access mode must be 0700 but is %o,\n"
3230 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003231 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003232
3233static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003234 "Refer to your distribution on how to get it, or\n"
3235 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3236 "on how to implement it.\n";
3237
Martin Minarik37032f82012-06-18 20:15:18 +02003238static void
3239verify_xdg_runtime_dir(void)
3240{
3241 char *dir = getenv("XDG_RUNTIME_DIR");
3242 struct stat s;
3243
3244 if (!dir) {
3245 weston_log(xdg_error_message);
3246 weston_log_continue(xdg_detail_message);
3247 exit(EXIT_FAILURE);
3248 }
3249
3250 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3251 weston_log(xdg_wrong_message, dir);
3252 weston_log_continue(xdg_detail_message);
3253 exit(EXIT_FAILURE);
3254 }
3255
3256 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3257 weston_log(xdg_wrong_mode_message,
3258 dir, s.st_mode & 0777, s.st_uid);
3259 weston_log_continue(xdg_detail_message);
3260 }
3261}
3262
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003263static int
3264usage(int error_code)
3265{
3266 fprintf(stderr,
3267 "Usage: weston [OPTIONS]\n\n"
3268 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3269 "Weston supports multiple backends, and depending on which backend is in use\n"
3270 "different options will be accepted.\n\n"
3271
3272
3273 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003274 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003275 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3276 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3277 " -S, --socket=NAME\tName of socket to listen on\n"
3278 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003279 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003280 " --log==FILE\t\tLog to the given file\n"
3281 " -h, --help\t\tThis help message\n\n");
3282
3283 fprintf(stderr,
3284 "Options for drm-backend.so:\n\n"
3285 " --connector=ID\tBring up only this connector\n"
3286 " --seat=SEAT\t\tThe seat that weston should run on\n"
3287 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003288 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003289 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3290
3291 fprintf(stderr,
3292 "Options for x11-backend.so:\n\n"
3293 " --width=WIDTH\t\tWidth of X window\n"
3294 " --height=HEIGHT\tHeight of X window\n"
3295 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003296 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003297 " --output-count=COUNT\tCreate multiple outputs\n"
3298 " --no-input\t\tDont create input devices\n\n");
3299
3300 fprintf(stderr,
3301 "Options for wayland-backend.so:\n\n"
3302 " --width=WIDTH\t\tWidth of Wayland surface\n"
3303 " --height=HEIGHT\tHeight of Wayland surface\n"
3304 " --display=DISPLAY\tWayland display to connect to\n\n");
3305
3306 exit(error_code);
3307}
3308
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003309int main(int argc, char *argv[])
3310{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003311 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003312 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003313 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003314 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003315 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003316 struct sigaction segv_action;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003317 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003318 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003319 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003320 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003321 char *backend = NULL;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003322 const char *modules = "desktop-shell.so", *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003323 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003324 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003325 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003326 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003327 int32_t version = 0;
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003328 char *config_file;
3329
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003330 const struct config_key core_config_keys[] = {
3331 { "modules", CONFIG_KEY_STRING, &modules },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003332 };
3333
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003334 const struct config_section cs[] = {
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003335 { "core",
3336 core_config_keys, ARRAY_LENGTH(core_config_keys) },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003337 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003338
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003339 const struct weston_option core_options[] = {
3340 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3341 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3342 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003343 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003344 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003345 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003346 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003347 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003348
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003349 argc = parse_options(core_options,
3350 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003351
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003352 if (help)
3353 usage(EXIT_SUCCESS);
3354
Scott Moreau12245142012-08-29 15:15:58 -06003355 if (version) {
3356 printf(PACKAGE_STRING "\n");
3357 return EXIT_SUCCESS;
3358 }
3359
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003360 weston_log_file_open(log);
3361
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003362 weston_log("%s\n"
3363 STAMP_SPACE "%s\n"
3364 STAMP_SPACE "Bug reports to: %s\n"
3365 STAMP_SPACE "Build: %s\n",
3366 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003367 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003368 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003369
Martin Minarik37032f82012-06-18 20:15:18 +02003370 verify_xdg_runtime_dir();
3371
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003372 display = wl_display_create();
3373
Tiago Vignatti2116b892011-08-08 05:52:59 -07003374 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003375 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3376 display);
3377 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3378 display);
3379 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3380 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003381
3382 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003383 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3384 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003385
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003386 if (!backend) {
3387 if (getenv("WAYLAND_DISPLAY"))
3388 backend = "wayland-backend.so";
3389 else if (getenv("DISPLAY"))
3390 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003391 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003392 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003393 }
3394
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003395 config_file = config_file_path("weston.ini");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003396 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003397
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003398 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003399 if (!backend_init)
3400 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003401
Daniel Stonec1be8e52012-06-01 11:14:02 -04003402 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003403 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003404 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003405 exit(EXIT_FAILURE);
3406 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003407
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003408 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3409 segv_action.sa_sigaction = on_segv_signal;
3410 sigemptyset(&segv_action.sa_mask);
3411 sigaction(SIGSEGV, &segv_action, NULL);
3412 segv_compositor = ec;
3413
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003414 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03003415 weston_log("fatal: unhandled option: %s\n", argv[i]);
3416 if (argv[1]) {
3417 ret = EXIT_FAILURE;
3418 goto out;
3419 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003420
Daniel Stonec1be8e52012-06-01 11:14:02 -04003421 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01003422
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003423 ec->option_idle_time = idle_time;
3424 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003425
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003426 setenv("WAYLAND_DISPLAY", socket_name, 1);
3427
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003428 if (load_modules(ec, modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003429 goto out;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003430 if (load_modules(ec, option_modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003431 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003432
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003433 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003434 weston_log("fatal: failed to add socket: %m\n");
3435 ret = EXIT_FAILURE;
3436 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003437 }
3438
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003439 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003440 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003441
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003442 wl_display_run(display);
3443
3444 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003445 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05003446 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003447
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003448 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003449
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003450 for (i = ARRAY_LENGTH(signals); i;)
3451 wl_event_source_remove(signals[--i]);
3452
Daniel Stone855028f2012-05-01 20:37:10 +01003453 weston_compositor_xkb_destroy(ec);
3454
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003455 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003456 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003457
Martin Minarik19e6f262012-06-07 13:08:46 +02003458 weston_log_file_close();
3459
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003460 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003461}