blob: bda13818e1b4b6ac461fd20535bc6c8dc4ef1577 [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
Pekka Paalanen50719bc2011-11-22 14:18:50 +020052#include <wayland-server.h>
Kristian Høgsberg82863022010-06-04 21:52:02 -040053#include "compositor.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030054#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040055#include "git-version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050056
Kristian Høgsberg27da5382011-06-21 17:32:25 -040057static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040058static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040059
60static int
61sigchld_handler(int signal_number, void *data)
62{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050063 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040064 int status;
65 pid_t pid;
66
Bill Spitzak027b9622012-03-17 15:22:03 -070067 pid = waitpid(-1, &status, WNOHANG);
68 if (!pid)
69 return 1;
70
Kristian Høgsberg27da5382011-06-21 17:32:25 -040071 wl_list_for_each(p, &child_process_list, link) {
72 if (p->pid == pid)
73 break;
74 }
75
76 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020077 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040078 return 1;
79 }
80
81 wl_list_remove(&p->link);
82 p->cleanup(p, status);
83
84 return 1;
85}
86
Alex Wu2dda6042012-04-17 17:20:47 +080087WL_EXPORT int
88weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode)
89{
90 if (!output->switch_mode)
91 return -1;
92
93 return output->switch_mode(output, mode);
94}
95
Kristian Høgsberg27da5382011-06-21 17:32:25 -040096WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050097weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -040098{
99 wl_list_insert(&child_process_list, &process->link);
100}
101
Benjamin Franzke06286262011-05-06 19:12:33 +0200102static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200103child_client_exec(int sockfd, const char *path)
104{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500105 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200106 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200107 sigset_t allsigs;
108
109 /* do not give our signal mask to the new process */
110 sigfillset(&allsigs);
111 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200112
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500113 /* Launch clients as the user. */
114 seteuid(getuid());
115
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500116 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
117 * non-CLOEXEC fd to pass through exec. */
118 clientfd = dup(sockfd);
119 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200120 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500121 return;
122 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200123
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500124 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200125 setenv("WAYLAND_SOCKET", s, 1);
126
127 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200128 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200129 path);
130}
131
132WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500133weston_client_launch(struct weston_compositor *compositor,
134 struct weston_process *proc,
135 const char *path,
136 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200137{
138 int sv[2];
139 pid_t pid;
140 struct wl_client *client;
141
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300142 weston_log("launching '%s'\n", path);
143
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300144 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200145 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200146 "socketpair failed while launching '%s': %m\n",
147 path);
148 return NULL;
149 }
150
151 pid = fork();
152 if (pid == -1) {
153 close(sv[0]);
154 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200155 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200156 "fork failed while launching '%s': %m\n", path);
157 return NULL;
158 }
159
160 if (pid == 0) {
161 child_client_exec(sv[1], path);
162 exit(-1);
163 }
164
165 close(sv[1]);
166
167 client = wl_client_create(compositor->wl_display, sv[0]);
168 if (!client) {
169 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200170 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200171 "wl_client_create failed while launching '%s'.\n",
172 path);
173 return NULL;
174 }
175
176 proc->pid = pid;
177 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500178 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200179
180 return client;
181}
182
183static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400184surface_handle_buffer_destroy(struct wl_listener *listener, void *data)
Benjamin Franzke06286262011-05-06 19:12:33 +0200185{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500186 struct weston_surface *es =
187 container_of(listener, struct weston_surface,
188 buffer_destroy_listener);
Benjamin Franzke06286262011-05-06 19:12:33 +0200189
Kristian Høgsberga85b4fb2012-06-20 09:24:35 -0400190 if (es->buffer && wl_buffer_is_shm(es->buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400191 es->compositor->renderer->flush_damage(es);
Kristian Høgsberga85b4fb2012-06-20 09:24:35 -0400192
Kristian Høgsberg24596942011-10-24 17:51:02 -0400193 es->buffer = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200194}
195
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500196static const pixman_region32_data_t undef_region_data;
197
198static void
199undef_region(pixman_region32_t *region)
200{
Kristian Høgsberg66a099b2012-05-29 16:49:45 -0400201 if (region->data != &undef_region_data)
202 pixman_region32_fini(region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500203 region->data = (pixman_region32_data_t *) &undef_region_data;
204}
205
206static int
207region_is_undefined(pixman_region32_t *region)
208{
209 return region->data == &undef_region_data;
210}
211
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200212static void
213empty_region(pixman_region32_t *region)
214{
215 if (!region_is_undefined(region))
216 pixman_region32_fini(region);
217
218 pixman_region32_init(region);
219}
220
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500221WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500222weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500223{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500224 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400225
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200226 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400227 if (surface == NULL)
228 return NULL;
229
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400230 wl_signal_init(&surface->surface.resource.destroy_signal);
Kristian Høgsberg48891542012-02-23 17:38:33 -0500231
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500232 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500233 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500234
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400235 surface->surface.resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400236
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500237 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400238 surface->alpha = 1.0;
Juan Zhao46436612012-03-20 01:48:46 +0800239 surface->pitch = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400240
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200241 surface->num_textures = 0;
242 surface->num_images = 0;
243
Benjamin Franzke06286262011-05-06 19:12:33 +0200244 surface->buffer = NULL;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400245 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400246 surface->plane = &compositor->primary_plane;
Benjamin Franzke06286262011-05-06 19:12:33 +0200247
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400248 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500249 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500250 pixman_region32_init(&surface->clip);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500251 undef_region(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200252 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500253 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400254
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400255 surface->buffer_destroy_listener.notify =
256 surface_handle_buffer_destroy;
Benjamin Franzke06286262011-05-06 19:12:33 +0200257
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200258 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200259 wl_list_insert(&surface->geometry.transformation_list,
260 &surface->transform.position.link);
261 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200262 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200263 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500264
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400265 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500266}
267
Alex Wu8811bf92012-02-28 18:07:54 +0800268WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500269weston_surface_set_color(struct weston_surface *surface,
270 GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
271{
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500272 surface->color[0] = red;
273 surface->color[1] = green;
274 surface->color[2] = blue;
275 surface->color[3] = alpha;
276 surface->shader = &surface->compositor->solid_shader;
277}
278
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400279WL_EXPORT void
280weston_surface_to_global_float(struct weston_surface *surface,
281 GLfloat sx, GLfloat sy, GLfloat *x, GLfloat *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200282{
283 if (surface->transform.enabled) {
284 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
285
286 weston_matrix_transform(&surface->transform.matrix, &v);
287
288 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200289 weston_log("warning: numerical instability in "
Pekka Paalanenece8a012012-02-08 15:23:15 +0200290 "weston_surface_to_global(), divisor = %g\n",
291 v.f[3]);
292 *x = 0;
293 *y = 0;
294 return;
295 }
296
297 *x = v.f[0] / v.f[3];
298 *y = v.f[1] / v.f[3];
299 } else {
300 *x = sx + surface->geometry.x;
301 *y = sy + surface->geometry.y;
302 }
303}
304
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500305WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400306weston_surface_move_to_plane(struct weston_surface *surface,
307 struct weston_plane *plane)
308{
309 if (surface->plane == plane)
310 return;
311
312 weston_surface_damage_below(surface);
313 surface->plane = plane;
314 weston_surface_damage(surface);
315}
316
317WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500318weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200319{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500320 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200321
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500322 pixman_region32_init(&damage);
323 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
324 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400325 pixman_region32_union(&surface->plane->damage,
326 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500327 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200328}
329
330static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200331surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
332 int32_t width, int32_t height,
333 pixman_region32_t *bbox)
334{
Pekka Paalanen219b9822012-02-08 15:38:37 +0200335 GLfloat min_x = HUGE_VALF, min_y = HUGE_VALF;
336 GLfloat max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200337 int32_t s[4][2] = {
338 { sx, sy },
339 { sx, sy + height },
340 { sx + width, sy },
341 { sx + width, sy + height }
342 };
Pekka Paalanen219b9822012-02-08 15:38:37 +0200343 GLfloat int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200344 int i;
345
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300346 if (width == 0 || height == 0) {
347 /* avoid rounding empty bbox to 1x1 */
348 pixman_region32_init(bbox);
349 return;
350 }
351
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200352 for (i = 0; i < 4; ++i) {
Pekka Paalanen219b9822012-02-08 15:38:37 +0200353 GLfloat x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400354 weston_surface_to_global_float(surface,
355 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200356 if (x < min_x)
357 min_x = x;
358 if (x > max_x)
359 max_x = x;
360 if (y < min_y)
361 min_y = y;
362 if (y > max_y)
363 max_y = y;
364 }
365
Pekka Paalanen219b9822012-02-08 15:38:37 +0200366 int_x = floorf(min_x);
367 int_y = floorf(min_y);
368 pixman_region32_init_rect(bbox, int_x, int_y,
369 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200370}
371
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200372static void
373weston_surface_update_transform_disable(struct weston_surface *surface)
374{
375 surface->transform.enabled = 0;
376
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200377 /* round off fractions when not transformed */
378 surface->geometry.x = roundf(surface->geometry.x);
379 surface->geometry.y = roundf(surface->geometry.y);
380
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200381 pixman_region32_init_rect(&surface->transform.boundingbox,
382 surface->geometry.x,
383 surface->geometry.y,
384 surface->geometry.width,
385 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500386
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400387 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500388 pixman_region32_copy(&surface->transform.opaque,
389 &surface->opaque);
390 pixman_region32_translate(&surface->transform.opaque,
391 surface->geometry.x,
392 surface->geometry.y);
393 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200394}
395
396static int
397weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200398{
399 struct weston_matrix *matrix = &surface->transform.matrix;
400 struct weston_matrix *inverse = &surface->transform.inverse;
401 struct weston_transform *tform;
402
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200403 surface->transform.enabled = 1;
404
405 /* Otherwise identity matrix, but with x and y translation. */
406 surface->transform.position.matrix.d[12] = surface->geometry.x;
407 surface->transform.position.matrix.d[13] = surface->geometry.y;
408
409 weston_matrix_init(matrix);
410 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
411 weston_matrix_multiply(matrix, &tform->matrix);
412
413 if (weston_matrix_invert(inverse, matrix) < 0) {
414 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200415 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200416 " transformation not invertible.\n", surface);
417 return -1;
418 }
419
420 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
421 surface->geometry.height,
422 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500423
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200424 return 0;
425}
426
427WL_EXPORT void
428weston_surface_update_transform(struct weston_surface *surface)
429{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200430 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200431 return;
432
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200433 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200434
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500435 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200436
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200437 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500438 pixman_region32_fini(&surface->transform.opaque);
439 pixman_region32_init(&surface->transform.opaque);
440
441 if (region_is_undefined(&surface->input))
442 pixman_region32_init_rect(&surface->input, 0, 0,
443 surface->geometry.width,
444 surface->geometry.height);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200445
Pekka Paalanencd403622012-01-25 13:37:39 +0200446 /* transform.position is always in transformation_list */
447 if (surface->geometry.transformation_list.next ==
448 &surface->transform.position.link &&
449 surface->geometry.transformation_list.prev ==
450 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200451 weston_surface_update_transform_disable(surface);
452 } else {
453 if (weston_surface_update_transform_enable(surface) < 0)
454 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200455 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200456
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400457 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200458
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300459 if (weston_surface_is_mapped(surface))
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +0200460 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200461}
462
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200463WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100464weston_surface_to_global_fixed(struct weston_surface *surface,
465 wl_fixed_t sx, wl_fixed_t sy,
466 wl_fixed_t *x, wl_fixed_t *y)
467{
468 GLfloat xf, yf;
469
470 weston_surface_to_global_float(surface,
471 wl_fixed_to_double(sx),
472 wl_fixed_to_double(sy),
473 &xf, &yf);
474 *x = wl_fixed_from_double(xf);
475 *y = wl_fixed_from_double(yf);
476}
477
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400478WL_EXPORT void
479weston_surface_from_global_float(struct weston_surface *surface,
480 GLfloat x, GLfloat y, GLfloat *sx, GLfloat *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200481{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200482 if (surface->transform.enabled) {
483 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
484
485 weston_matrix_transform(&surface->transform.inverse, &v);
486
487 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200488 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200489 "weston_surface_from_global(), divisor = %g\n",
490 v.f[3]);
491 *sx = 0;
492 *sy = 0;
493 return;
494 }
495
Pekka Paalanencd403622012-01-25 13:37:39 +0200496 *sx = v.f[0] / v.f[3];
497 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200498 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200499 *sx = x - surface->geometry.x;
500 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200501 }
502}
503
504WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100505weston_surface_from_global_fixed(struct weston_surface *surface,
506 wl_fixed_t x, wl_fixed_t y,
507 wl_fixed_t *sx, wl_fixed_t *sy)
508{
509 GLfloat sxf, syf;
510
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400511 weston_surface_from_global_float(surface,
512 wl_fixed_to_double(x),
513 wl_fixed_to_double(y),
514 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100515 *sx = wl_fixed_from_double(sxf);
516 *sy = wl_fixed_from_double(syf);
517}
518
519WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200520weston_surface_from_global(struct weston_surface *surface,
521 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
522{
523 GLfloat sxf, syf;
524
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400525 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200526 *sx = floorf(sxf);
527 *sy = floorf(syf);
528}
529
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400530WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400531weston_surface_schedule_repaint(struct weston_surface *surface)
532{
533 struct weston_output *output;
534
535 wl_list_for_each(output, &surface->compositor->output_list, link)
536 if (surface->output_mask & (1 << output->id))
537 weston_output_schedule_repaint(output);
538}
539
540WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500541weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500542{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400543 pixman_region32_union_rect(&surface->damage, &surface->damage,
544 0, 0, surface->geometry.width,
545 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200546
Kristian Høgsberg98238702012-08-03 16:29:12 -0400547 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500548}
549
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400550WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500551weston_surface_configure(struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200552 GLfloat x, GLfloat y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400553{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200554 surface->geometry.x = x;
555 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200556 surface->geometry.width = width;
557 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200558 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400559}
560
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200561WL_EXPORT void
562weston_surface_set_position(struct weston_surface *surface,
563 GLfloat x, GLfloat y)
564{
565 surface->geometry.x = x;
566 surface->geometry.y = y;
567 surface->geometry.dirty = 1;
568}
569
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300570WL_EXPORT int
571weston_surface_is_mapped(struct weston_surface *surface)
572{
573 if (surface->output)
574 return 1;
575 else
576 return 0;
577}
578
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400579WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500580weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500581{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400582 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500583
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400584 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500585
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400586 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500587}
588
Tiago Vignatti9d393522012-02-10 16:26:19 +0200589static struct weston_surface *
590weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100591 wl_fixed_t x, wl_fixed_t y,
592 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200593{
594 struct weston_surface *surface;
595
596 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100597 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500598 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100599 wl_fixed_to_int(*sx),
600 wl_fixed_to_int(*sy),
601 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200602 return surface;
603 }
604
605 return NULL;
606}
607
608static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400609weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500610{
Scott Moreau447013d2012-02-18 05:05:29 -0700611 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500612 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400613 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500614
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400615 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100616 return;
617
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400618 surface = weston_compositor_pick_surface(seat->compositor,
619 pointer->x,
620 pointer->y,
621 &pointer->current_x,
622 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500623
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400624 if (&surface->surface != pointer->current) {
625 interface = pointer->grab->interface;
626 pointer->current = &surface->surface;
627 interface->focus(pointer->grab, &surface->surface,
628 pointer->current_x,
629 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500630 }
631
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400632 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500633 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100634 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400635 pointer->x,
636 pointer->y,
637 &pointer->grab->x,
638 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500639}
640
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400641static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500642weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400643{
Daniel Stone37816df2012-05-16 18:45:18 +0100644 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400645
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500646 if (!compositor->focus)
647 return;
648
Daniel Stone37816df2012-05-16 18:45:18 +0100649 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400650 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400651}
652
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400653WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500654weston_surface_unmap(struct weston_surface *surface)
655{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100656 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500657
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500658 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500659 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500660 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500661
Daniel Stone4dab5db2012-05-30 16:31:53 +0100662 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100663 if (seat->seat.keyboard &&
664 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100665 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100666 if (seat->seat.pointer &&
667 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100668 wl_pointer_set_focus(seat->seat.pointer,
669 NULL,
670 wl_fixed_from_int(0),
671 wl_fixed_from_int(0));
672 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500673
Kristian Høgsberg98238702012-08-03 16:29:12 -0400674 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500675}
676
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400677struct weston_frame_callback {
678 struct wl_resource resource;
679 struct wl_list link;
680};
681
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500682static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400683destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500684{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500685 struct weston_surface *surface =
686 container_of(resource,
687 struct weston_surface, surface.resource);
688 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400689 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400690
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300691 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500692 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400693
Benjamin Franzke06286262011-05-06 19:12:33 +0200694 if (surface->buffer)
695 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400696
Kristian Høgsberg42263852012-09-06 21:59:29 -0400697 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200698
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200699 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200700 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500701 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500702 pixman_region32_fini(&surface->clip);
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500703 if (!region_is_undefined(&surface->input))
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500704 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200705
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400706 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
707 wl_resource_destroy(&cb->resource);
708
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400709 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500710}
711
Alex Wu8811bf92012-02-28 18:07:54 +0800712WL_EXPORT void
713weston_surface_destroy(struct weston_surface *surface)
714{
715 /* Not a valid way to destroy a client surface */
716 assert(surface->surface.resource.client == NULL);
717
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400718 wl_signal_emit(&surface->surface.resource.destroy_signal,
719 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800720 destroy_surface(&surface->surface.resource);
721}
722
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100723static void
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -0400724weston_surface_attach(struct wl_surface *surface, struct wl_buffer *buffer)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100725{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500726 struct weston_surface *es = (struct weston_surface *) surface;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100727
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300728 if (es->buffer) {
729 weston_buffer_post_release(es->buffer);
730 wl_list_remove(&es->buffer_destroy_listener.link);
731 }
732
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400733 if (buffer) {
734 buffer->busy_count++;
735 wl_signal_add(&buffer->resource.destroy_signal,
736 &es->buffer_destroy_listener);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300737
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400738 if (es->geometry.width != buffer->width ||
739 es->geometry.height != buffer->height) {
740 undef_region(&es->input);
741 pixman_region32_fini(&es->opaque);
742 pixman_region32_init(&es->opaque);
743 }
744 } else {
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300745 if (weston_surface_is_mapped(es))
746 weston_surface_unmap(es);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300747 }
748
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400749 es->compositor->renderer->attach(es, buffer);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400750 es->buffer = buffer;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100751}
752
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500753WL_EXPORT void
754weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400755{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500756 wl_list_remove(&surface->layer_link);
757 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500758 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500759 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400760}
761
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400762WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500763weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400764{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500765 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400766
767 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500768 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400769}
770
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500771WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500772weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +0200773{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400774 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200775 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200776
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400777 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -0500778 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200779}
780
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400781WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500782weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400783{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500784 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400785
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400786 pixman_region32_union(&compositor->primary_plane.damage,
787 &compositor->primary_plane.damage,
788 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400789 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400790}
791
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400792static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500793fade_frame(struct weston_animation *animation,
794 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400795{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500796 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400797 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500798 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500799 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400800
Scott Moreau94b0b0c2012-06-14 01:01:56 -0600801 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -0600802 compositor->fade.spring.timestamp = msecs;
803
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500804 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500805 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500806 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
807 compositor->fade.spring.current);
808 weston_surface_damage(surface);
809
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500810 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400811 compositor->fade.spring.current =
812 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400813 wl_list_remove(&animation->link);
814 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200815
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500816 if (compositor->fade.spring.current < 0.001) {
817 destroy_surface(&surface->surface.resource);
818 compositor->fade.surface = NULL;
819 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500820 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400821 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200822 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400823 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400824}
825
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400826static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400827surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400828 pixman_region32_t *opaque)
829{
830 if (surface->buffer && wl_buffer_is_shm(surface->buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400831 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400832
833 if (surface->transform.enabled) {
834 pixman_box32_t *extents;
835
836 extents = pixman_region32_extents(&surface->damage);
837 surface_compute_bbox(surface, extents->x1, extents->y1,
838 extents->x2 - extents->x1,
839 extents->y2 - extents->y1,
840 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400841 pixman_region32_translate(&surface->damage,
842 -surface->plane->x,
843 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400844 } else {
845 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400846 surface->geometry.x - surface->plane->x,
847 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400848 }
849
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400850 pixman_region32_union(&surface->plane->damage,
851 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400852 empty_region(&surface->damage);
853 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400854 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400855}
856
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400857static void
Rob Bradford0fb79752012-08-02 15:36:57 +0100858weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400859{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500860 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500861 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500862 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -0500863 struct weston_animation *animation, *next;
864 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +0200865 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300866 pixman_region32_t opaque, output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500867
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +0200868 weston_compositor_update_drag_surfaces(ec);
869
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500870 /* Rebuild the surface list and update surface transforms up front. */
871 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +0200872 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500873 wl_list_for_each(layer, &ec->layer_list, link) {
874 wl_list_for_each(es, &layer->surface_list, layer_link) {
875 weston_surface_update_transform(es);
876 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +0200877 if (es->output == output) {
878 wl_list_insert_list(&frame_callback_list,
879 &es->frame_callback_list);
880 wl_list_init(&es->frame_callback_list);
881 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500882 }
883 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +0200884
Kristian Høgsberg79af73e2012-08-03 15:45:23 -0400885 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800886 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -0400887 else
888 wl_list_for_each(es, &ec->surface_list, link)
889 weston_surface_move_to_plane(es, &ec->primary_plane);
890
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500891 pixman_region32_init(&opaque);
892
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400893 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400894 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500895
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400896 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -0400897
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400898 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400899 pixman_region32_intersect(&output_damage,
900 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300901 pixman_region32_subtract(&ec->primary_plane.damage,
902 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400903
Scott Moreauccbf29d2012-02-22 14:21:41 -0700904 if (output->dirty)
905 weston_output_update_matrix(output);
906
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400907 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400908
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500909 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500910
Kristian Høgsbergef044142011-06-21 15:02:12 -0400911 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100912
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -0400913 weston_compositor_repick(ec);
914 wl_event_loop_dispatch(ec->input_loop, 0);
915
Jonas Ådahldb773762012-06-13 00:01:21 +0200916 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500917 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400918 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500919 }
Jonas Ådahldb773762012-06-13 00:01:21 +0200920 wl_list_init(&frame_callback_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500921
Scott Moreaud64cf212012-06-08 19:40:54 -0600922 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -0600923 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -0600924 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -0600925 }
Kristian Høgsbergef044142011-06-21 15:02:12 -0400926}
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400927
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -0500928static int
929weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -0400930{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -0500931 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -0500932
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -0500933 wl_event_loop_dispatch(compositor->input_loop, 0);
934
935 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -0400936}
937
938WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +0100939weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -0400940{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -0500941 struct weston_compositor *compositor = output->compositor;
942 struct wl_event_loop *loop =
943 wl_display_get_event_loop(compositor->wl_display);
944 int fd;
945
Kristian Høgsberge9d04922012-06-19 23:54:26 -0400946 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -0500947 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -0500948 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -0500949 return;
950 }
951
952 output->repaint_scheduled = 0;
953 if (compositor->input_loop_source)
954 return;
955
956 fd = wl_event_loop_get_fd(compositor->input_loop);
957 compositor->input_loop_source =
958 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
959 weston_compositor_read_input, compositor);
960}
961
962static void
963idle_repaint(void *data)
964{
965 struct weston_output *output = data;
966
967 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400968}
969
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400970WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500971weston_layer_init(struct weston_layer *layer, struct wl_list *below)
972{
973 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200974 if (below != NULL)
975 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500976}
977
978WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400979weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400980{
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400981 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -0400982 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100983
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500984 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400985 return;
986
Kristian Høgsbergef044142011-06-21 15:02:12 -0400987 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400988 output->repaint_needed = 1;
989 if (output->repaint_scheduled)
990 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100991
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400992 wl_event_loop_add_idle(loop, idle_repaint, output);
993 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -0500994
995 if (compositor->input_loop_source) {
996 wl_event_source_remove(compositor->input_loop_source);
997 compositor->input_loop_source = NULL;
998 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400999}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001000
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001001WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001002weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1003{
1004 struct weston_output *output;
1005
1006 wl_list_for_each(output, &compositor->output_list, link)
1007 weston_output_schedule_repaint(output);
1008}
1009
1010WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001011weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001012{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001013 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001014 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001015
Scott Moreau9d1b1122012-06-08 19:40:53 -06001016 output = container_of(compositor->output_list.next,
1017 struct weston_output, link);
1018
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001019 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001020 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001021 return;
1022
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001023 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001024 surface = weston_surface_create(compositor);
1025 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001026 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001027 wl_list_insert(&compositor->fade_layer.surface_list,
1028 &surface->layer_link);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001029 weston_surface_assign_output(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001030 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001031 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001032 }
1033
1034 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001035 if (wl_list_empty(&compositor->fade.animation.link)) {
1036 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001037 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001038 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001039 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001040}
1041
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001042static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001043surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001044{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001045 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001046}
1047
Casey Dahlin96d8a752012-04-19 22:50:07 -04001048static struct wl_resource *
1049find_resource_for_client(struct wl_list *list, struct wl_client *client)
1050{
1051 struct wl_resource *r;
1052
1053 wl_list_for_each(r, list, link) {
1054 if (r->client == client)
1055 return r;
1056 }
1057
1058 return NULL;
1059}
1060
Casey Dahlin9074db52012-04-19 22:50:09 -04001061static void
1062weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1063{
1064 uint32_t different = es->output_mask ^ mask;
1065 uint32_t entered = mask & different;
1066 uint32_t left = es->output_mask & different;
1067 struct weston_output *output;
Rob Bradford8667e772012-05-18 14:13:03 +01001068 struct wl_resource *resource = NULL;
Casey Dahlin9074db52012-04-19 22:50:09 -04001069 struct wl_client *client = es->surface.resource.client;
1070
Scott Moreaua5021522012-08-03 17:11:51 -06001071 es->output_mask = mask;
Casey Dahlin9074db52012-04-19 22:50:09 -04001072 if (es->surface.resource.client == NULL)
1073 return;
1074 if (different == 0)
1075 return;
1076
Casey Dahlin9074db52012-04-19 22:50:09 -04001077 wl_list_for_each(output, &es->compositor->output_list, link) {
1078 if (1 << output->id & different)
1079 resource =
1080 find_resource_for_client(&output->resource_list,
1081 client);
Kristian Høgsbergc7814d22012-07-12 12:34:43 -04001082 if (resource == NULL)
1083 continue;
Casey Dahlin9074db52012-04-19 22:50:09 -04001084 if (1 << output->id & entered)
1085 wl_surface_send_enter(&es->surface.resource, resource);
1086 if (1 << output->id & left)
1087 wl_surface_send_leave(&es->surface.resource, resource);
1088 }
1089}
1090
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001091WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001092weston_surface_assign_output(struct weston_surface *es)
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001093{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001094 struct weston_compositor *ec = es->compositor;
1095 struct weston_output *output, *new_output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001096 pixman_region32_t region;
Casey Dahlin9074db52012-04-19 22:50:09 -04001097 uint32_t max, area, mask;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001098 pixman_box32_t *e;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001099
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001100 new_output = NULL;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001101 max = 0;
Casey Dahlin9074db52012-04-19 22:50:09 -04001102 mask = 0;
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001103 pixman_region32_init(&region);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001104 wl_list_for_each(output, &ec->output_list, link) {
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001105 pixman_region32_intersect(&region, &es->transform.boundingbox,
1106 &output->region);
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001107
1108 e = pixman_region32_extents(&region);
1109 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1110
Casey Dahlin9074db52012-04-19 22:50:09 -04001111 if (area > 0)
1112 mask |= 1 << output->id;
1113
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001114 if (area >= max) {
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001115 new_output = output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001116 max = area;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001117 }
1118 }
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001119 pixman_region32_fini(&region);
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001120
1121 es->output = new_output;
Casey Dahlin9074db52012-04-19 22:50:09 -04001122 weston_surface_update_output_mask(es, mask);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001123}
1124
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001125static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001126surface_attach(struct wl_client *client,
1127 struct wl_resource *resource,
1128 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1129{
1130 struct weston_surface *es = resource->data;
1131 struct wl_buffer *buffer = NULL;
1132
1133 if (buffer_resource)
1134 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001135
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -04001136 weston_surface_attach(&es->surface, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001137
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001138 if (buffer && es->configure)
1139 es->configure(es, sx, sy);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001140}
1141
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001142static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001143surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001144 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001145 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001146{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001147 struct weston_surface *es = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001148
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001149 pixman_region32_union_rect(&es->damage, &es->damage,
1150 x, y, width, height);
Kristian Høgsberg98238702012-08-03 16:29:12 -04001151 weston_surface_schedule_repaint(es);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001152}
1153
Kristian Høgsberg33418202011-08-16 23:01:28 -04001154static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001155destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001156{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001157 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001158
1159 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001160 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001161}
1162
1163static void
1164surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001165 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001166{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001167 struct weston_frame_callback *cb;
1168 struct weston_surface *es = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001169
1170 cb = malloc(sizeof *cb);
1171 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001172 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001173 return;
1174 }
1175
1176 cb->resource.object.interface = &wl_callback_interface;
1177 cb->resource.object.id = callback;
1178 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001179 cb->resource.client = client;
1180 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001181
1182 wl_client_add_resource(client, &cb->resource);
Jonas Ådahldb773762012-06-13 00:01:21 +02001183 wl_list_insert(es->frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001184}
1185
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001186static void
1187surface_set_opaque_region(struct wl_client *client,
1188 struct wl_resource *resource,
1189 struct wl_resource *region_resource)
1190{
1191 struct weston_surface *surface = resource->data;
1192 struct weston_region *region;
1193
1194 pixman_region32_fini(&surface->opaque);
1195
1196 if (region_resource) {
1197 region = region_resource->data;
1198 pixman_region32_init_rect(&surface->opaque, 0, 0,
1199 surface->geometry.width,
1200 surface->geometry.height);
1201 pixman_region32_intersect(&surface->opaque,
1202 &surface->opaque, &region->region);
1203 } else {
1204 pixman_region32_init(&surface->opaque);
1205 }
1206
1207 surface->geometry.dirty = 1;
1208}
1209
1210static void
1211surface_set_input_region(struct wl_client *client,
1212 struct wl_resource *resource,
1213 struct wl_resource *region_resource)
1214{
1215 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001216 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001217
1218 if (region_resource) {
1219 region = region_resource->data;
1220 pixman_region32_init_rect(&surface->input, 0, 0,
1221 surface->geometry.width,
1222 surface->geometry.height);
1223 pixman_region32_intersect(&surface->input,
1224 &surface->input, &region->region);
1225 } else {
1226 pixman_region32_init_rect(&surface->input, 0, 0,
1227 surface->geometry.width,
1228 surface->geometry.height);
1229 }
1230
Kristian Høgsberg98238702012-08-03 16:29:12 -04001231 weston_surface_schedule_repaint(surface);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001232}
1233
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001234static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001235 surface_destroy,
1236 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001237 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001238 surface_frame,
1239 surface_set_opaque_region,
1240 surface_set_input_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001241};
1242
1243static void
1244compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001245 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001246{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001247 struct weston_compositor *ec = resource->data;
1248 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001249
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001250 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001251 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001252 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001253 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001254 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001255
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001256 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001257
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001258 surface->surface.resource.object.id = id;
1259 surface->surface.resource.object.interface = &wl_surface_interface;
1260 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001261 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001262 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001263
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001264 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001265}
1266
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001267static void
1268destroy_region(struct wl_resource *resource)
1269{
1270 struct weston_region *region =
1271 container_of(resource, struct weston_region, resource);
1272
1273 pixman_region32_fini(&region->region);
1274 free(region);
1275}
1276
1277static void
1278region_destroy(struct wl_client *client, struct wl_resource *resource)
1279{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001280 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001281}
1282
1283static void
1284region_add(struct wl_client *client, struct wl_resource *resource,
1285 int32_t x, int32_t y, int32_t width, int32_t height)
1286{
1287 struct weston_region *region = resource->data;
1288
1289 pixman_region32_union_rect(&region->region, &region->region,
1290 x, y, width, height);
1291}
1292
1293static void
1294region_subtract(struct wl_client *client, struct wl_resource *resource,
1295 int32_t x, int32_t y, int32_t width, int32_t height)
1296{
1297 struct weston_region *region = resource->data;
1298 pixman_region32_t rect;
1299
1300 pixman_region32_init_rect(&rect, x, y, width, height);
1301 pixman_region32_subtract(&region->region, &region->region, &rect);
1302 pixman_region32_fini(&rect);
1303}
1304
1305static const struct wl_region_interface region_interface = {
1306 region_destroy,
1307 region_add,
1308 region_subtract
1309};
1310
1311static void
1312compositor_create_region(struct wl_client *client,
1313 struct wl_resource *resource, uint32_t id)
1314{
1315 struct weston_region *region;
1316
1317 region = malloc(sizeof *region);
1318 if (region == NULL) {
1319 wl_resource_post_no_memory(resource);
1320 return;
1321 }
1322
1323 region->resource.destroy = destroy_region;
1324
1325 region->resource.object.id = id;
1326 region->resource.object.interface = &wl_region_interface;
1327 region->resource.object.implementation =
1328 (void (**)(void)) &region_interface;
1329 region->resource.data = region;
1330
1331 pixman_region32_init(&region->region);
1332
1333 wl_client_add_resource(client, &region->resource);
1334}
1335
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001336static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001337 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001338 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001339};
1340
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001341WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001342weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001343{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001344 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1345 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001346
1347 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001348 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001349}
1350
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001351static void
1352weston_compositor_dpms_on(struct weston_compositor *compositor)
1353{
1354 struct weston_output *output;
1355
1356 wl_list_for_each(output, &compositor->output_list, link)
1357 if (output->set_dpms)
1358 output->set_dpms(output, WESTON_DPMS_ON);
1359}
1360
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001361WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001362weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001363{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001364 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1365 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001366 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001367 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001368 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001369 }
1370}
1371
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001372static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001373weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001374{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001375 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001376 compositor->idle_inhibit++;
1377}
1378
1379static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001380weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001381{
1382 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001383 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001384}
1385
1386static int
1387idle_handler(void *data)
1388{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001389 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001390
1391 if (compositor->idle_inhibit)
1392 return 1;
1393
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001394 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001395
1396 return 1;
1397}
1398
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001399WL_EXPORT void
1400weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1401{
1402 pixman_region32_init(&plane->damage);
1403 plane->x = x;
1404 plane->y = y;
1405}
1406
1407WL_EXPORT void
1408weston_plane_release(struct weston_plane *plane)
1409{
1410 pixman_region32_fini(&plane->damage);
1411}
1412
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001413static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001414weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001415
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001416static void
Daniel Stone37816df2012-05-16 18:45:18 +01001417clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001418{
Daniel Stone37816df2012-05-16 18:45:18 +01001419 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001420 struct weston_output *output, *prev = NULL;
1421 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001422
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001423 x = wl_fixed_to_int(*fx);
1424 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001425 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1426 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001427
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001428 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001429 if (pixman_region32_contains_point(&output->region,
1430 x, y, NULL))
1431 valid = 1;
1432 if (pixman_region32_contains_point(&output->region,
1433 old_x, old_y, NULL))
1434 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001435 }
Daniel Stone37816df2012-05-16 18:45:18 +01001436
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001437 if (!valid) {
1438 if (x < prev->x)
1439 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001440 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001441 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001442 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001443 if (y < prev->y)
1444 *fy = wl_fixed_from_int(prev->y);
1445 else if (y >= prev->y + prev->current->height)
1446 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001447 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001448 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001449}
1450
1451WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001452notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001453{
1454 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001455 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001456 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001457 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001458 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001459
1460 weston_compositor_activity(ec);
1461
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001462 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001463
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001464 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001465
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001466 pointer->x = x;
1467 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001468
1469 ix = wl_fixed_to_int(x);
1470 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001471
Scott Moreauccbf29d2012-02-22 14:21:41 -07001472 wl_list_for_each(output, &ec->output_list, link)
1473 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001474 pixman_region32_contains_point(&output->region,
1475 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06001476 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001477
Daniel Stone37816df2012-05-16 18:45:18 +01001478 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001479 interface = pointer->grab->interface;
1480 interface->motion(pointer->grab, time,
1481 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001482
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001483 if (seat->sprite) {
1484 weston_surface_set_position(seat->sprite,
1485 ix - seat->hotspot_x,
1486 iy - seat->hotspot_y);
1487 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001488 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001489}
1490
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001491WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001492weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001493 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001494{
Daniel Stone37816df2012-05-16 18:45:18 +01001495 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001496
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001497 if (seat->seat.keyboard) {
1498 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1499 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04001500 }
1501
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001502 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001503}
1504
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001505WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001506notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001507 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001508{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001509 struct weston_compositor *compositor = seat->compositor;
1510 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001511 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001512 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001513 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001514
Daniel Stone4dbadb12012-05-30 16:31:51 +01001515 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001516 if (compositor->ping_handler && focus)
1517 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001518 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001519 if (pointer->button_count == 0) {
1520 pointer->grab_button = button;
1521 pointer->grab_time = time;
1522 pointer->grab_x = pointer->x;
1523 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001524 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001525 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001526 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001527 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001528 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001529 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001530
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001531 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001532 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001533
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001534 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001535
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001536 if (pointer->button_count == 1)
1537 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001538 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001539}
1540
Scott Moreau210d0792012-03-22 10:47:01 -06001541WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001542notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01001543 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001544{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001545 struct weston_compositor *compositor = seat->compositor;
1546 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001547 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001548 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001549 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1550
1551 if (compositor->ping_handler && focus)
1552 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001553
1554 weston_compositor_activity(compositor);
1555
Scott Moreau6a3633d2012-03-20 08:47:59 -06001556 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001557 weston_compositor_run_axis_binding(compositor, seat,
1558 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06001559 else
1560 return;
1561
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001562 if (pointer->focus_resource)
1563 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01001564 value);
Scott Moreau210d0792012-03-22 10:47:01 -06001565}
1566
Daniel Stone05d58682012-06-22 13:21:38 +01001567WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001568notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001569{
Daniel Stone05d58682012-06-22 13:21:38 +01001570 struct wl_keyboard *keyboard = &seat->keyboard;
1571 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01001572 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001573 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01001574 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01001575 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01001576
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001577 /* Serialize and update our internal state, checking to see if it's
1578 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01001579 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1580 XKB_STATE_DEPRESSED);
1581 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1582 XKB_STATE_LATCHED);
1583 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1584 XKB_STATE_LOCKED);
1585 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01001586 XKB_STATE_EFFECTIVE);
1587
Daniel Stone71c38772012-06-22 13:21:30 +01001588 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
1589 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
1590 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
1591 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01001592 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01001593
Daniel Stone71c38772012-06-22 13:21:30 +01001594 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
1595 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
1596 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
1597 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001598
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001599 /* And update the modifier_state for bindings. */
1600 mods_lookup = mods_depressed | mods_latched;
1601 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01001602 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001603 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01001604 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001605 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01001606 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001607 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04001608 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
1609 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01001610
Daniel Stone8c8164f2012-05-30 16:31:45 +01001611 /* Finally, notify the compositor that LEDs have changed. */
1612 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001613 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001614 leds |= LED_NUM_LOCK;
1615 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001616 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001617 leds |= LED_CAPS_LOCK;
1618 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001619 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001620 leds |= LED_SCROLL_LOCK;
1621 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01001622 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01001623 seat->xkb_state.leds = leds;
1624
Daniel Stone05d58682012-06-22 13:21:38 +01001625 if (changed) {
1626 grab->interface->modifiers(grab,
1627 serial,
1628 keyboard->modifiers.mods_depressed,
1629 keyboard->modifiers.mods_latched,
1630 keyboard->modifiers.mods_locked,
1631 keyboard->modifiers.group);
1632 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001633}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001634
Daniel Stone05d58682012-06-22 13:21:38 +01001635static void
1636update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001637 enum wl_keyboard_key_state state)
1638{
1639 enum xkb_key_direction direction;
1640
1641 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1642 direction = XKB_KEY_DOWN;
1643 else
1644 direction = XKB_KEY_UP;
1645
1646 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1647 * broken keycode system, which starts at 8. */
1648 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1649
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001650 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001651}
1652
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001653WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001654notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001655 enum wl_keyboard_key_state state,
1656 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001657{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001658 struct weston_compositor *compositor = seat->compositor;
1659 struct wl_keyboard *keyboard = seat->seat.keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01001660 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001661 (struct weston_surface *) keyboard->focus;
1662 struct wl_keyboard_grab *grab = keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001663 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001664 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001665
Daniel Stonec9785ea2012-05-30 16:31:52 +01001666 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001667 if (compositor->ping_handler && focus)
1668 compositor->ping_handler(focus, serial);
1669
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001670 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001671 keyboard->grab_key = key;
1672 keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07001673 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001674 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001675 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001676
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001677 end = keyboard->keys.data + keyboard->keys.size;
1678 for (k = keyboard->keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01001679 if (*k == key) {
1680 /* Ignore server-generated repeats. */
1681 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1682 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001683 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01001684 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001685 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001686 keyboard->keys.size = (void *) end - keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001687 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001688 k = wl_array_add(&keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001689 *k = key;
1690 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001691
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001692 if (grab == &keyboard->default_grab) {
1693 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001694 state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001695 grab = keyboard->grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06001696 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001697
Daniel Stone7ace3902012-05-30 16:31:40 +01001698 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01001699
1700 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001701 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01001702 wl_display_get_serial(compositor->wl_display),
1703 key,
1704 state);
1705 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001706}
1707
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001708WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001709notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01001710 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001711{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001712 struct weston_compositor *compositor = seat->compositor;
1713 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001714
1715 if (output) {
Daniel Stone37816df2012-05-16 18:45:18 +01001716 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001717 x - pointer->x,
1718 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001719
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001720 pointer->x = x;
1721 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001722 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001723 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001724 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001725 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03001726 /* FIXME: We should call wl_pointer_set_focus(seat,
1727 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001728 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001729}
1730
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001731static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001732destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001733{
Daniel Stone37816df2012-05-16 18:45:18 +01001734 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001735
Daniel Stone37816df2012-05-16 18:45:18 +01001736 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001737 saved_kbd_focus_listener);
1738
Daniel Stone37816df2012-05-16 18:45:18 +01001739 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001740}
1741
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001742WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001743notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001744 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001745{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001746 struct weston_compositor *compositor = seat->compositor;
1747 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001748 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001749 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001750
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001751 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001752 wl_array_copy(&keyboard->keys, keys);
1753 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001754 weston_compositor_idle_inhibit(compositor);
1755 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001756 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001757 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001758 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001759
Daniel Stoneef172672012-06-22 13:21:32 +01001760 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001761 wl_array_for_each(k, &keyboard->keys) {
1762 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01001763 WL_KEYBOARD_KEY_STATE_PRESSED);
1764 }
1765
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001766 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001767
1768 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001769 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1770 wl_keyboard_set_focus(keyboard, surface);
1771 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001772 }
1773}
1774
1775WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001776notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01001777{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001778 struct weston_compositor *compositor = seat->compositor;
1779 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001780 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001781
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001782 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001783 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001784 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001785 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001786 WL_KEYBOARD_KEY_STATE_RELEASED);
1787 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001788
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001789 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001790
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001791 if (keyboard->focus) {
1792 seat->saved_kbd_focus = keyboard->focus;
1793 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01001794 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001795 wl_signal_add(&keyboard->focus->resource.destroy_signal,
1796 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001797 }
1798
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001799 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001800 /* FIXME: We really need keyboard grab cancel here to
1801 * let the grab shut down properly. As it is we leak
1802 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001803 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001804}
1805
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001806static void
Daniel Stone37816df2012-05-16 18:45:18 +01001807touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001808{
Daniel Stone37816df2012-05-16 18:45:18 +01001809 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001810 struct wl_resource *resource;
1811
Pekka Paalanen56464252012-07-31 13:21:08 +03001812 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001813 return;
1814
Pekka Paalanen56464252012-07-31 13:21:08 +03001815 if (seat->touch->focus_resource)
1816 wl_list_remove(&seat->touch->focus_listener.link);
1817 seat->touch->focus = NULL;
1818 seat->touch->focus_resource = NULL;
1819
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001820 if (surface) {
1821 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01001822 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04001823 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001824 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02001825 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001826 return;
1827 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001828
Daniel Stone37816df2012-05-16 18:45:18 +01001829 seat->touch->focus = surface;
1830 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03001831 wl_signal_add(&resource->destroy_signal,
1832 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001833 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001834}
1835
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001836/**
1837 * notify_touch - emulates button touches and notifies surfaces accordingly.
1838 *
1839 * It assumes always the correct cycle sequence until it gets here: touch_down
1840 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1841 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001842 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001843 */
1844WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001845notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001846 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001847{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001848 struct weston_compositor *ec = seat->compositor;
1849 struct wl_touch *touch = seat->seat.touch;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001850 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001851 wl_fixed_t sx, sy;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001852 uint32_t serial = 0;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001853
1854 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01001855 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001856 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001857
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001858 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001859
1860 /* the first finger down picks the surface, and all further go
1861 * to that surface for the remainder of the touch session i.e.
1862 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001863 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001864 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001865 touch_set_focus(seat, &es->surface);
1866 } else if (touch->focus) {
1867 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001868 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001869 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001870
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001871 if (touch->focus_resource && touch->focus)
1872 wl_touch_send_down(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001873 serial, time,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001874 &touch->focus->resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001875 touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001876 break;
Daniel Stone37816df2012-05-16 18:45:18 +01001877 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001878 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001879 if (!es)
1880 break;
1881
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001882 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001883 if (touch->focus_resource)
1884 wl_touch_send_motion(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001885 time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001886 break;
Daniel Stone37816df2012-05-16 18:45:18 +01001887 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001888 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001889 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001890
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001891 if (touch->focus_resource)
1892 wl_touch_send_up(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001893 serial, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001894 if (seat->num_tp == 0)
1895 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001896 break;
1897 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001898}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001899
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001900static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001901pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1902{
1903 struct weston_seat *seat = container_of(listener, struct weston_seat,
1904 sprite_destroy_listener);
1905
1906 seat->sprite = NULL;
1907}
1908
1909static void
1910pointer_cursor_surface_configure(struct weston_surface *es,
1911 int32_t dx, int32_t dy)
1912{
1913 struct weston_seat *seat = es->private;
1914 int x, y;
1915
1916 assert(es == seat->sprite);
1917
1918 seat->hotspot_x -= dx;
1919 seat->hotspot_y -= dy;
1920
1921 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
1922 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
1923
1924 weston_surface_configure(seat->sprite, x, y,
1925 es->buffer->width, es->buffer->height);
1926
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03001927 empty_region(&es->input);
1928
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001929 if (!weston_surface_is_mapped(es)) {
1930 wl_list_insert(&es->compositor->cursor_layer.surface_list,
1931 &es->layer_link);
1932 weston_surface_assign_output(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001933 }
1934}
1935
1936static void
1937pointer_unmap_sprite(struct weston_seat *seat)
1938{
1939 if (weston_surface_is_mapped(seat->sprite))
1940 weston_surface_unmap(seat->sprite);
1941
1942 wl_list_remove(&seat->sprite_destroy_listener.link);
1943 seat->sprite->configure = NULL;
1944 seat->sprite->private = NULL;
1945 seat->sprite = NULL;
1946}
1947
1948static void
1949pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1950 uint32_t serial, struct wl_resource *surface_resource,
1951 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001952{
Daniel Stone37816df2012-05-16 18:45:18 +01001953 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001954 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001955
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001956 if (surface_resource)
1957 surface = container_of(surface_resource->data,
1958 struct weston_surface, surface);
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04001959
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04001960 if (seat->seat.pointer->focus == NULL)
1961 return;
1962 if (seat->seat.pointer->focus->resource.client != client)
1963 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04001964 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001965 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03001966
1967 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03001968 if (surface->configure) {
1969 wl_resource_post_error(&surface->surface.resource,
1970 WL_DISPLAY_ERROR_INVALID_OBJECT,
1971 "surface->configure already "
1972 "set");
1973 return;
1974 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001975 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001976
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001977 if (seat->sprite)
1978 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001979
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001980 if (!surface)
1981 return;
1982
1983 wl_signal_add(&surface->surface.resource.destroy_signal,
1984 &seat->sprite_destroy_listener);
1985
1986 surface->configure = pointer_cursor_surface_configure;
1987 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001988 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01001989 seat->hotspot_x = x;
1990 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001991
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04001992 if (surface->buffer)
1993 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001994}
1995
Daniel Stone37816df2012-05-16 18:45:18 +01001996static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001997 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001998};
1999
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002000static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002001handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002002{
Daniel Stone37816df2012-05-16 18:45:18 +01002003 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002004
Daniel Stone37816df2012-05-16 18:45:18 +01002005 seat = container_of(listener, struct weston_seat,
2006 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002007
Daniel Stone37816df2012-05-16 18:45:18 +01002008 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002009}
2010
Casey Dahlin9074db52012-04-19 22:50:09 -04002011static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002012{
2013 wl_list_remove(&resource->link);
2014 free(resource);
2015}
2016
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002017static void
Daniel Stone37816df2012-05-16 18:45:18 +01002018seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2019 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002020{
Daniel Stone37816df2012-05-16 18:45:18 +01002021 struct weston_seat *seat = resource->data;
2022 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002023
Daniel Stone37816df2012-05-16 18:45:18 +01002024 if (!seat->seat.pointer)
2025 return;
2026
2027 cr = wl_client_add_object(client, &wl_pointer_interface,
2028 &pointer_interface, id, seat);
2029 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002030 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002031
2032 if (seat->seat.pointer->focus &&
2033 seat->seat.pointer->focus->resource.client == client) {
2034 struct weston_surface *surface;
2035 wl_fixed_t sx, sy;
2036
2037 surface = (struct weston_surface *) seat->seat.pointer->focus;
2038 weston_surface_from_global_fixed(surface,
2039 seat->seat.pointer->x,
2040 seat->seat.pointer->y,
2041 &sx,
2042 &sy);
2043 wl_pointer_set_focus(seat->seat.pointer,
2044 seat->seat.pointer->focus,
2045 sx,
2046 sy);
2047 }
Daniel Stone37816df2012-05-16 18:45:18 +01002048}
2049
2050static void
2051seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2052 uint32_t id)
2053{
2054 struct weston_seat *seat = resource->data;
2055 struct wl_resource *cr;
2056
2057 if (!seat->seat.keyboard)
2058 return;
2059
2060 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2061 seat);
2062 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002063 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002064
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002065 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2066 seat->xkb_info.keymap_fd,
2067 seat->xkb_info.keymap_size);
2068
Daniel Stone17b13bd2012-05-30 16:31:39 +01002069 if (seat->seat.keyboard->focus &&
2070 seat->seat.keyboard->focus->resource.client == client) {
2071 wl_keyboard_set_focus(seat->seat.keyboard,
2072 seat->seat.keyboard->focus);
2073 }
Daniel Stone37816df2012-05-16 18:45:18 +01002074}
2075
2076static void
2077seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2078 uint32_t id)
2079{
2080 struct weston_seat *seat = resource->data;
2081 struct wl_resource *cr;
2082
2083 if (!seat->seat.touch)
2084 return;
2085
2086 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2087 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002088 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002089}
2090
2091static const struct wl_seat_interface seat_interface = {
2092 seat_get_pointer,
2093 seat_get_keyboard,
2094 seat_get_touch,
2095};
2096
2097static void
2098bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2099{
2100 struct wl_seat *seat = data;
2101 struct wl_resource *resource;
2102 enum wl_seat_capability caps = 0;
2103
2104 resource = wl_client_add_object(client, &wl_seat_interface,
2105 &seat_interface, id, data);
2106 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002107 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002108
2109 if (seat->pointer)
2110 caps |= WL_SEAT_CAPABILITY_POINTER;
2111 if (seat->keyboard)
2112 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2113 if (seat->touch)
2114 caps |= WL_SEAT_CAPABILITY_TOUCH;
2115
2116 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002117}
2118
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002119static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002120device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002121{
Daniel Stone37816df2012-05-16 18:45:18 +01002122 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002123
Daniel Stone37816df2012-05-16 18:45:18 +01002124 seat = container_of(listener, struct weston_seat,
2125 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002126
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002127 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002128}
2129
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002130static void weston_compositor_xkb_init(struct weston_compositor *ec,
2131 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002132{
Daniel Stonee379da92012-05-30 16:32:04 +01002133 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002134 ec->xkb_context = xkb_context_new(0);
2135 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002136 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002137 exit(1);
2138 }
Daniel Stone994679a2012-05-30 16:31:43 +01002139 }
2140
2141 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002142 ec->xkb_names = *names;
2143 if (!ec->xkb_names.rules)
2144 ec->xkb_names.rules = strdup("evdev");
2145 if (!ec->xkb_names.model)
2146 ec->xkb_names.model = strdup("pc105");
2147 if (!ec->xkb_names.layout)
2148 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002149}
2150
Daniel Stonee379da92012-05-30 16:32:04 +01002151static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2152{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002153 if (xkb_info->keymap)
2154 xkb_map_unref(xkb_info->keymap);
2155
2156 if (xkb_info->keymap_area)
2157 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2158 if (xkb_info->keymap_fd >= 0)
2159 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002160}
2161
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002162static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2163{
Daniel Stonee379da92012-05-30 16:32:04 +01002164 free((char *) ec->xkb_names.rules);
2165 free((char *) ec->xkb_names.model);
2166 free((char *) ec->xkb_names.layout);
2167 free((char *) ec->xkb_names.variant);
2168 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002169
Daniel Stonee379da92012-05-30 16:32:04 +01002170 xkb_info_destroy(&ec->xkb_info);
2171 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002172}
2173
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002174static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002175weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002176{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002177 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002178
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002179 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2180 XKB_MOD_NAME_SHIFT);
2181 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2182 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002183 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2184 XKB_MOD_NAME_CTRL);
2185 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2186 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002187 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2188 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002189 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2190 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002191 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002192
2193 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2194 XKB_LED_NAME_NUM);
2195 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2196 XKB_LED_NAME_CAPS);
2197 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2198 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002199
2200 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2201 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002202 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002203 exit(EXIT_FAILURE);
2204 }
2205 xkb_info->keymap_size = strlen(keymap_str) + 1;
2206
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002207 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002208 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002209 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002210 (unsigned long) xkb_info->keymap_size);
2211 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002212 }
2213
2214 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2215 PROT_READ | PROT_WRITE,
2216 MAP_SHARED, xkb_info->keymap_fd, 0);
2217 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002218 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002219 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002220 goto err_dev_zero;
2221 }
2222 strcpy(xkb_info->keymap_area, keymap_str);
2223 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002224
2225 return;
2226
2227err_dev_zero:
2228 close(xkb_info->keymap_fd);
2229 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002230err_keymap_str:
2231 free(keymap_str);
2232 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002233}
2234
2235static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002236weston_compositor_build_global_keymap(struct weston_compositor *ec)
2237{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002238 if (ec->xkb_info.keymap != NULL)
2239 return;
2240
Daniel Stonee379da92012-05-30 16:32:04 +01002241 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002242 &ec->xkb_names,
2243 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002244 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002245 weston_log("failed to compile global XKB keymap\n");
2246 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002247 "options %s",
2248 ec->xkb_names.rules, ec->xkb_names.model,
2249 ec->xkb_names.layout, ec->xkb_names.variant,
2250 ec->xkb_names.options);
2251 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002252 }
2253
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002254 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002255}
2256
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002257WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002258weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002259{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002260 if (seat->has_keyboard)
2261 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002262
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002263 if (keymap != NULL) {
2264 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002265 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002266 }
2267 else {
2268 weston_compositor_build_global_keymap(seat->compositor);
2269 seat->xkb_info = seat->compositor->xkb_info;
2270 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2271 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002272
2273 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2274 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002275 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01002276 exit(1);
2277 }
2278
Daniel Stone71c38772012-06-22 13:21:30 +01002279 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002280
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002281 wl_keyboard_init(&seat->keyboard);
2282 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2283
2284 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01002285}
2286
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002287WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002288weston_seat_init_pointer(struct weston_seat *seat)
2289{
2290 if (seat->has_pointer)
2291 return;
2292
2293 wl_pointer_init(&seat->pointer);
2294 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2295
2296 seat->has_pointer = 1;
2297}
2298
2299WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002300weston_seat_init_touch(struct weston_seat *seat)
2301{
2302 if (seat->has_touch)
2303 return;
2304
2305 wl_touch_init(&seat->touch);
2306 wl_seat_set_touch(&seat->seat, &seat->touch);
2307
2308 seat->has_touch = 1;
2309}
2310
2311WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002312weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002313{
Daniel Stone37816df2012-05-16 18:45:18 +01002314 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002315 seat->has_pointer = 0;
2316 seat->has_keyboard = 0;
2317 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002318
Daniel Stone37816df2012-05-16 18:45:18 +01002319 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2320 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002321
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002322 seat->sprite = NULL;
2323 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002324
Daniel Stone37816df2012-05-16 18:45:18 +01002325 seat->compositor = ec;
2326 seat->hotspot_x = 16;
2327 seat->hotspot_y = 16;
2328 seat->modifier_state = 0;
2329 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002330
Daniel Stone37816df2012-05-16 18:45:18 +01002331 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002332 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002333
Daniel Stone37816df2012-05-16 18:45:18 +01002334 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002335
Daniel Stone37816df2012-05-16 18:45:18 +01002336 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2337 wl_signal_add(&seat->seat.drag_icon_signal,
2338 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002339
2340 clipboard_create(seat);
Jan Arne Petersene829adc2012-08-10 16:47:22 +02002341 input_method_create(ec, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002342}
2343
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002344WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002345weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002346{
Daniel Stone37816df2012-05-16 18:45:18 +01002347 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002348 /* The global object is destroyed at wl_display_destroy() time. */
2349
Daniel Stone37816df2012-05-16 18:45:18 +01002350 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002351 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002352
Daniel Stone74419a22012-05-30 16:32:02 +01002353 if (seat->xkb_state.state != NULL)
2354 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01002355 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002356
Daniel Stone37816df2012-05-16 18:45:18 +01002357 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002358}
2359
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002360static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002361drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2362{
2363 weston_surface_configure(es,
2364 es->geometry.x + sx, es->geometry.y + sy,
2365 es->buffer->width, es->buffer->height);
2366}
2367
2368static int
Daniel Stone37816df2012-05-16 18:45:18 +01002369device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002370 struct weston_surface *surface)
2371{
Daniel Stone37816df2012-05-16 18:45:18 +01002372 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002373
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002374 if (surface->configure) {
2375 wl_resource_post_error(&surface->surface.resource,
2376 WL_DISPLAY_ERROR_INVALID_OBJECT,
2377 "surface->configure already set");
2378 return 0;
2379 }
2380
Daniel Stone37816df2012-05-16 18:45:18 +01002381 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002382
Daniel Stone37816df2012-05-16 18:45:18 +01002383 weston_surface_set_position(ws->drag_surface,
2384 wl_fixed_to_double(seat->pointer->x),
2385 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002386
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002387 surface->configure = drag_surface_configure;
2388
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002389 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002390 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002391
2392 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002393}
2394
2395static void
Daniel Stone37816df2012-05-16 18:45:18 +01002396device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002397{
Daniel Stone37816df2012-05-16 18:45:18 +01002398 seat->drag_surface->configure = NULL;
2399 undef_region(&seat->drag_surface->input);
2400 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2401 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002402}
2403
2404static void
Daniel Stone37816df2012-05-16 18:45:18 +01002405device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002406{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002407 struct wl_list *list;
2408
Daniel Stone37816df2012-05-16 18:45:18 +01002409 if (weston_surface_is_mapped(seat->drag_surface) ||
2410 !seat->drag_surface->buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002411 return;
2412
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002413 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2414 list = &seat->sprite->layer_link;
2415 else
2416 list = &seat->compositor->cursor_layer.surface_list;
2417
2418 wl_list_insert(list, &seat->drag_surface->layer_link);
Daniel Stone37816df2012-05-16 18:45:18 +01002419 weston_surface_assign_output(seat->drag_surface);
2420 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002421}
2422
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002423static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002424weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01002425 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002426{
2427 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002428
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002429 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002430 return;
2431
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002432 if (seat->drag_surface && seat->seat.drag_surface &&
2433 (&seat->drag_surface->surface.resource !=
2434 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002435 /* between calls to this funcion we got a new drag_surface */
2436 surface_changed = 1;
2437
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002438 if (!seat->seat.drag_surface || surface_changed) {
2439 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002440 if (!surface_changed)
2441 return;
2442 }
2443
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002444 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002445 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002446 seat->seat.drag_surface;
2447 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002448 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002449 }
2450
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002451 /* the client may not have attached a buffer to the drag surface
2452 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002453 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002454
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02002455 /* the client may have attached a buffer with a different size to
2456 * the drag surface, causing the input region to be reset */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002457 if (region_is_undefined(&seat->drag_surface->input))
2458 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02002459
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002460 if (!dx && !dy)
2461 return;
2462
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002463 weston_surface_set_position(seat->drag_surface,
2464 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
2465 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002466}
2467
2468WL_EXPORT void
2469weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2470{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002471 struct weston_seat *seat;
2472
2473 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002474 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002475}
2476
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002477static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002478bind_output(struct wl_client *client,
2479 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002480{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002481 struct weston_output *output = data;
2482 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002483 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002484
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002485 resource = wl_client_add_object(client,
2486 &wl_output_interface, NULL, id, data);
2487
Casey Dahlin9074db52012-04-19 22:50:09 -04002488 wl_list_insert(&output->resource_list, &resource->link);
2489 resource->destroy = unbind_resource;
2490
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002491 wl_output_send_geometry(resource,
2492 output->x,
2493 output->y,
2494 output->mm_width,
2495 output->mm_height,
2496 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002497 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002498 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002499
2500 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002501 wl_output_send_mode(resource,
2502 mode->flags,
2503 mode->width,
2504 mode->height,
2505 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002506 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002507}
2508
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002509WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002510weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002511{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002512 struct weston_compositor *c = output->compositor;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002513 int i;
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002514
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002515 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002516
2517 for (i = 0; i < 2; i++)
2518 pixman_region32_fini(&output->buffer_damage[i]);
2519
Casey Dahlin9074db52012-04-19 22:50:09 -04002520 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002521
2522 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002523}
2524
Scott Moreau1bad5db2012-08-18 01:04:05 -06002525static void
2526weston_output_compute_transform(struct weston_output *output)
2527{
2528 struct weston_matrix transform;
2529 int flip;
2530
2531 weston_matrix_init(&transform);
2532
2533 switch(output->transform) {
2534 case WL_OUTPUT_TRANSFORM_FLIPPED:
2535 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2536 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2537 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2538 flip = -1;
2539 break;
2540 default:
2541 flip = 1;
2542 break;
2543 }
2544
2545 switch(output->transform) {
2546 case WL_OUTPUT_TRANSFORM_NORMAL:
2547 case WL_OUTPUT_TRANSFORM_FLIPPED:
2548 transform.d[0] = flip;
2549 transform.d[1] = 0;
2550 transform.d[4] = 0;
2551 transform.d[5] = 1;
2552 break;
2553 case WL_OUTPUT_TRANSFORM_90:
2554 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2555 transform.d[0] = 0;
2556 transform.d[1] = -flip;
2557 transform.d[4] = 1;
2558 transform.d[5] = 0;
2559 break;
2560 case WL_OUTPUT_TRANSFORM_180:
2561 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2562 transform.d[0] = -flip;
2563 transform.d[1] = 0;
2564 transform.d[4] = 0;
2565 transform.d[5] = -1;
2566 break;
2567 case WL_OUTPUT_TRANSFORM_270:
2568 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2569 transform.d[0] = 0;
2570 transform.d[1] = flip;
2571 transform.d[4] = -1;
2572 transform.d[5] = 0;
2573 break;
2574 default:
2575 break;
2576 }
2577
2578 weston_matrix_multiply(&output->matrix, &transform);
2579}
2580
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002581WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002582weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002583{
Scott Moreau850ca422012-05-21 15:21:25 -06002584 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002585 struct weston_matrix camera;
2586 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002587
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002588 weston_matrix_init(&output->matrix);
2589 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002590 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2591 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002592
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002593 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002594 2.0 / (output->width + output->border.left + output->border.right),
2595 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2596
2597 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002598
Scott Moreauccbf29d2012-02-22 14:21:41 -07002599 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002600 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002601 weston_matrix_init(&camera);
2602 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002603 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002604 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002605 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002606 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002607 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002608 weston_matrix_multiply(&output->matrix, &modelview);
2609 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002610
Scott Moreauccbf29d2012-02-22 14:21:41 -07002611 output->dirty = 0;
2612}
2613
Scott Moreau1bad5db2012-08-18 01:04:05 -06002614static void
2615weston_output_transform_init(struct weston_output *output, uint32_t transform)
2616{
2617 output->transform = transform;
2618
2619 switch (transform) {
2620 case WL_OUTPUT_TRANSFORM_90:
2621 case WL_OUTPUT_TRANSFORM_270:
2622 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2623 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2624 /* Swap width and height */
2625 output->width = output->current->height;
2626 output->height = output->current->width;
2627 break;
2628 case WL_OUTPUT_TRANSFORM_NORMAL:
2629 case WL_OUTPUT_TRANSFORM_180:
2630 case WL_OUTPUT_TRANSFORM_FLIPPED:
2631 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2632 output->width = output->current->width;
2633 output->height = output->current->height;
2634 break;
2635 default:
2636 break;
2637 }
2638}
2639
Scott Moreauccbf29d2012-02-22 14:21:41 -07002640WL_EXPORT void
2641weston_output_move(struct weston_output *output, int x, int y)
2642{
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002643 int i;
2644
Scott Moreauccbf29d2012-02-22 14:21:41 -07002645 output->x = x;
2646 output->y = y;
2647
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002648 output->current_buffer = 0;
2649 for (i = 0; i < 2; i++)
2650 pixman_region32_init(&output->buffer_damage[i]);
2651
Scott Moreauccbf29d2012-02-22 14:21:41 -07002652 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002653 output->width,
2654 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002655}
2656
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002657WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002658weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002659 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002660{
2661 output->compositor = c;
2662 output->x = x;
2663 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002664 output->border.top = 0;
2665 output->border.bottom = 0;
2666 output->border.left = 0;
2667 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002668 output->mm_width = width;
2669 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002670 output->dirty = 1;
2671
Scott Moreau1bad5db2012-08-18 01:04:05 -06002672 if (transform != WL_OUTPUT_TRANSFORM_NORMAL)
2673 output->disable_planes++;
2674
2675 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06002676 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002677
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002678 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002679 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002680
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002681 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002682 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002683 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002684
Casey Dahlin58ba1372012-04-19 22:50:08 -04002685 output->id = ffs(~output->compositor->output_id_pool) - 1;
2686 output->compositor->output_id_pool |= 1 << output->id;
2687
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002688 output->global =
2689 wl_display_add_global(c->wl_display, &wl_output_interface,
2690 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002691}
2692
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002693static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002694compositor_bind(struct wl_client *client,
2695 void *data, uint32_t version, uint32_t id)
2696{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002697 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002698
2699 wl_client_add_object(client, &wl_compositor_interface,
2700 &compositor_interface, id, compositor);
2701}
2702
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002703static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002704log_uname(void)
2705{
2706 struct utsname usys;
2707
2708 uname(&usys);
2709
2710 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2711 usys.version, usys.machine);
2712}
2713
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002714WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002715weston_compositor_init(struct weston_compositor *ec,
2716 struct wl_display *display,
2717 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04002718 char *argv[],
2719 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002720{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002721 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002722 struct xkb_rule_names xkb_names;
2723 const struct config_key keyboard_config_keys[] = {
2724 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2725 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2726 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2727 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2728 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2729 };
2730 const struct config_section cs[] = {
2731 { "keyboard",
2732 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
2733 };
2734
2735 memset(&xkb_names, 0, sizeof(xkb_names));
2736 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002737
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002738 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002739 wl_signal_init(&ec->destroy_signal);
2740 wl_signal_init(&ec->activate_signal);
2741 wl_signal_init(&ec->lock_signal);
2742 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002743 wl_signal_init(&ec->show_input_panel_signal);
2744 wl_signal_init(&ec->hide_input_panel_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002745 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002746
Casey Dahlin58ba1372012-04-19 22:50:08 -04002747 ec->output_id_pool = 0;
2748
Kristian Høgsberga8873122011-11-23 10:39:34 -05002749 if (!wl_display_add_global(display, &wl_compositor_interface,
2750 ec, compositor_bind))
2751 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002752
Daniel Stone725c2c32012-06-22 14:04:36 +01002753 wl_list_init(&ec->surface_list);
2754 wl_list_init(&ec->layer_list);
2755 wl_list_init(&ec->seat_list);
2756 wl_list_init(&ec->output_list);
2757 wl_list_init(&ec->key_binding_list);
2758 wl_list_init(&ec->button_binding_list);
2759 wl_list_init(&ec->axis_binding_list);
2760 wl_list_init(&ec->fade.animation.link);
2761
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002762 weston_plane_init(&ec->primary_plane, 0, 0);
2763
Daniel Stone725c2c32012-06-22 14:04:36 +01002764 weston_compositor_xkb_init(ec, &xkb_names);
2765
2766 ec->ping_handler = NULL;
2767
2768 screenshooter_create(ec);
2769 text_cursor_position_notifier_create(ec);
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +02002770 text_model_factory_create(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002771
2772 wl_data_device_manager_init(ec->wl_display);
2773
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002774 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002775
Daniel Stone725c2c32012-06-22 14:04:36 +01002776 loop = wl_display_get_event_loop(ec->wl_display);
2777 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2778 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2779
2780 ec->input_loop = wl_event_loop_create();
2781
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002782 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
2783 ec->fade.animation.frame = fade_frame;
2784
2785 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2786 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2787
2788 weston_compositor_schedule_repaint(ec);
2789
Daniel Stone725c2c32012-06-22 14:04:36 +01002790 return 0;
2791}
2792
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002793WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002794weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002795{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002796 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002797
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002798 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002799 if (ec->input_loop_source)
2800 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002801
Matt Roper361d2ad2011-08-29 13:52:23 -07002802 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002803 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002804 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002805
Daniel Stone325fc2d2012-05-30 16:31:58 +01002806 weston_binding_list_destroy_all(&ec->key_binding_list);
2807 weston_binding_list_destroy_all(&ec->button_binding_list);
2808 weston_binding_list_destroy_all(&ec->axis_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002809
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002810 weston_plane_release(&ec->primary_plane);
2811
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002812 wl_array_release(&ec->vertices);
2813 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05002814 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002815
2816 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07002817}
2818
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002819static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002820{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002821 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002822
Martin Minarik6d118362012-06-07 18:01:59 +02002823 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002824 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002825
2826 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002827}
2828
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002829static void
2830on_segv_signal(int s, siginfo_t *siginfo, void *context)
2831{
2832 void *buffer[32];
2833 int i, count;
2834 Dl_info info;
2835
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002836 /* This SIGSEGV handler will do a best-effort backtrace, and
2837 * then call the backend restore function, which will switch
2838 * back to the vt we launched from or ungrab X etc and then
2839 * raise SIGTRAP. If we run weston under gdb from X or a
2840 * different vt, and tell gdb "handle SIGSEGV nostop", this
2841 * will allow weston to switch back to gdb on crash and then
2842 * gdb will catch the crash with SIGTRAP. */
2843
Martin Minarik6d118362012-06-07 18:01:59 +02002844 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002845
2846 count = backtrace(buffer, ARRAY_LENGTH(buffer));
2847 for (i = 0; i < count; i++) {
2848 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02002849 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002850 (long) buffer[i],
2851 info.dli_sname ? info.dli_sname : "--",
2852 info.dli_fname);
2853 }
2854
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002855 segv_compositor->restore(segv_compositor);
2856
2857 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002858}
2859
2860
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002861static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04002862load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002863{
2864 char path[PATH_MAX];
2865 void *module, *init;
2866
2867 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07002868 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002869 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02002870 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002871
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002872 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
2873 if (module) {
2874 weston_log("Module '%s' already loaded\n", path);
2875 dlclose(module);
2876 return NULL;
2877 }
2878
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002879 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04002880 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002881 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002882 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002883 return NULL;
2884 }
2885
2886 init = dlsym(module, entrypoint);
2887 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002888 weston_log("Failed to lookup init function: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002889 return NULL;
2890 }
2891
2892 return init;
2893}
2894
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002895static int
2896load_modules(struct weston_compositor *ec, const char *modules)
2897{
2898 const char *p, *end;
2899 char buffer[256];
2900 int (*module_init)(struct weston_compositor *ec);
2901
2902 if (modules == NULL)
2903 return 0;
2904
2905 p = modules;
2906 while (*p) {
2907 end = strchrnul(p, ',');
2908 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
2909 module_init = load_module(buffer, "module_init");
2910 if (module_init)
2911 module_init(ec);
2912 p = end;
2913 while (*p == ',')
2914 p++;
2915
2916 }
2917
2918 return 0;
2919}
2920
Pekka Paalanen78a0b572012-06-06 16:59:44 +03002921static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02002922 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
2923
2924static const char xdg_wrong_message[] =
2925 "fatal: environment variable XDG_RUNTIME_DIR\n"
2926 "is set to \"%s\", which is not a directory.\n";
2927
2928static const char xdg_wrong_mode_message[] =
2929 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
2930 "correctly. Unix access mode must be 0700 but is %o,\n"
2931 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04002932 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02002933
2934static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03002935 "Refer to your distribution on how to get it, or\n"
2936 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
2937 "on how to implement it.\n";
2938
Martin Minarik37032f82012-06-18 20:15:18 +02002939static void
2940verify_xdg_runtime_dir(void)
2941{
2942 char *dir = getenv("XDG_RUNTIME_DIR");
2943 struct stat s;
2944
2945 if (!dir) {
2946 weston_log(xdg_error_message);
2947 weston_log_continue(xdg_detail_message);
2948 exit(EXIT_FAILURE);
2949 }
2950
2951 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
2952 weston_log(xdg_wrong_message, dir);
2953 weston_log_continue(xdg_detail_message);
2954 exit(EXIT_FAILURE);
2955 }
2956
2957 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
2958 weston_log(xdg_wrong_mode_message,
2959 dir, s.st_mode & 0777, s.st_uid);
2960 weston_log_continue(xdg_detail_message);
2961 }
2962}
2963
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04002964static int
2965usage(int error_code)
2966{
2967 fprintf(stderr,
2968 "Usage: weston [OPTIONS]\n\n"
2969 "This is weston version " VERSION ", the Wayland reference compositor.\n"
2970 "Weston supports multiple backends, and depending on which backend is in use\n"
2971 "different options will be accepted.\n\n"
2972
2973
2974 "Core options:\n\n"
2975 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
2976 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
2977 " -S, --socket=NAME\tName of socket to listen on\n"
2978 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002979 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04002980 " --log==FILE\t\tLog to the given file\n"
2981 " -h, --help\t\tThis help message\n\n");
2982
2983 fprintf(stderr,
2984 "Options for drm-backend.so:\n\n"
2985 " --connector=ID\tBring up only this connector\n"
2986 " --seat=SEAT\t\tThe seat that weston should run on\n"
2987 " --tty=TTY\t\tThe tty to use\n"
2988 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
2989
2990 fprintf(stderr,
2991 "Options for x11-backend.so:\n\n"
2992 " --width=WIDTH\t\tWidth of X window\n"
2993 " --height=HEIGHT\tHeight of X window\n"
2994 " --fullscreen\t\tRun in fullscreen mode\n"
2995 " --output-count=COUNT\tCreate multiple outputs\n"
2996 " --no-input\t\tDont create input devices\n\n");
2997
2998 fprintf(stderr,
2999 "Options for wayland-backend.so:\n\n"
3000 " --width=WIDTH\t\tWidth of Wayland surface\n"
3001 " --height=HEIGHT\tHeight of Wayland surface\n"
3002 " --display=DISPLAY\tWayland display to connect to\n\n");
3003
3004 exit(error_code);
3005}
3006
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003007int main(int argc, char *argv[])
3008{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003009 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003010 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003011 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003012 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003013 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003014 struct sigaction segv_action;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003015 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003016 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003017 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003018 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003019 char *backend = NULL;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003020 const char *modules = "desktop-shell.so", *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003021 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003022 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003023 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003024 char *socket_name = "wayland-0";
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003025 char *config_file;
3026
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003027 const struct config_key core_config_keys[] = {
3028 { "modules", CONFIG_KEY_STRING, &modules },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003029 };
3030
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003031 const struct config_section cs[] = {
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003032 { "core",
3033 core_config_keys, ARRAY_LENGTH(core_config_keys) },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003034 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003035
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003036 const struct weston_option core_options[] = {
3037 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3038 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3039 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003040 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003041 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003042 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003043 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003044
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003045 argc = parse_options(core_options,
3046 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003047
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003048 if (help)
3049 usage(EXIT_SUCCESS);
3050
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003051 weston_log_file_open(log);
3052
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003053 weston_log("%s\n"
3054 STAMP_SPACE "%s\n"
3055 STAMP_SPACE "Bug reports to: %s\n"
3056 STAMP_SPACE "Build: %s\n",
3057 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003058 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003059 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003060
Martin Minarik37032f82012-06-18 20:15:18 +02003061 verify_xdg_runtime_dir();
3062
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003063 display = wl_display_create();
3064
Tiago Vignatti2116b892011-08-08 05:52:59 -07003065 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003066 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3067 display);
3068 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3069 display);
3070 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3071 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003072
3073 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003074 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3075 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003076
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003077 if (!backend) {
3078 if (getenv("WAYLAND_DISPLAY"))
3079 backend = "wayland-backend.so";
3080 else if (getenv("DISPLAY"))
3081 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003082 else
3083 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003084 }
3085
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003086 config_file = config_file_path("weston.ini");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003087 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003088
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003089 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003090 if (!backend_init)
3091 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003092
Daniel Stonec1be8e52012-06-01 11:14:02 -04003093 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003094 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003095 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003096 exit(EXIT_FAILURE);
3097 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003098
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003099 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3100 segv_action.sa_sigaction = on_segv_signal;
3101 sigemptyset(&segv_action.sa_mask);
3102 sigaction(SIGSEGV, &segv_action, NULL);
3103 segv_compositor = ec;
3104
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003105 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03003106 weston_log("fatal: unhandled option: %s\n", argv[i]);
3107 if (argv[1]) {
3108 ret = EXIT_FAILURE;
3109 goto out;
3110 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003111
Daniel Stonec1be8e52012-06-01 11:14:02 -04003112 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01003113
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003114 ec->option_idle_time = idle_time;
3115 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003116
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003117 setenv("WAYLAND_DISPLAY", socket_name, 1);
3118
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003119 if (load_modules(ec, modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003120 goto out;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003121 if (load_modules(ec, option_modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003122 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003123
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003124 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003125 weston_log("fatal: failed to add socket: %m\n");
3126 ret = EXIT_FAILURE;
3127 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003128 }
3129
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003130 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003131 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003132
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003133 wl_display_run(display);
3134
3135 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003136 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05003137 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003138
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003139 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003140
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003141 for (i = ARRAY_LENGTH(signals); i;)
3142 wl_event_source_remove(signals[--i]);
3143
Daniel Stone855028f2012-05-01 20:37:10 +01003144 weston_compositor_xkb_destroy(ec);
3145
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003146 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003147 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003148
Martin Minarik19e6f262012-06-07 13:08:46 +02003149 weston_log_file_close();
3150
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003151 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003152}