blob: 47d0ec5a58e6fe0ad1ef68f9dd49cd29ded64bba [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{
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200685 int i;
686
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500687 struct weston_surface *surface =
688 container_of(resource,
689 struct weston_surface, surface.resource);
690 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400691 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400692
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300693 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500694 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400695
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200696 glDeleteTextures(surface->num_textures, surface->textures);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200697
Benjamin Franzke06286262011-05-06 19:12:33 +0200698 if (surface->buffer)
699 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400700
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200701 for (i = 0; i < surface->num_images; i++)
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400702 compositor->destroy_image(compositor->egl_display,
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200703 surface->images[i]);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200704
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200705 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200706 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500707 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500708 pixman_region32_fini(&surface->clip);
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500709 if (!region_is_undefined(&surface->input))
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500710 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200711
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400712 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
713 wl_resource_destroy(&cb->resource);
714
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400715 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500716}
717
Alex Wu8811bf92012-02-28 18:07:54 +0800718WL_EXPORT void
719weston_surface_destroy(struct weston_surface *surface)
720{
721 /* Not a valid way to destroy a client surface */
722 assert(surface->surface.resource.client == NULL);
723
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400724 wl_signal_emit(&surface->surface.resource.destroy_signal,
725 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800726 destroy_surface(&surface->surface.resource);
727}
728
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100729static void
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -0400730weston_surface_attach(struct wl_surface *surface, struct wl_buffer *buffer)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100731{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500732 struct weston_surface *es = (struct weston_surface *) surface;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100733
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300734 if (es->buffer) {
735 weston_buffer_post_release(es->buffer);
736 wl_list_remove(&es->buffer_destroy_listener.link);
737 }
738
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400739 if (buffer) {
740 buffer->busy_count++;
741 wl_signal_add(&buffer->resource.destroy_signal,
742 &es->buffer_destroy_listener);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300743
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400744 if (es->geometry.width != buffer->width ||
745 es->geometry.height != buffer->height) {
746 undef_region(&es->input);
747 pixman_region32_fini(&es->opaque);
748 pixman_region32_init(&es->opaque);
749 }
750 } else {
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300751 if (weston_surface_is_mapped(es))
752 weston_surface_unmap(es);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300753 }
754
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400755 es->compositor->renderer->attach(es, buffer);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400756 es->buffer = buffer;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100757}
758
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500759WL_EXPORT void
760weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400761{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500762 wl_list_remove(&surface->layer_link);
763 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500764 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500765 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400766}
767
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400768WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500769weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400770{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500771 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400772
773 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500774 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400775}
776
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500777WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500778weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +0200779{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400780 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200781 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200782
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400783 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -0500784 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200785}
786
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400787WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500788weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400789{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500790 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400791
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400792 pixman_region32_union(&compositor->primary_plane.damage,
793 &compositor->primary_plane.damage,
794 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400795 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400796}
797
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400798static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500799fade_frame(struct weston_animation *animation,
800 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400801{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500802 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400803 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500804 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500805 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400806
Scott Moreau94b0b0c2012-06-14 01:01:56 -0600807 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -0600808 compositor->fade.spring.timestamp = msecs;
809
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500810 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500811 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500812 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
813 compositor->fade.spring.current);
814 weston_surface_damage(surface);
815
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500816 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400817 compositor->fade.spring.current =
818 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400819 wl_list_remove(&animation->link);
820 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200821
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500822 if (compositor->fade.spring.current < 0.001) {
823 destroy_surface(&surface->surface.resource);
824 compositor->fade.surface = NULL;
825 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500826 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400827 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200828 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400829 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400830}
831
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400832static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400833surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400834 pixman_region32_t *opaque)
835{
836 if (surface->buffer && wl_buffer_is_shm(surface->buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400837 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400838
839 if (surface->transform.enabled) {
840 pixman_box32_t *extents;
841
842 extents = pixman_region32_extents(&surface->damage);
843 surface_compute_bbox(surface, extents->x1, extents->y1,
844 extents->x2 - extents->x1,
845 extents->y2 - extents->y1,
846 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400847 pixman_region32_translate(&surface->damage,
848 -surface->plane->x,
849 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400850 } else {
851 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400852 surface->geometry.x - surface->plane->x,
853 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400854 }
855
856 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400857 pixman_region32_union(&surface->plane->damage,
858 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400859 empty_region(&surface->damage);
860 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400861 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400862}
863
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400864static void
Rob Bradford0fb79752012-08-02 15:36:57 +0100865weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400866{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500867 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500868 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500869 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -0500870 struct weston_animation *animation, *next;
871 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +0200872 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400873 pixman_region32_t opaque, output_damage, new_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500874
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +0200875 weston_compositor_update_drag_surfaces(ec);
876
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500877 /* Rebuild the surface list and update surface transforms up front. */
878 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +0200879 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500880 wl_list_for_each(layer, &ec->layer_list, link) {
881 wl_list_for_each(es, &layer->surface_list, layer_link) {
882 weston_surface_update_transform(es);
883 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +0200884 if (es->output == output) {
885 wl_list_insert_list(&frame_callback_list,
886 &es->frame_callback_list);
887 wl_list_init(&es->frame_callback_list);
888 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500889 }
890 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +0200891
Kristian Høgsberg79af73e2012-08-03 15:45:23 -0400892 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800893 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -0400894 else
895 wl_list_for_each(es, &ec->surface_list, link)
896 weston_surface_move_to_plane(es, &ec->primary_plane);
897
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500898 pixman_region32_init(&opaque);
899
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400900 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400901 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500902
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400903 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -0400904
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400905 pixman_region32_init(&output_damage);
906
907 pixman_region32_init(&new_damage);
908 pixman_region32_copy(&new_damage, &ec->primary_plane.damage);
909
910 pixman_region32_union(&ec->primary_plane.damage,
911 &ec->primary_plane.damage,
912 &output->previous_damage);
913
914 pixman_region32_intersect(&output->previous_damage,
915 &new_damage, &output->region);
916
917 pixman_region32_intersect(&output_damage,
918 &ec->primary_plane.damage, &output->region);
919
920 pixman_region32_fini(&new_damage);
921
Scott Moreauccbf29d2012-02-22 14:21:41 -0700922 if (output->dirty)
923 weston_output_update_matrix(output);
924
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400925 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400926
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500927 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500928
Kristian Høgsbergef044142011-06-21 15:02:12 -0400929 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100930
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -0400931 weston_compositor_repick(ec);
932 wl_event_loop_dispatch(ec->input_loop, 0);
933
Jonas Ådahldb773762012-06-13 00:01:21 +0200934 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500935 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400936 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500937 }
Jonas Ådahldb773762012-06-13 00:01:21 +0200938 wl_list_init(&frame_callback_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500939
Scott Moreaud64cf212012-06-08 19:40:54 -0600940 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -0600941 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -0600942 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -0600943 }
Kristian Høgsbergef044142011-06-21 15:02:12 -0400944}
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400945
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -0500946static int
947weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -0400948{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -0500949 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -0500950
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -0500951 wl_event_loop_dispatch(compositor->input_loop, 0);
952
953 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -0400954}
955
956WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +0100957weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -0400958{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -0500959 struct weston_compositor *compositor = output->compositor;
960 struct wl_event_loop *loop =
961 wl_display_get_event_loop(compositor->wl_display);
962 int fd;
963
Kristian Høgsberge9d04922012-06-19 23:54:26 -0400964 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -0500965 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -0500966 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -0500967 return;
968 }
969
970 output->repaint_scheduled = 0;
971 if (compositor->input_loop_source)
972 return;
973
974 fd = wl_event_loop_get_fd(compositor->input_loop);
975 compositor->input_loop_source =
976 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
977 weston_compositor_read_input, compositor);
978}
979
980static void
981idle_repaint(void *data)
982{
983 struct weston_output *output = data;
984
985 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400986}
987
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400988WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500989weston_layer_init(struct weston_layer *layer, struct wl_list *below)
990{
991 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200992 if (below != NULL)
993 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500994}
995
996WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400997weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400998{
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400999 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001000 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001001
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001002 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001003 return;
1004
Kristian Høgsbergef044142011-06-21 15:02:12 -04001005 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001006 output->repaint_needed = 1;
1007 if (output->repaint_scheduled)
1008 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001009
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001010 wl_event_loop_add_idle(loop, idle_repaint, output);
1011 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001012
1013 if (compositor->input_loop_source) {
1014 wl_event_source_remove(compositor->input_loop_source);
1015 compositor->input_loop_source = NULL;
1016 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001017}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001018
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001019WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001020weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1021{
1022 struct weston_output *output;
1023
1024 wl_list_for_each(output, &compositor->output_list, link)
1025 weston_output_schedule_repaint(output);
1026}
1027
1028WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001029weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001030{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001031 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001032 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001033
Scott Moreau9d1b1122012-06-08 19:40:53 -06001034 output = container_of(compositor->output_list.next,
1035 struct weston_output, link);
1036
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001037 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001038 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001039 return;
1040
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001041 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001042 surface = weston_surface_create(compositor);
1043 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001044 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001045 wl_list_insert(&compositor->fade_layer.surface_list,
1046 &surface->layer_link);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001047 weston_surface_assign_output(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001048 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001049 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001050 }
1051
1052 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001053 if (wl_list_empty(&compositor->fade.animation.link)) {
1054 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001055 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001056 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001057 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001058}
1059
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001060static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001061surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001062{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001063 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001064}
1065
Casey Dahlin96d8a752012-04-19 22:50:07 -04001066static struct wl_resource *
1067find_resource_for_client(struct wl_list *list, struct wl_client *client)
1068{
1069 struct wl_resource *r;
1070
1071 wl_list_for_each(r, list, link) {
1072 if (r->client == client)
1073 return r;
1074 }
1075
1076 return NULL;
1077}
1078
Casey Dahlin9074db52012-04-19 22:50:09 -04001079static void
1080weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1081{
1082 uint32_t different = es->output_mask ^ mask;
1083 uint32_t entered = mask & different;
1084 uint32_t left = es->output_mask & different;
1085 struct weston_output *output;
Rob Bradford8667e772012-05-18 14:13:03 +01001086 struct wl_resource *resource = NULL;
Casey Dahlin9074db52012-04-19 22:50:09 -04001087 struct wl_client *client = es->surface.resource.client;
1088
Scott Moreaua5021522012-08-03 17:11:51 -06001089 es->output_mask = mask;
Casey Dahlin9074db52012-04-19 22:50:09 -04001090 if (es->surface.resource.client == NULL)
1091 return;
1092 if (different == 0)
1093 return;
1094
Casey Dahlin9074db52012-04-19 22:50:09 -04001095 wl_list_for_each(output, &es->compositor->output_list, link) {
1096 if (1 << output->id & different)
1097 resource =
1098 find_resource_for_client(&output->resource_list,
1099 client);
Kristian Høgsbergc7814d22012-07-12 12:34:43 -04001100 if (resource == NULL)
1101 continue;
Casey Dahlin9074db52012-04-19 22:50:09 -04001102 if (1 << output->id & entered)
1103 wl_surface_send_enter(&es->surface.resource, resource);
1104 if (1 << output->id & left)
1105 wl_surface_send_leave(&es->surface.resource, resource);
1106 }
1107}
1108
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001109WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001110weston_surface_assign_output(struct weston_surface *es)
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001111{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001112 struct weston_compositor *ec = es->compositor;
1113 struct weston_output *output, *new_output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001114 pixman_region32_t region;
Casey Dahlin9074db52012-04-19 22:50:09 -04001115 uint32_t max, area, mask;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001116 pixman_box32_t *e;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001117
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001118 new_output = NULL;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001119 max = 0;
Casey Dahlin9074db52012-04-19 22:50:09 -04001120 mask = 0;
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001121 pixman_region32_init(&region);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001122 wl_list_for_each(output, &ec->output_list, link) {
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001123 pixman_region32_intersect(&region, &es->transform.boundingbox,
1124 &output->region);
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001125
1126 e = pixman_region32_extents(&region);
1127 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1128
Casey Dahlin9074db52012-04-19 22:50:09 -04001129 if (area > 0)
1130 mask |= 1 << output->id;
1131
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001132 if (area >= max) {
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001133 new_output = output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001134 max = area;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001135 }
1136 }
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001137 pixman_region32_fini(&region);
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001138
1139 es->output = new_output;
Casey Dahlin9074db52012-04-19 22:50:09 -04001140 weston_surface_update_output_mask(es, mask);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001141}
1142
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001143static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001144surface_attach(struct wl_client *client,
1145 struct wl_resource *resource,
1146 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1147{
1148 struct weston_surface *es = resource->data;
1149 struct wl_buffer *buffer = NULL;
1150
1151 if (buffer_resource)
1152 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001153
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -04001154 weston_surface_attach(&es->surface, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001155
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001156 if (buffer && es->configure)
1157 es->configure(es, sx, sy);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001158}
1159
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001160static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001161surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001162 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001163 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001164{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001165 struct weston_surface *es = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001166
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001167 pixman_region32_union_rect(&es->damage, &es->damage,
1168 x, y, width, height);
Kristian Høgsberg98238702012-08-03 16:29:12 -04001169 weston_surface_schedule_repaint(es);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001170}
1171
Kristian Høgsberg33418202011-08-16 23:01:28 -04001172static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001173destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001174{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001175 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001176
1177 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001178 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001179}
1180
1181static void
1182surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001183 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001184{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001185 struct weston_frame_callback *cb;
1186 struct weston_surface *es = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001187
1188 cb = malloc(sizeof *cb);
1189 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001190 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001191 return;
1192 }
1193
1194 cb->resource.object.interface = &wl_callback_interface;
1195 cb->resource.object.id = callback;
1196 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001197 cb->resource.client = client;
1198 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001199
1200 wl_client_add_resource(client, &cb->resource);
Jonas Ådahldb773762012-06-13 00:01:21 +02001201 wl_list_insert(es->frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001202}
1203
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001204static void
1205surface_set_opaque_region(struct wl_client *client,
1206 struct wl_resource *resource,
1207 struct wl_resource *region_resource)
1208{
1209 struct weston_surface *surface = resource->data;
1210 struct weston_region *region;
1211
1212 pixman_region32_fini(&surface->opaque);
1213
1214 if (region_resource) {
1215 region = region_resource->data;
1216 pixman_region32_init_rect(&surface->opaque, 0, 0,
1217 surface->geometry.width,
1218 surface->geometry.height);
1219 pixman_region32_intersect(&surface->opaque,
1220 &surface->opaque, &region->region);
1221 } else {
1222 pixman_region32_init(&surface->opaque);
1223 }
1224
1225 surface->geometry.dirty = 1;
1226}
1227
1228static void
1229surface_set_input_region(struct wl_client *client,
1230 struct wl_resource *resource,
1231 struct wl_resource *region_resource)
1232{
1233 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001234 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001235
1236 if (region_resource) {
1237 region = region_resource->data;
1238 pixman_region32_init_rect(&surface->input, 0, 0,
1239 surface->geometry.width,
1240 surface->geometry.height);
1241 pixman_region32_intersect(&surface->input,
1242 &surface->input, &region->region);
1243 } else {
1244 pixman_region32_init_rect(&surface->input, 0, 0,
1245 surface->geometry.width,
1246 surface->geometry.height);
1247 }
1248
Kristian Høgsberg98238702012-08-03 16:29:12 -04001249 weston_surface_schedule_repaint(surface);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001250}
1251
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001252static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001253 surface_destroy,
1254 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001255 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001256 surface_frame,
1257 surface_set_opaque_region,
1258 surface_set_input_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001259};
1260
1261static void
1262compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001263 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001264{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001265 struct weston_compositor *ec = resource->data;
1266 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001267
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001268 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001269 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001270 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001271 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001272 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001273
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001274 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001275
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001276 surface->surface.resource.object.id = id;
1277 surface->surface.resource.object.interface = &wl_surface_interface;
1278 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001279 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001280 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001281
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001282 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001283}
1284
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001285static void
1286destroy_region(struct wl_resource *resource)
1287{
1288 struct weston_region *region =
1289 container_of(resource, struct weston_region, resource);
1290
1291 pixman_region32_fini(&region->region);
1292 free(region);
1293}
1294
1295static void
1296region_destroy(struct wl_client *client, struct wl_resource *resource)
1297{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001298 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001299}
1300
1301static void
1302region_add(struct wl_client *client, struct wl_resource *resource,
1303 int32_t x, int32_t y, int32_t width, int32_t height)
1304{
1305 struct weston_region *region = resource->data;
1306
1307 pixman_region32_union_rect(&region->region, &region->region,
1308 x, y, width, height);
1309}
1310
1311static void
1312region_subtract(struct wl_client *client, struct wl_resource *resource,
1313 int32_t x, int32_t y, int32_t width, int32_t height)
1314{
1315 struct weston_region *region = resource->data;
1316 pixman_region32_t rect;
1317
1318 pixman_region32_init_rect(&rect, x, y, width, height);
1319 pixman_region32_subtract(&region->region, &region->region, &rect);
1320 pixman_region32_fini(&rect);
1321}
1322
1323static const struct wl_region_interface region_interface = {
1324 region_destroy,
1325 region_add,
1326 region_subtract
1327};
1328
1329static void
1330compositor_create_region(struct wl_client *client,
1331 struct wl_resource *resource, uint32_t id)
1332{
1333 struct weston_region *region;
1334
1335 region = malloc(sizeof *region);
1336 if (region == NULL) {
1337 wl_resource_post_no_memory(resource);
1338 return;
1339 }
1340
1341 region->resource.destroy = destroy_region;
1342
1343 region->resource.object.id = id;
1344 region->resource.object.interface = &wl_region_interface;
1345 region->resource.object.implementation =
1346 (void (**)(void)) &region_interface;
1347 region->resource.data = region;
1348
1349 pixman_region32_init(&region->region);
1350
1351 wl_client_add_resource(client, &region->resource);
1352}
1353
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001354static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001355 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001356 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001357};
1358
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001359WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001360weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001361{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001362 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1363 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001364
1365 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001366 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001367}
1368
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001369static void
1370weston_compositor_dpms_on(struct weston_compositor *compositor)
1371{
1372 struct weston_output *output;
1373
1374 wl_list_for_each(output, &compositor->output_list, link)
1375 if (output->set_dpms)
1376 output->set_dpms(output, WESTON_DPMS_ON);
1377}
1378
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001379WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001380weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001381{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001382 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1383 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001384 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001385 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001386 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001387 }
1388}
1389
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001390static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001391weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001392{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001393 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001394 compositor->idle_inhibit++;
1395}
1396
1397static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001398weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001399{
1400 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001401 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001402}
1403
1404static int
1405idle_handler(void *data)
1406{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001407 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001408
1409 if (compositor->idle_inhibit)
1410 return 1;
1411
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001412 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001413
1414 return 1;
1415}
1416
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001417WL_EXPORT void
1418weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1419{
1420 pixman_region32_init(&plane->damage);
1421 plane->x = x;
1422 plane->y = y;
1423}
1424
1425WL_EXPORT void
1426weston_plane_release(struct weston_plane *plane)
1427{
1428 pixman_region32_fini(&plane->damage);
1429}
1430
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001431static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001432weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001433
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001434static void
Daniel Stone37816df2012-05-16 18:45:18 +01001435clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001436{
Daniel Stone37816df2012-05-16 18:45:18 +01001437 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001438 struct weston_output *output, *prev = NULL;
1439 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001440
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001441 x = wl_fixed_to_int(*fx);
1442 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001443 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1444 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001445
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001446 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001447 if (pixman_region32_contains_point(&output->region,
1448 x, y, NULL))
1449 valid = 1;
1450 if (pixman_region32_contains_point(&output->region,
1451 old_x, old_y, NULL))
1452 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001453 }
Daniel Stone37816df2012-05-16 18:45:18 +01001454
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001455 if (!valid) {
1456 if (x < prev->x)
1457 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001458 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001459 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001460 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001461 if (y < prev->y)
1462 *fy = wl_fixed_from_int(prev->y);
1463 else if (y >= prev->y + prev->current->height)
1464 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001465 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001466 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001467}
1468
1469WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001470notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001471{
1472 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001473 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001474 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001475 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001476 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001477
1478 weston_compositor_activity(ec);
1479
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001480 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001481
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001482 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001483
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001484 pointer->x = x;
1485 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001486
1487 ix = wl_fixed_to_int(x);
1488 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001489
Scott Moreauccbf29d2012-02-22 14:21:41 -07001490 wl_list_for_each(output, &ec->output_list, link)
1491 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001492 pixman_region32_contains_point(&output->region,
1493 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06001494 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001495
Daniel Stone37816df2012-05-16 18:45:18 +01001496 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001497 interface = pointer->grab->interface;
1498 interface->motion(pointer->grab, time,
1499 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001500
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001501 if (seat->sprite) {
1502 weston_surface_set_position(seat->sprite,
1503 ix - seat->hotspot_x,
1504 iy - seat->hotspot_y);
1505 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001506 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001507}
1508
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001509WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001510weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001511 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001512{
Daniel Stone37816df2012-05-16 18:45:18 +01001513 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001514
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001515 if (seat->seat.keyboard) {
1516 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1517 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04001518 }
1519
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001520 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001521}
1522
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001523WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001524notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001525 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001526{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001527 struct weston_compositor *compositor = seat->compositor;
1528 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001529 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001530 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001531 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001532
Daniel Stone4dbadb12012-05-30 16:31:51 +01001533 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001534 if (compositor->ping_handler && focus)
1535 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001536 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001537 if (pointer->button_count == 0) {
1538 pointer->grab_button = button;
1539 pointer->grab_time = time;
1540 pointer->grab_x = pointer->x;
1541 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001542 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001543 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001544 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001545 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001546 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001547 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001548
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001549 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001550 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001551
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001552 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001553
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001554 if (pointer->button_count == 1)
1555 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001556 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001557}
1558
Scott Moreau210d0792012-03-22 10:47:01 -06001559WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001560notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01001561 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001562{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001563 struct weston_compositor *compositor = seat->compositor;
1564 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001565 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001566 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001567 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1568
1569 if (compositor->ping_handler && focus)
1570 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001571
1572 weston_compositor_activity(compositor);
1573
Scott Moreau6a3633d2012-03-20 08:47:59 -06001574 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001575 weston_compositor_run_axis_binding(compositor, seat,
1576 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06001577 else
1578 return;
1579
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001580 if (pointer->focus_resource)
1581 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01001582 value);
Scott Moreau210d0792012-03-22 10:47:01 -06001583}
1584
Daniel Stone05d58682012-06-22 13:21:38 +01001585WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001586notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001587{
Daniel Stone05d58682012-06-22 13:21:38 +01001588 struct wl_keyboard *keyboard = &seat->keyboard;
1589 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01001590 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001591 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01001592 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01001593 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01001594
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001595 /* Serialize and update our internal state, checking to see if it's
1596 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01001597 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1598 XKB_STATE_DEPRESSED);
1599 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1600 XKB_STATE_LATCHED);
1601 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1602 XKB_STATE_LOCKED);
1603 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01001604 XKB_STATE_EFFECTIVE);
1605
Daniel Stone71c38772012-06-22 13:21:30 +01001606 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
1607 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
1608 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
1609 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01001610 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01001611
Daniel Stone71c38772012-06-22 13:21:30 +01001612 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
1613 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
1614 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
1615 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001616
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001617 /* And update the modifier_state for bindings. */
1618 mods_lookup = mods_depressed | mods_latched;
1619 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01001620 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001621 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01001622 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001623 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01001624 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001625 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04001626 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
1627 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01001628
Daniel Stone8c8164f2012-05-30 16:31:45 +01001629 /* Finally, notify the compositor that LEDs have changed. */
1630 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001631 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001632 leds |= LED_NUM_LOCK;
1633 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001634 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001635 leds |= LED_CAPS_LOCK;
1636 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001637 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001638 leds |= LED_SCROLL_LOCK;
1639 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01001640 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01001641 seat->xkb_state.leds = leds;
1642
Daniel Stone05d58682012-06-22 13:21:38 +01001643 if (changed) {
1644 grab->interface->modifiers(grab,
1645 serial,
1646 keyboard->modifiers.mods_depressed,
1647 keyboard->modifiers.mods_latched,
1648 keyboard->modifiers.mods_locked,
1649 keyboard->modifiers.group);
1650 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001651}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001652
Daniel Stone05d58682012-06-22 13:21:38 +01001653static void
1654update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001655 enum wl_keyboard_key_state state)
1656{
1657 enum xkb_key_direction direction;
1658
1659 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1660 direction = XKB_KEY_DOWN;
1661 else
1662 direction = XKB_KEY_UP;
1663
1664 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1665 * broken keycode system, which starts at 8. */
1666 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1667
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001668 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001669}
1670
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001671WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001672notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001673 enum wl_keyboard_key_state state,
1674 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001675{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001676 struct weston_compositor *compositor = seat->compositor;
1677 struct wl_keyboard *keyboard = seat->seat.keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01001678 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001679 (struct weston_surface *) keyboard->focus;
1680 struct wl_keyboard_grab *grab = keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001681 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001682 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001683
Daniel Stonec9785ea2012-05-30 16:31:52 +01001684 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001685 if (compositor->ping_handler && focus)
1686 compositor->ping_handler(focus, serial);
1687
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001688 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001689 keyboard->grab_key = key;
1690 keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07001691 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001692 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001693 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001694
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001695 end = keyboard->keys.data + keyboard->keys.size;
1696 for (k = keyboard->keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01001697 if (*k == key) {
1698 /* Ignore server-generated repeats. */
1699 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1700 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001701 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01001702 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001703 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001704 keyboard->keys.size = (void *) end - keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001705 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001706 k = wl_array_add(&keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001707 *k = key;
1708 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001709
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001710 if (grab == &keyboard->default_grab) {
1711 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001712 state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001713 grab = keyboard->grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06001714 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001715
Daniel Stone7ace3902012-05-30 16:31:40 +01001716 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01001717
1718 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001719 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01001720 wl_display_get_serial(compositor->wl_display),
1721 key,
1722 state);
1723 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001724}
1725
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001726WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001727notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01001728 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001729{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001730 struct weston_compositor *compositor = seat->compositor;
1731 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001732
1733 if (output) {
Daniel Stone37816df2012-05-16 18:45:18 +01001734 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001735 x - pointer->x,
1736 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001737
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001738 pointer->x = x;
1739 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001740 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001741 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001742 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001743 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03001744 /* FIXME: We should call wl_pointer_set_focus(seat,
1745 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001746 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001747}
1748
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001749static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001750destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001751{
Daniel Stone37816df2012-05-16 18:45:18 +01001752 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001753
Daniel Stone37816df2012-05-16 18:45:18 +01001754 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001755 saved_kbd_focus_listener);
1756
Daniel Stone37816df2012-05-16 18:45:18 +01001757 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001758}
1759
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001760WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001761notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001762 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001763{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001764 struct weston_compositor *compositor = seat->compositor;
1765 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001766 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001767 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001768
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001769 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001770 wl_array_copy(&keyboard->keys, keys);
1771 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001772 weston_compositor_idle_inhibit(compositor);
1773 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001774 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001775 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001776 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001777
Daniel Stoneef172672012-06-22 13:21:32 +01001778 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001779 wl_array_for_each(k, &keyboard->keys) {
1780 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01001781 WL_KEYBOARD_KEY_STATE_PRESSED);
1782 }
1783
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001784 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001785
1786 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001787 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1788 wl_keyboard_set_focus(keyboard, surface);
1789 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001790 }
1791}
1792
1793WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001794notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01001795{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001796 struct weston_compositor *compositor = seat->compositor;
1797 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001798 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001799
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001800 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001801 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001802 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001803 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001804 WL_KEYBOARD_KEY_STATE_RELEASED);
1805 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001806
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001807 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001808
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001809 if (keyboard->focus) {
1810 seat->saved_kbd_focus = keyboard->focus;
1811 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01001812 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001813 wl_signal_add(&keyboard->focus->resource.destroy_signal,
1814 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001815 }
1816
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001817 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001818 /* FIXME: We really need keyboard grab cancel here to
1819 * let the grab shut down properly. As it is we leak
1820 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001821 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001822}
1823
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001824static void
Daniel Stone37816df2012-05-16 18:45:18 +01001825touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001826{
Daniel Stone37816df2012-05-16 18:45:18 +01001827 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001828 struct wl_resource *resource;
1829
Pekka Paalanen56464252012-07-31 13:21:08 +03001830 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001831 return;
1832
Pekka Paalanen56464252012-07-31 13:21:08 +03001833 if (seat->touch->focus_resource)
1834 wl_list_remove(&seat->touch->focus_listener.link);
1835 seat->touch->focus = NULL;
1836 seat->touch->focus_resource = NULL;
1837
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001838 if (surface) {
1839 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01001840 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04001841 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001842 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02001843 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001844 return;
1845 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001846
Daniel Stone37816df2012-05-16 18:45:18 +01001847 seat->touch->focus = surface;
1848 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03001849 wl_signal_add(&resource->destroy_signal,
1850 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001851 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001852}
1853
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001854/**
1855 * notify_touch - emulates button touches and notifies surfaces accordingly.
1856 *
1857 * It assumes always the correct cycle sequence until it gets here: touch_down
1858 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1859 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001860 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001861 */
1862WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001863notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001864 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001865{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001866 struct weston_compositor *ec = seat->compositor;
1867 struct wl_touch *touch = seat->seat.touch;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001868 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001869 wl_fixed_t sx, sy;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001870 uint32_t serial = 0;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001871
1872 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01001873 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001874 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001875
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001876 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001877
1878 /* the first finger down picks the surface, and all further go
1879 * to that surface for the remainder of the touch session i.e.
1880 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001881 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001882 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001883 touch_set_focus(seat, &es->surface);
1884 } else if (touch->focus) {
1885 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001886 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001887 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001888
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001889 if (touch->focus_resource && touch->focus)
1890 wl_touch_send_down(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001891 serial, time,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001892 &touch->focus->resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001893 touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001894 break;
Daniel Stone37816df2012-05-16 18:45:18 +01001895 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001896 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001897 if (!es)
1898 break;
1899
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001900 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001901 if (touch->focus_resource)
1902 wl_touch_send_motion(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001903 time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001904 break;
Daniel Stone37816df2012-05-16 18:45:18 +01001905 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001906 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001907 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001908
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001909 if (touch->focus_resource)
1910 wl_touch_send_up(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001911 serial, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001912 if (seat->num_tp == 0)
1913 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001914 break;
1915 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001916}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001917
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001918static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001919pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1920{
1921 struct weston_seat *seat = container_of(listener, struct weston_seat,
1922 sprite_destroy_listener);
1923
1924 seat->sprite = NULL;
1925}
1926
1927static void
1928pointer_cursor_surface_configure(struct weston_surface *es,
1929 int32_t dx, int32_t dy)
1930{
1931 struct weston_seat *seat = es->private;
1932 int x, y;
1933
1934 assert(es == seat->sprite);
1935
1936 seat->hotspot_x -= dx;
1937 seat->hotspot_y -= dy;
1938
1939 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
1940 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
1941
1942 weston_surface_configure(seat->sprite, x, y,
1943 es->buffer->width, es->buffer->height);
1944
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03001945 empty_region(&es->input);
1946
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001947 if (!weston_surface_is_mapped(es)) {
1948 wl_list_insert(&es->compositor->cursor_layer.surface_list,
1949 &es->layer_link);
1950 weston_surface_assign_output(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001951 }
1952}
1953
1954static void
1955pointer_unmap_sprite(struct weston_seat *seat)
1956{
1957 if (weston_surface_is_mapped(seat->sprite))
1958 weston_surface_unmap(seat->sprite);
1959
1960 wl_list_remove(&seat->sprite_destroy_listener.link);
1961 seat->sprite->configure = NULL;
1962 seat->sprite->private = NULL;
1963 seat->sprite = NULL;
1964}
1965
1966static void
1967pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1968 uint32_t serial, struct wl_resource *surface_resource,
1969 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001970{
Daniel Stone37816df2012-05-16 18:45:18 +01001971 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001972 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001973
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001974 if (surface_resource)
1975 surface = container_of(surface_resource->data,
1976 struct weston_surface, surface);
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04001977
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04001978 if (seat->seat.pointer->focus == NULL)
1979 return;
1980 if (seat->seat.pointer->focus->resource.client != client)
1981 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04001982 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001983 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03001984
1985 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03001986 if (surface->configure) {
1987 wl_resource_post_error(&surface->surface.resource,
1988 WL_DISPLAY_ERROR_INVALID_OBJECT,
1989 "surface->configure already "
1990 "set");
1991 return;
1992 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001993 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001994
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001995 if (seat->sprite)
1996 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001997
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001998 if (!surface)
1999 return;
2000
2001 wl_signal_add(&surface->surface.resource.destroy_signal,
2002 &seat->sprite_destroy_listener);
2003
2004 surface->configure = pointer_cursor_surface_configure;
2005 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002006 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002007 seat->hotspot_x = x;
2008 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002009
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04002010 if (surface->buffer)
2011 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002012}
2013
Daniel Stone37816df2012-05-16 18:45:18 +01002014static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002015 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002016};
2017
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002018static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002019handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002020{
Daniel Stone37816df2012-05-16 18:45:18 +01002021 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002022
Daniel Stone37816df2012-05-16 18:45:18 +01002023 seat = container_of(listener, struct weston_seat,
2024 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002025
Daniel Stone37816df2012-05-16 18:45:18 +01002026 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002027}
2028
Casey Dahlin9074db52012-04-19 22:50:09 -04002029static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002030{
2031 wl_list_remove(&resource->link);
2032 free(resource);
2033}
2034
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002035static void
Daniel Stone37816df2012-05-16 18:45:18 +01002036seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2037 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002038{
Daniel Stone37816df2012-05-16 18:45:18 +01002039 struct weston_seat *seat = resource->data;
2040 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002041
Daniel Stone37816df2012-05-16 18:45:18 +01002042 if (!seat->seat.pointer)
2043 return;
2044
2045 cr = wl_client_add_object(client, &wl_pointer_interface,
2046 &pointer_interface, id, seat);
2047 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002048 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002049
2050 if (seat->seat.pointer->focus &&
2051 seat->seat.pointer->focus->resource.client == client) {
2052 struct weston_surface *surface;
2053 wl_fixed_t sx, sy;
2054
2055 surface = (struct weston_surface *) seat->seat.pointer->focus;
2056 weston_surface_from_global_fixed(surface,
2057 seat->seat.pointer->x,
2058 seat->seat.pointer->y,
2059 &sx,
2060 &sy);
2061 wl_pointer_set_focus(seat->seat.pointer,
2062 seat->seat.pointer->focus,
2063 sx,
2064 sy);
2065 }
Daniel Stone37816df2012-05-16 18:45:18 +01002066}
2067
2068static void
2069seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2070 uint32_t id)
2071{
2072 struct weston_seat *seat = resource->data;
2073 struct wl_resource *cr;
2074
2075 if (!seat->seat.keyboard)
2076 return;
2077
2078 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2079 seat);
2080 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002081 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002082
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002083 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2084 seat->xkb_info.keymap_fd,
2085 seat->xkb_info.keymap_size);
2086
Daniel Stone17b13bd2012-05-30 16:31:39 +01002087 if (seat->seat.keyboard->focus &&
2088 seat->seat.keyboard->focus->resource.client == client) {
2089 wl_keyboard_set_focus(seat->seat.keyboard,
2090 seat->seat.keyboard->focus);
2091 }
Daniel Stone37816df2012-05-16 18:45:18 +01002092}
2093
2094static void
2095seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2096 uint32_t id)
2097{
2098 struct weston_seat *seat = resource->data;
2099 struct wl_resource *cr;
2100
2101 if (!seat->seat.touch)
2102 return;
2103
2104 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2105 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002106 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002107}
2108
2109static const struct wl_seat_interface seat_interface = {
2110 seat_get_pointer,
2111 seat_get_keyboard,
2112 seat_get_touch,
2113};
2114
2115static void
2116bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2117{
2118 struct wl_seat *seat = data;
2119 struct wl_resource *resource;
2120 enum wl_seat_capability caps = 0;
2121
2122 resource = wl_client_add_object(client, &wl_seat_interface,
2123 &seat_interface, id, data);
2124 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002125 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002126
2127 if (seat->pointer)
2128 caps |= WL_SEAT_CAPABILITY_POINTER;
2129 if (seat->keyboard)
2130 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2131 if (seat->touch)
2132 caps |= WL_SEAT_CAPABILITY_TOUCH;
2133
2134 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002135}
2136
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002137static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002138device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002139{
Daniel Stone37816df2012-05-16 18:45:18 +01002140 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002141
Daniel Stone37816df2012-05-16 18:45:18 +01002142 seat = container_of(listener, struct weston_seat,
2143 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002144
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002145 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002146}
2147
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002148static void weston_compositor_xkb_init(struct weston_compositor *ec,
2149 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002150{
Daniel Stonee379da92012-05-30 16:32:04 +01002151 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002152 ec->xkb_context = xkb_context_new(0);
2153 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002154 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002155 exit(1);
2156 }
Daniel Stone994679a2012-05-30 16:31:43 +01002157 }
2158
2159 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002160 ec->xkb_names = *names;
2161 if (!ec->xkb_names.rules)
2162 ec->xkb_names.rules = strdup("evdev");
2163 if (!ec->xkb_names.model)
2164 ec->xkb_names.model = strdup("pc105");
2165 if (!ec->xkb_names.layout)
2166 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002167}
2168
Daniel Stonee379da92012-05-30 16:32:04 +01002169static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2170{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002171 if (xkb_info->keymap)
2172 xkb_map_unref(xkb_info->keymap);
2173
2174 if (xkb_info->keymap_area)
2175 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2176 if (xkb_info->keymap_fd >= 0)
2177 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002178}
2179
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002180static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2181{
Daniel Stonee379da92012-05-30 16:32:04 +01002182 free((char *) ec->xkb_names.rules);
2183 free((char *) ec->xkb_names.model);
2184 free((char *) ec->xkb_names.layout);
2185 free((char *) ec->xkb_names.variant);
2186 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002187
Daniel Stonee379da92012-05-30 16:32:04 +01002188 xkb_info_destroy(&ec->xkb_info);
2189 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002190}
2191
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002192static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002193weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002194{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002195 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002196
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002197 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2198 XKB_MOD_NAME_SHIFT);
2199 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2200 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002201 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2202 XKB_MOD_NAME_CTRL);
2203 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2204 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002205 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2206 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002207 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2208 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002209 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002210
2211 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2212 XKB_LED_NAME_NUM);
2213 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2214 XKB_LED_NAME_CAPS);
2215 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2216 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002217
2218 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2219 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002220 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002221 exit(EXIT_FAILURE);
2222 }
2223 xkb_info->keymap_size = strlen(keymap_str) + 1;
2224
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002225 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002226 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002227 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002228 (unsigned long) xkb_info->keymap_size);
2229 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002230 }
2231
2232 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2233 PROT_READ | PROT_WRITE,
2234 MAP_SHARED, xkb_info->keymap_fd, 0);
2235 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002236 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002237 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002238 goto err_dev_zero;
2239 }
2240 strcpy(xkb_info->keymap_area, keymap_str);
2241 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002242
2243 return;
2244
2245err_dev_zero:
2246 close(xkb_info->keymap_fd);
2247 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002248err_keymap_str:
2249 free(keymap_str);
2250 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002251}
2252
2253static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002254weston_compositor_build_global_keymap(struct weston_compositor *ec)
2255{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002256 if (ec->xkb_info.keymap != NULL)
2257 return;
2258
Daniel Stonee379da92012-05-30 16:32:04 +01002259 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002260 &ec->xkb_names,
2261 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002262 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002263 weston_log("failed to compile global XKB keymap\n");
2264 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002265 "options %s",
2266 ec->xkb_names.rules, ec->xkb_names.model,
2267 ec->xkb_names.layout, ec->xkb_names.variant,
2268 ec->xkb_names.options);
2269 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002270 }
2271
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002272 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002273}
2274
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002275WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002276weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002277{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002278 if (seat->has_keyboard)
2279 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002280
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002281 if (keymap != NULL) {
2282 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002283 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002284 }
2285 else {
2286 weston_compositor_build_global_keymap(seat->compositor);
2287 seat->xkb_info = seat->compositor->xkb_info;
2288 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2289 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002290
2291 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2292 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002293 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01002294 exit(1);
2295 }
2296
Daniel Stone71c38772012-06-22 13:21:30 +01002297 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002298
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002299 wl_keyboard_init(&seat->keyboard);
2300 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2301
2302 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01002303}
2304
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002305WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002306weston_seat_init_pointer(struct weston_seat *seat)
2307{
2308 if (seat->has_pointer)
2309 return;
2310
2311 wl_pointer_init(&seat->pointer);
2312 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2313
2314 seat->has_pointer = 1;
2315}
2316
2317WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002318weston_seat_init_touch(struct weston_seat *seat)
2319{
2320 if (seat->has_touch)
2321 return;
2322
2323 wl_touch_init(&seat->touch);
2324 wl_seat_set_touch(&seat->seat, &seat->touch);
2325
2326 seat->has_touch = 1;
2327}
2328
2329WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002330weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002331{
Daniel Stone37816df2012-05-16 18:45:18 +01002332 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002333 seat->has_pointer = 0;
2334 seat->has_keyboard = 0;
2335 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002336
Daniel Stone37816df2012-05-16 18:45:18 +01002337 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2338 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002339
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002340 seat->sprite = NULL;
2341 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002342
Daniel Stone37816df2012-05-16 18:45:18 +01002343 seat->compositor = ec;
2344 seat->hotspot_x = 16;
2345 seat->hotspot_y = 16;
2346 seat->modifier_state = 0;
2347 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002348
Daniel Stone37816df2012-05-16 18:45:18 +01002349 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002350 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002351
Daniel Stone37816df2012-05-16 18:45:18 +01002352 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002353
Daniel Stone37816df2012-05-16 18:45:18 +01002354 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2355 wl_signal_add(&seat->seat.drag_icon_signal,
2356 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002357
2358 clipboard_create(seat);
Jan Arne Petersene829adc2012-08-10 16:47:22 +02002359 input_method_create(ec, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002360}
2361
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002362WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002363weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002364{
Daniel Stone37816df2012-05-16 18:45:18 +01002365 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002366 /* The global object is destroyed at wl_display_destroy() time. */
2367
Daniel Stone37816df2012-05-16 18:45:18 +01002368 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002369 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002370
Daniel Stone74419a22012-05-30 16:32:02 +01002371 if (seat->xkb_state.state != NULL)
2372 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01002373 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002374
Daniel Stone37816df2012-05-16 18:45:18 +01002375 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002376}
2377
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002378static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002379drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2380{
2381 weston_surface_configure(es,
2382 es->geometry.x + sx, es->geometry.y + sy,
2383 es->buffer->width, es->buffer->height);
2384}
2385
2386static int
Daniel Stone37816df2012-05-16 18:45:18 +01002387device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002388 struct weston_surface *surface)
2389{
Daniel Stone37816df2012-05-16 18:45:18 +01002390 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002391
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002392 if (surface->configure) {
2393 wl_resource_post_error(&surface->surface.resource,
2394 WL_DISPLAY_ERROR_INVALID_OBJECT,
2395 "surface->configure already set");
2396 return 0;
2397 }
2398
Daniel Stone37816df2012-05-16 18:45:18 +01002399 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002400
Daniel Stone37816df2012-05-16 18:45:18 +01002401 weston_surface_set_position(ws->drag_surface,
2402 wl_fixed_to_double(seat->pointer->x),
2403 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002404
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002405 surface->configure = drag_surface_configure;
2406
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002407 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002408 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002409
2410 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002411}
2412
2413static void
Daniel Stone37816df2012-05-16 18:45:18 +01002414device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002415{
Daniel Stone37816df2012-05-16 18:45:18 +01002416 seat->drag_surface->configure = NULL;
2417 undef_region(&seat->drag_surface->input);
2418 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2419 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002420}
2421
2422static void
Daniel Stone37816df2012-05-16 18:45:18 +01002423device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002424{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002425 struct wl_list *list;
2426
Daniel Stone37816df2012-05-16 18:45:18 +01002427 if (weston_surface_is_mapped(seat->drag_surface) ||
2428 !seat->drag_surface->buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002429 return;
2430
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002431 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2432 list = &seat->sprite->layer_link;
2433 else
2434 list = &seat->compositor->cursor_layer.surface_list;
2435
2436 wl_list_insert(list, &seat->drag_surface->layer_link);
Daniel Stone37816df2012-05-16 18:45:18 +01002437 weston_surface_assign_output(seat->drag_surface);
2438 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002439}
2440
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002441static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002442weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01002443 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002444{
2445 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002446
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002447 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002448 return;
2449
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002450 if (seat->drag_surface && seat->seat.drag_surface &&
2451 (&seat->drag_surface->surface.resource !=
2452 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002453 /* between calls to this funcion we got a new drag_surface */
2454 surface_changed = 1;
2455
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002456 if (!seat->seat.drag_surface || surface_changed) {
2457 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002458 if (!surface_changed)
2459 return;
2460 }
2461
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002462 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002463 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002464 seat->seat.drag_surface;
2465 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002466 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002467 }
2468
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002469 /* the client may not have attached a buffer to the drag surface
2470 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002471 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002472
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02002473 /* the client may have attached a buffer with a different size to
2474 * the drag surface, causing the input region to be reset */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002475 if (region_is_undefined(&seat->drag_surface->input))
2476 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02002477
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002478 if (!dx && !dy)
2479 return;
2480
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002481 weston_surface_set_position(seat->drag_surface,
2482 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
2483 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002484}
2485
2486WL_EXPORT void
2487weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2488{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002489 struct weston_seat *seat;
2490
2491 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002492 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002493}
2494
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002495static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002496bind_output(struct wl_client *client,
2497 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002498{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002499 struct weston_output *output = data;
2500 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002501 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002502
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002503 resource = wl_client_add_object(client,
2504 &wl_output_interface, NULL, id, data);
2505
Casey Dahlin9074db52012-04-19 22:50:09 -04002506 wl_list_insert(&output->resource_list, &resource->link);
2507 resource->destroy = unbind_resource;
2508
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002509 wl_output_send_geometry(resource,
2510 output->x,
2511 output->y,
2512 output->mm_width,
2513 output->mm_height,
2514 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002515 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002516 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002517
2518 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002519 wl_output_send_mode(resource,
2520 mode->flags,
2521 mode->width,
2522 mode->height,
2523 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002524 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002525}
2526
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002527WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002528weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002529{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002530 struct weston_compositor *c = output->compositor;
2531
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002532 pixman_region32_fini(&output->region);
Kristian Høgsberg8b72f602011-06-23 20:46:34 -04002533 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002534 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002535
2536 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002537}
2538
Scott Moreau1bad5db2012-08-18 01:04:05 -06002539static void
2540weston_output_compute_transform(struct weston_output *output)
2541{
2542 struct weston_matrix transform;
2543 int flip;
2544
2545 weston_matrix_init(&transform);
2546
2547 switch(output->transform) {
2548 case WL_OUTPUT_TRANSFORM_FLIPPED:
2549 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2550 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2551 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2552 flip = -1;
2553 break;
2554 default:
2555 flip = 1;
2556 break;
2557 }
2558
2559 switch(output->transform) {
2560 case WL_OUTPUT_TRANSFORM_NORMAL:
2561 case WL_OUTPUT_TRANSFORM_FLIPPED:
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_90:
2568 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2569 transform.d[0] = 0;
2570 transform.d[1] = -flip;
2571 transform.d[4] = 1;
2572 transform.d[5] = 0;
2573 break;
2574 case WL_OUTPUT_TRANSFORM_180:
2575 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2576 transform.d[0] = -flip;
2577 transform.d[1] = 0;
2578 transform.d[4] = 0;
2579 transform.d[5] = -1;
2580 break;
2581 case WL_OUTPUT_TRANSFORM_270:
2582 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2583 transform.d[0] = 0;
2584 transform.d[1] = flip;
2585 transform.d[4] = -1;
2586 transform.d[5] = 0;
2587 break;
2588 default:
2589 break;
2590 }
2591
2592 weston_matrix_multiply(&output->matrix, &transform);
2593}
2594
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002595WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002596weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002597{
Scott Moreau850ca422012-05-21 15:21:25 -06002598 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002599 struct weston_matrix camera;
2600 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002601
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002602 weston_matrix_init(&output->matrix);
2603 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002604 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2605 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002606
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002607 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002608 2.0 / (output->width + output->border.left + output->border.right),
2609 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2610
2611 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002612
Scott Moreauccbf29d2012-02-22 14:21:41 -07002613 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002614 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002615 weston_matrix_init(&camera);
2616 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002617 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002618 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002619 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002620 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002621 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002622 weston_matrix_multiply(&output->matrix, &modelview);
2623 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002624
Scott Moreauccbf29d2012-02-22 14:21:41 -07002625 output->dirty = 0;
2626}
2627
Scott Moreau1bad5db2012-08-18 01:04:05 -06002628static void
2629weston_output_transform_init(struct weston_output *output, uint32_t transform)
2630{
2631 output->transform = transform;
2632
2633 switch (transform) {
2634 case WL_OUTPUT_TRANSFORM_90:
2635 case WL_OUTPUT_TRANSFORM_270:
2636 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2637 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2638 /* Swap width and height */
2639 output->width = output->current->height;
2640 output->height = output->current->width;
2641 break;
2642 case WL_OUTPUT_TRANSFORM_NORMAL:
2643 case WL_OUTPUT_TRANSFORM_180:
2644 case WL_OUTPUT_TRANSFORM_FLIPPED:
2645 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2646 output->width = output->current->width;
2647 output->height = output->current->height;
2648 break;
2649 default:
2650 break;
2651 }
2652}
2653
Scott Moreauccbf29d2012-02-22 14:21:41 -07002654WL_EXPORT void
2655weston_output_move(struct weston_output *output, int x, int y)
2656{
2657 output->x = x;
2658 output->y = y;
2659
2660 pixman_region32_init(&output->previous_damage);
2661 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002662 output->width,
2663 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002664}
2665
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002666WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002667weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002668 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002669{
2670 output->compositor = c;
2671 output->x = x;
2672 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002673 output->border.top = 0;
2674 output->border.bottom = 0;
2675 output->border.left = 0;
2676 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002677 output->mm_width = width;
2678 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002679 output->dirty = 1;
2680
Scott Moreau1bad5db2012-08-18 01:04:05 -06002681 if (transform != WL_OUTPUT_TRANSFORM_NORMAL)
2682 output->disable_planes++;
2683
2684 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06002685 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002686
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002687 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002688 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002689
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002690 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002691 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002692 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002693
Casey Dahlin58ba1372012-04-19 22:50:08 -04002694 output->id = ffs(~output->compositor->output_id_pool) - 1;
2695 output->compositor->output_id_pool |= 1 << output->id;
2696
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002697 output->global =
2698 wl_display_add_global(c->wl_display, &wl_output_interface,
2699 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002700}
2701
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002702static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002703compositor_bind(struct wl_client *client,
2704 void *data, uint32_t version, uint32_t id)
2705{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002706 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002707
2708 wl_client_add_object(client, &wl_compositor_interface,
2709 &compositor_interface, id, compositor);
2710}
2711
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002712static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002713log_uname(void)
2714{
2715 struct utsname usys;
2716
2717 uname(&usys);
2718
2719 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2720 usys.version, usys.machine);
2721}
2722
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002723WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002724weston_compositor_init(struct weston_compositor *ec,
2725 struct wl_display *display,
2726 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04002727 char *argv[],
2728 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002729{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002730 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002731 struct xkb_rule_names xkb_names;
2732 const struct config_key keyboard_config_keys[] = {
2733 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2734 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2735 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2736 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2737 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2738 };
2739 const struct config_section cs[] = {
2740 { "keyboard",
2741 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
2742 };
2743
2744 memset(&xkb_names, 0, sizeof(xkb_names));
2745 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002746
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002747 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002748 wl_signal_init(&ec->destroy_signal);
2749 wl_signal_init(&ec->activate_signal);
2750 wl_signal_init(&ec->lock_signal);
2751 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002752 wl_signal_init(&ec->show_input_panel_signal);
2753 wl_signal_init(&ec->hide_input_panel_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002754 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002755
Casey Dahlin58ba1372012-04-19 22:50:08 -04002756 ec->output_id_pool = 0;
2757
Kristian Høgsberga8873122011-11-23 10:39:34 -05002758 if (!wl_display_add_global(display, &wl_compositor_interface,
2759 ec, compositor_bind))
2760 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002761
Daniel Stone725c2c32012-06-22 14:04:36 +01002762 wl_list_init(&ec->surface_list);
2763 wl_list_init(&ec->layer_list);
2764 wl_list_init(&ec->seat_list);
2765 wl_list_init(&ec->output_list);
2766 wl_list_init(&ec->key_binding_list);
2767 wl_list_init(&ec->button_binding_list);
2768 wl_list_init(&ec->axis_binding_list);
2769 wl_list_init(&ec->fade.animation.link);
2770
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002771 weston_plane_init(&ec->primary_plane, 0, 0);
2772
Daniel Stone725c2c32012-06-22 14:04:36 +01002773 weston_compositor_xkb_init(ec, &xkb_names);
2774
2775 ec->ping_handler = NULL;
2776
2777 screenshooter_create(ec);
2778 text_cursor_position_notifier_create(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002779
2780 wl_data_device_manager_init(ec->wl_display);
2781
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002782 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002783
Daniel Stone725c2c32012-06-22 14:04:36 +01002784 loop = wl_display_get_event_loop(ec->wl_display);
2785 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2786 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2787
2788 ec->input_loop = wl_event_loop_create();
2789
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002790 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
2791 ec->fade.animation.frame = fade_frame;
2792
2793 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2794 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2795
2796 weston_compositor_schedule_repaint(ec);
2797
Daniel Stone725c2c32012-06-22 14:04:36 +01002798 return 0;
2799}
2800
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002801WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002802weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002803{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002804 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002805
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002806 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002807 if (ec->input_loop_source)
2808 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002809
Matt Roper361d2ad2011-08-29 13:52:23 -07002810 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002811 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002812 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002813
Daniel Stone325fc2d2012-05-30 16:31:58 +01002814 weston_binding_list_destroy_all(&ec->key_binding_list);
2815 weston_binding_list_destroy_all(&ec->button_binding_list);
2816 weston_binding_list_destroy_all(&ec->axis_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002817
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002818 weston_plane_release(&ec->primary_plane);
2819
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002820 wl_array_release(&ec->vertices);
2821 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05002822 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002823
2824 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07002825}
2826
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002827static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002828{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002829 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002830
Martin Minarik6d118362012-06-07 18:01:59 +02002831 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002832 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002833
2834 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002835}
2836
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002837static void
2838on_segv_signal(int s, siginfo_t *siginfo, void *context)
2839{
2840 void *buffer[32];
2841 int i, count;
2842 Dl_info info;
2843
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002844 /* This SIGSEGV handler will do a best-effort backtrace, and
2845 * then call the backend restore function, which will switch
2846 * back to the vt we launched from or ungrab X etc and then
2847 * raise SIGTRAP. If we run weston under gdb from X or a
2848 * different vt, and tell gdb "handle SIGSEGV nostop", this
2849 * will allow weston to switch back to gdb on crash and then
2850 * gdb will catch the crash with SIGTRAP. */
2851
Martin Minarik6d118362012-06-07 18:01:59 +02002852 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002853
2854 count = backtrace(buffer, ARRAY_LENGTH(buffer));
2855 for (i = 0; i < count; i++) {
2856 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02002857 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002858 (long) buffer[i],
2859 info.dli_sname ? info.dli_sname : "--",
2860 info.dli_fname);
2861 }
2862
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002863 segv_compositor->restore(segv_compositor);
2864
2865 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002866}
2867
2868
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002869static void *
2870load_module(const char *name, const char *entrypoint, void **handle)
2871{
2872 char path[PATH_MAX];
2873 void *module, *init;
2874
2875 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07002876 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002877 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02002878 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002879
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002880 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04002881 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002882 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002883 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002884 return NULL;
2885 }
2886
2887 init = dlsym(module, entrypoint);
2888 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002889 weston_log("Failed to lookup init function: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002890 return NULL;
2891 }
2892
2893 return init;
2894}
2895
Pekka Paalanen78a0b572012-06-06 16:59:44 +03002896static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02002897 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
2898
2899static const char xdg_wrong_message[] =
2900 "fatal: environment variable XDG_RUNTIME_DIR\n"
2901 "is set to \"%s\", which is not a directory.\n";
2902
2903static const char xdg_wrong_mode_message[] =
2904 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
2905 "correctly. Unix access mode must be 0700 but is %o,\n"
2906 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04002907 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02002908
2909static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03002910 "Refer to your distribution on how to get it, or\n"
2911 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
2912 "on how to implement it.\n";
2913
Martin Minarik37032f82012-06-18 20:15:18 +02002914static void
2915verify_xdg_runtime_dir(void)
2916{
2917 char *dir = getenv("XDG_RUNTIME_DIR");
2918 struct stat s;
2919
2920 if (!dir) {
2921 weston_log(xdg_error_message);
2922 weston_log_continue(xdg_detail_message);
2923 exit(EXIT_FAILURE);
2924 }
2925
2926 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
2927 weston_log(xdg_wrong_message, dir);
2928 weston_log_continue(xdg_detail_message);
2929 exit(EXIT_FAILURE);
2930 }
2931
2932 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
2933 weston_log(xdg_wrong_mode_message,
2934 dir, s.st_mode & 0777, s.st_uid);
2935 weston_log_continue(xdg_detail_message);
2936 }
2937}
2938
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04002939static int
2940usage(int error_code)
2941{
2942 fprintf(stderr,
2943 "Usage: weston [OPTIONS]\n\n"
2944 "This is weston version " VERSION ", the Wayland reference compositor.\n"
2945 "Weston supports multiple backends, and depending on which backend is in use\n"
2946 "different options will be accepted.\n\n"
2947
2948
2949 "Core options:\n\n"
2950 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
2951 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
2952 " -S, --socket=NAME\tName of socket to listen on\n"
2953 " -i, --idle-time=SECS\tIdle time in seconds\n"
2954 " --xserver\t\tEnable X server integration\n"
2955 " --module\t\tLoad the specified module\n"
2956 " --log==FILE\t\tLog to the given file\n"
2957 " -h, --help\t\tThis help message\n\n");
2958
2959 fprintf(stderr,
2960 "Options for drm-backend.so:\n\n"
2961 " --connector=ID\tBring up only this connector\n"
2962 " --seat=SEAT\t\tThe seat that weston should run on\n"
2963 " --tty=TTY\t\tThe tty to use\n"
2964 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
2965
2966 fprintf(stderr,
2967 "Options for x11-backend.so:\n\n"
2968 " --width=WIDTH\t\tWidth of X window\n"
2969 " --height=HEIGHT\tHeight of X window\n"
2970 " --fullscreen\t\tRun in fullscreen mode\n"
2971 " --output-count=COUNT\tCreate multiple outputs\n"
2972 " --no-input\t\tDont create input devices\n\n");
2973
2974 fprintf(stderr,
2975 "Options for wayland-backend.so:\n\n"
2976 " --width=WIDTH\t\tWidth of Wayland surface\n"
2977 " --height=HEIGHT\tHeight of Wayland surface\n"
2978 " --display=DISPLAY\tWayland display to connect to\n\n");
2979
2980 exit(error_code);
2981}
2982
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002983int main(int argc, char *argv[])
2984{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03002985 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002986 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002987 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02002988 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002989 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002990 struct sigaction segv_action;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04002991 void *shell_module, *backend_module, *xserver_module;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04002992 int (*module_init)(struct weston_compositor *ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002993 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002994 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04002995 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02002996 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002997 char *backend = NULL;
2998 char *shell = NULL;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04002999 char *module = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003000 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003001 int32_t idle_time = 300;
Scott Moreaue17f6102012-04-25 10:03:06 -06003002 int32_t xserver = 0;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003003 int32_t help = 0;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003004 char *socket_name = NULL;
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003005 char *config_file;
3006
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003007 const struct config_key shell_config_keys[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003008 { "type", CONFIG_KEY_STRING, &shell },
3009 };
3010
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003011 const struct config_section cs[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003012 { "shell",
3013 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
3014 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003015
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003016 const struct weston_option core_options[] = {
3017 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3018 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3019 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003020 { WESTON_OPTION_BOOLEAN, "xserver", 0, &xserver },
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003021 { WESTON_OPTION_STRING, "module", 0, &module },
Martin Minarik19e6f262012-06-07 13:08:46 +02003022 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003023 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003024 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003025
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003026 argc = parse_options(core_options,
3027 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003028
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003029 if (help)
3030 usage(EXIT_SUCCESS);
3031
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003032 weston_log_file_open(log);
3033
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003034 weston_log("%s\n"
3035 STAMP_SPACE "%s\n"
3036 STAMP_SPACE "Bug reports to: %s\n"
3037 STAMP_SPACE "Build: %s\n",
3038 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003039 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003040 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003041
Martin Minarik37032f82012-06-18 20:15:18 +02003042 verify_xdg_runtime_dir();
3043
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003044 display = wl_display_create();
3045
Tiago Vignatti2116b892011-08-08 05:52:59 -07003046 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003047 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3048 display);
3049 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3050 display);
3051 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3052 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003053
3054 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003055 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3056 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003057
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003058 if (!backend) {
3059 if (getenv("WAYLAND_DISPLAY"))
3060 backend = "wayland-backend.so";
3061 else if (getenv("DISPLAY"))
3062 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003063 else
3064 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003065 }
3066
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003067 config_file = config_file_path("weston.ini");
3068 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003069
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003070 backend_init = load_module(backend, "backend_init", &backend_module);
3071 if (!backend_init)
3072 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003073
Daniel Stonec1be8e52012-06-01 11:14:02 -04003074 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003075 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003076 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003077 exit(EXIT_FAILURE);
3078 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003079
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003080 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3081 segv_action.sa_sigaction = on_segv_signal;
3082 sigemptyset(&segv_action.sa_mask);
3083 sigaction(SIGSEGV, &segv_action, NULL);
3084 segv_compositor = ec;
3085
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003086 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03003087 weston_log("fatal: unhandled option: %s\n", argv[i]);
3088 if (argv[1]) {
3089 ret = EXIT_FAILURE;
3090 goto out;
3091 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003092
Daniel Stonec1be8e52012-06-01 11:14:02 -04003093 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01003094
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003095 ec->option_idle_time = idle_time;
3096 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003097
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003098 module_init = NULL;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04003099 if (xserver)
Tiago Vignatti629ce232012-05-23 23:04:14 +03003100 module_init = load_module("xwayland.so",
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003101 "weston_xserver_init",
3102 &xserver_module);
Pekka Paalanen33616392012-07-30 16:56:57 +03003103 if (module_init && module_init(ec) < 0) {
3104 ret = EXIT_FAILURE;
3105 goto out;
3106 }
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04003107
Kristian Høgsberg33d75092012-08-13 13:56:03 -04003108 if (socket_name)
3109 setenv("WAYLAND_DISPLAY", socket_name, 1);
3110
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003111 if (!shell)
3112 shell = "desktop-shell.so";
3113 module_init = load_module(shell, "shell_init", &shell_module);
Pekka Paalanen33616392012-07-30 16:56:57 +03003114 if (!module_init || module_init(ec) < 0) {
3115 ret = EXIT_FAILURE;
3116 goto out;
3117 }
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003118
3119
3120 module_init = NULL;
3121 if (module)
3122 module_init = load_module(module, "module_init", NULL);
Pekka Paalanen33616392012-07-30 16:56:57 +03003123 if (module_init && module_init(ec) < 0) {
3124 ret = EXIT_FAILURE;
3125 goto out;
3126 }
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003127
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003128 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003129 weston_log("fatal: failed to add socket: %m\n");
3130 ret = EXIT_FAILURE;
3131 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003132 }
3133
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003134 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003135 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003136
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003137 wl_display_run(display);
3138
3139 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003140 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05003141 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003142
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003143 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003144
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003145 if (ec->has_bind_display)
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003146 ec->unbind_display(ec->egl_display, display);
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003147
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003148 for (i = ARRAY_LENGTH(signals); i;)
3149 wl_event_source_remove(signals[--i]);
3150
Daniel Stone855028f2012-05-01 20:37:10 +01003151 weston_compositor_xkb_destroy(ec);
3152
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003153 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003154 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003155
Martin Minarik19e6f262012-06-07 13:08:46 +02003156 weston_log_file_close();
3157
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003158 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003159}