blob: e2e264b02ff375cfba385920ec5d01b3fed19d14 [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
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300196static void
197surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
198{
199 struct weston_surface *surface =
200 container_of(listener, struct weston_surface,
201 pending.buffer_destroy_listener);
202
203 surface->pending.buffer = NULL;
204}
205
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200206static void
207empty_region(pixman_region32_t *region)
208{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300209 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200210 pixman_region32_init(region);
211}
212
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300213static void
214region_init_infinite(pixman_region32_t *region)
215{
216 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
217 UINT32_MAX, UINT32_MAX);
218}
219
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500220WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500221weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500222{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500223 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400224
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200225 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400226 if (surface == NULL)
227 return NULL;
228
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400229 wl_signal_init(&surface->surface.resource.destroy_signal);
Kristian Høgsberg48891542012-02-23 17:38:33 -0500230
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500231 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500232 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500233
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400234 surface->surface.resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400235
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500236 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400237 surface->alpha = 1.0;
Juan Zhao46436612012-03-20 01:48:46 +0800238 surface->pitch = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400239
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200240 surface->num_textures = 0;
241 surface->num_images = 0;
242
Benjamin Franzke06286262011-05-06 19:12:33 +0200243 surface->buffer = NULL;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400244 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400245 surface->plane = &compositor->primary_plane;
Benjamin Franzke06286262011-05-06 19:12:33 +0200246
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400247 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500248 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500249 pixman_region32_init(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300250 pixman_region32_init(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200251 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500252 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400253
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400254 surface->buffer_destroy_listener.notify =
255 surface_handle_buffer_destroy;
Benjamin Franzke06286262011-05-06 19:12:33 +0200256
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200257 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200258 wl_list_insert(&surface->geometry.transformation_list,
259 &surface->transform.position.link);
260 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200261 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200262 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500263
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300264 surface->pending.buffer_destroy_listener.notify =
265 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300266 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300267 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300268 region_init_infinite(&surface->pending.input);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300269
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400270 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500271}
272
Alex Wu8811bf92012-02-28 18:07:54 +0800273WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500274weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200275 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500276{
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500277 surface->color[0] = red;
278 surface->color[1] = green;
279 surface->color[2] = blue;
280 surface->color[3] = alpha;
281 surface->shader = &surface->compositor->solid_shader;
282}
283
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400284WL_EXPORT void
285weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200286 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200287{
288 if (surface->transform.enabled) {
289 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
290
291 weston_matrix_transform(&surface->transform.matrix, &v);
292
293 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200294 weston_log("warning: numerical instability in "
Pekka Paalanenece8a012012-02-08 15:23:15 +0200295 "weston_surface_to_global(), divisor = %g\n",
296 v.f[3]);
297 *x = 0;
298 *y = 0;
299 return;
300 }
301
302 *x = v.f[0] / v.f[3];
303 *y = v.f[1] / v.f[3];
304 } else {
305 *x = sx + surface->geometry.x;
306 *y = sy + surface->geometry.y;
307 }
308}
309
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500310WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400311weston_surface_move_to_plane(struct weston_surface *surface,
312 struct weston_plane *plane)
313{
314 if (surface->plane == plane)
315 return;
316
317 weston_surface_damage_below(surface);
318 surface->plane = plane;
319 weston_surface_damage(surface);
320}
321
322WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500323weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200324{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500325 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200326
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500327 pixman_region32_init(&damage);
328 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
329 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400330 pixman_region32_union(&surface->plane->damage,
331 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500332 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200333}
334
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400335static struct wl_resource *
336find_resource_for_client(struct wl_list *list, struct wl_client *client)
337{
338 struct wl_resource *r;
339
340 wl_list_for_each(r, list, link) {
341 if (r->client == client)
342 return r;
343 }
344
345 return NULL;
346}
347
348static void
349weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
350{
351 uint32_t different = es->output_mask ^ mask;
352 uint32_t entered = mask & different;
353 uint32_t left = es->output_mask & different;
354 struct weston_output *output;
355 struct wl_resource *resource = NULL;
356 struct wl_client *client = es->surface.resource.client;
357
358 es->output_mask = mask;
359 if (es->surface.resource.client == NULL)
360 return;
361 if (different == 0)
362 return;
363
364 wl_list_for_each(output, &es->compositor->output_list, link) {
365 if (1 << output->id & different)
366 resource =
367 find_resource_for_client(&output->resource_list,
368 client);
369 if (resource == NULL)
370 continue;
371 if (1 << output->id & entered)
372 wl_surface_send_enter(&es->surface.resource, resource);
373 if (1 << output->id & left)
374 wl_surface_send_leave(&es->surface.resource, resource);
375 }
376}
377
378static void
379weston_surface_assign_output(struct weston_surface *es)
380{
381 struct weston_compositor *ec = es->compositor;
382 struct weston_output *output, *new_output;
383 pixman_region32_t region;
384 uint32_t max, area, mask;
385 pixman_box32_t *e;
386
387 new_output = NULL;
388 max = 0;
389 mask = 0;
390 pixman_region32_init(&region);
391 wl_list_for_each(output, &ec->output_list, link) {
392 pixman_region32_intersect(&region, &es->transform.boundingbox,
393 &output->region);
394
395 e = pixman_region32_extents(&region);
396 area = (e->x2 - e->x1) * (e->y2 - e->y1);
397
398 if (area > 0)
399 mask |= 1 << output->id;
400
401 if (area >= max) {
402 new_output = output;
403 max = area;
404 }
405 }
406 pixman_region32_fini(&region);
407
408 es->output = new_output;
409 weston_surface_update_output_mask(es, mask);
410}
411
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200412static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200413surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
414 int32_t width, int32_t height,
415 pixman_region32_t *bbox)
416{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200417 float min_x = HUGE_VALF, min_y = HUGE_VALF;
418 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200419 int32_t s[4][2] = {
420 { sx, sy },
421 { sx, sy + height },
422 { sx + width, sy },
423 { sx + width, sy + height }
424 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200425 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200426 int i;
427
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300428 if (width == 0 || height == 0) {
429 /* avoid rounding empty bbox to 1x1 */
430 pixman_region32_init(bbox);
431 return;
432 }
433
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200434 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200435 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400436 weston_surface_to_global_float(surface,
437 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200438 if (x < min_x)
439 min_x = x;
440 if (x > max_x)
441 max_x = x;
442 if (y < min_y)
443 min_y = y;
444 if (y > max_y)
445 max_y = y;
446 }
447
Pekka Paalanen219b9822012-02-08 15:38:37 +0200448 int_x = floorf(min_x);
449 int_y = floorf(min_y);
450 pixman_region32_init_rect(bbox, int_x, int_y,
451 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200452}
453
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200454static void
455weston_surface_update_transform_disable(struct weston_surface *surface)
456{
457 surface->transform.enabled = 0;
458
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200459 /* round off fractions when not transformed */
460 surface->geometry.x = roundf(surface->geometry.x);
461 surface->geometry.y = roundf(surface->geometry.y);
462
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200463 pixman_region32_init_rect(&surface->transform.boundingbox,
464 surface->geometry.x,
465 surface->geometry.y,
466 surface->geometry.width,
467 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500468
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400469 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500470 pixman_region32_copy(&surface->transform.opaque,
471 &surface->opaque);
472 pixman_region32_translate(&surface->transform.opaque,
473 surface->geometry.x,
474 surface->geometry.y);
475 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200476}
477
478static int
479weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200480{
481 struct weston_matrix *matrix = &surface->transform.matrix;
482 struct weston_matrix *inverse = &surface->transform.inverse;
483 struct weston_transform *tform;
484
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200485 surface->transform.enabled = 1;
486
487 /* Otherwise identity matrix, but with x and y translation. */
488 surface->transform.position.matrix.d[12] = surface->geometry.x;
489 surface->transform.position.matrix.d[13] = surface->geometry.y;
490
491 weston_matrix_init(matrix);
492 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
493 weston_matrix_multiply(matrix, &tform->matrix);
494
495 if (weston_matrix_invert(inverse, matrix) < 0) {
496 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200497 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200498 " transformation not invertible.\n", surface);
499 return -1;
500 }
501
502 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
503 surface->geometry.height,
504 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500505
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200506 return 0;
507}
508
509WL_EXPORT void
510weston_surface_update_transform(struct weston_surface *surface)
511{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200512 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200513 return;
514
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200515 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200516
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500517 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200518
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200519 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500520 pixman_region32_fini(&surface->transform.opaque);
521 pixman_region32_init(&surface->transform.opaque);
522
Pekka Paalanencd403622012-01-25 13:37:39 +0200523 /* transform.position is always in transformation_list */
524 if (surface->geometry.transformation_list.next ==
525 &surface->transform.position.link &&
526 surface->geometry.transformation_list.prev ==
527 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200528 weston_surface_update_transform_disable(surface);
529 } else {
530 if (weston_surface_update_transform_enable(surface) < 0)
531 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200532 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200533
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400534 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200535
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300536 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200537}
538
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200539WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100540weston_surface_to_global_fixed(struct weston_surface *surface,
541 wl_fixed_t sx, wl_fixed_t sy,
542 wl_fixed_t *x, wl_fixed_t *y)
543{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200544 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100545
546 weston_surface_to_global_float(surface,
547 wl_fixed_to_double(sx),
548 wl_fixed_to_double(sy),
549 &xf, &yf);
550 *x = wl_fixed_from_double(xf);
551 *y = wl_fixed_from_double(yf);
552}
553
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400554WL_EXPORT void
555weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200556 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200557{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200558 if (surface->transform.enabled) {
559 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
560
561 weston_matrix_transform(&surface->transform.inverse, &v);
562
563 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200564 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200565 "weston_surface_from_global(), divisor = %g\n",
566 v.f[3]);
567 *sx = 0;
568 *sy = 0;
569 return;
570 }
571
Pekka Paalanencd403622012-01-25 13:37:39 +0200572 *sx = v.f[0] / v.f[3];
573 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200574 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200575 *sx = x - surface->geometry.x;
576 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200577 }
578}
579
580WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100581weston_surface_from_global_fixed(struct weston_surface *surface,
582 wl_fixed_t x, wl_fixed_t y,
583 wl_fixed_t *sx, wl_fixed_t *sy)
584{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200585 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100586
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400587 weston_surface_from_global_float(surface,
588 wl_fixed_to_double(x),
589 wl_fixed_to_double(y),
590 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100591 *sx = wl_fixed_from_double(sxf);
592 *sy = wl_fixed_from_double(syf);
593}
594
595WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200596weston_surface_from_global(struct weston_surface *surface,
597 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
598{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200599 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200600
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400601 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200602 *sx = floorf(sxf);
603 *sy = floorf(syf);
604}
605
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400606WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400607weston_surface_schedule_repaint(struct weston_surface *surface)
608{
609 struct weston_output *output;
610
611 wl_list_for_each(output, &surface->compositor->output_list, link)
612 if (surface->output_mask & (1 << output->id))
613 weston_output_schedule_repaint(output);
614}
615
616WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500617weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500618{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400619 pixman_region32_union_rect(&surface->damage, &surface->damage,
620 0, 0, surface->geometry.width,
621 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200622
Kristian Høgsberg98238702012-08-03 16:29:12 -0400623 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500624}
625
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400626WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500627weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200628 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400629{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200630 surface->geometry.x = x;
631 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200632 surface->geometry.width = width;
633 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200634 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400635}
636
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200637WL_EXPORT void
638weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200639 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200640{
641 surface->geometry.x = x;
642 surface->geometry.y = y;
643 surface->geometry.dirty = 1;
644}
645
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300646WL_EXPORT int
647weston_surface_is_mapped(struct weston_surface *surface)
648{
649 if (surface->output)
650 return 1;
651 else
652 return 0;
653}
654
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400655WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500656weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500657{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400658 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500659
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400660 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500661
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400662 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500663}
664
Tiago Vignatti9d393522012-02-10 16:26:19 +0200665static struct weston_surface *
666weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100667 wl_fixed_t x, wl_fixed_t y,
668 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200669{
670 struct weston_surface *surface;
671
672 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100673 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500674 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100675 wl_fixed_to_int(*sx),
676 wl_fixed_to_int(*sy),
677 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200678 return surface;
679 }
680
681 return NULL;
682}
683
684static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400685weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500686{
Scott Moreau447013d2012-02-18 05:05:29 -0700687 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500688 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400689 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500690
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400691 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100692 return;
693
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400694 surface = weston_compositor_pick_surface(seat->compositor,
695 pointer->x,
696 pointer->y,
697 &pointer->current_x,
698 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500699
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400700 if (&surface->surface != pointer->current) {
701 interface = pointer->grab->interface;
702 pointer->current = &surface->surface;
703 interface->focus(pointer->grab, &surface->surface,
704 pointer->current_x,
705 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500706 }
707
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400708 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500709 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100710 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400711 pointer->x,
712 pointer->y,
713 &pointer->grab->x,
714 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500715}
716
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400717static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500718weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400719{
Daniel Stone37816df2012-05-16 18:45:18 +0100720 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400721
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500722 if (!compositor->focus)
723 return;
724
Daniel Stone37816df2012-05-16 18:45:18 +0100725 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400726 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400727}
728
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400729WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500730weston_surface_unmap(struct weston_surface *surface)
731{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100732 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500733
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500734 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500735 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500736 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500737
Daniel Stone4dab5db2012-05-30 16:31:53 +0100738 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100739 if (seat->seat.keyboard &&
740 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100741 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100742 if (seat->seat.pointer &&
743 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100744 wl_pointer_set_focus(seat->seat.pointer,
745 NULL,
746 wl_fixed_from_int(0),
747 wl_fixed_from_int(0));
748 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500749
Kristian Høgsberg98238702012-08-03 16:29:12 -0400750 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500751}
752
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400753struct weston_frame_callback {
754 struct wl_resource resource;
755 struct wl_list link;
756};
757
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500758static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400759destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500760{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500761 struct weston_surface *surface =
762 container_of(resource,
763 struct weston_surface, surface.resource);
764 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400765 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400766
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300767 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500768 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400769
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300770 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300771 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +0300772 pixman_region32_fini(&surface->pending.damage);
773
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300774 if (surface->pending.buffer)
775 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
776
Benjamin Franzke06286262011-05-06 19:12:33 +0200777 if (surface->buffer)
778 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400779
Kristian Høgsberg42263852012-09-06 21:59:29 -0400780 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200781
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200782 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200783 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500784 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500785 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300786 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200787
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400788 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
789 wl_resource_destroy(&cb->resource);
790
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400791 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500792}
793
Alex Wu8811bf92012-02-28 18:07:54 +0800794WL_EXPORT void
795weston_surface_destroy(struct weston_surface *surface)
796{
797 /* Not a valid way to destroy a client surface */
798 assert(surface->surface.resource.client == NULL);
799
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400800 wl_signal_emit(&surface->surface.resource.destroy_signal,
801 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800802 destroy_surface(&surface->surface.resource);
803}
804
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100805static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300806weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100807{
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300808 if (surface->buffer) {
809 weston_buffer_post_release(surface->buffer);
810 wl_list_remove(&surface->buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300811 }
812
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400813 if (buffer) {
814 buffer->busy_count++;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300815 wl_signal_add(&buffer->resource.destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300816 &surface->buffer_destroy_listener);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400817 } else {
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300818 if (weston_surface_is_mapped(surface))
819 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300820 }
821
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300822 surface->compositor->renderer->attach(surface, buffer);
823 surface->buffer = buffer;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100824}
825
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500826WL_EXPORT void
827weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400828{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500829 wl_list_remove(&surface->layer_link);
830 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500831 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500832 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400833}
834
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400835WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500836weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400837{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500838 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400839
840 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500841 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400842}
843
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500844WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500845weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +0200846{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400847 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200848 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200849
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400850 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -0500851 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200852}
853
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400854WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500855weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400856{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500857 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400858
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400859 pixman_region32_union(&compositor->primary_plane.damage,
860 &compositor->primary_plane.damage,
861 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400862 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400863}
864
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400865static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500866fade_frame(struct weston_animation *animation,
867 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400868{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500869 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400870 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500871 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500872 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400873
Scott Moreau94b0b0c2012-06-14 01:01:56 -0600874 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -0600875 compositor->fade.spring.timestamp = msecs;
876
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500877 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500878 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500879 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
880 compositor->fade.spring.current);
881 weston_surface_damage(surface);
882
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500883 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400884 compositor->fade.spring.current =
885 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400886 wl_list_remove(&animation->link);
887 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200888
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500889 if (compositor->fade.spring.current < 0.001) {
890 destroy_surface(&surface->surface.resource);
891 compositor->fade.surface = NULL;
892 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500893 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400894 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200895 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400896 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400897}
898
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400899static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400900surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400901 pixman_region32_t *opaque)
902{
903 if (surface->buffer && wl_buffer_is_shm(surface->buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400904 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400905
906 if (surface->transform.enabled) {
907 pixman_box32_t *extents;
908
909 extents = pixman_region32_extents(&surface->damage);
910 surface_compute_bbox(surface, extents->x1, extents->y1,
911 extents->x2 - extents->x1,
912 extents->y2 - extents->y1,
913 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400914 pixman_region32_translate(&surface->damage,
915 -surface->plane->x,
916 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400917 } else {
918 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400919 surface->geometry.x - surface->plane->x,
920 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400921 }
922
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400923 pixman_region32_union(&surface->plane->damage,
924 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400925 empty_region(&surface->damage);
926 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400927 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400928}
929
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400930static void
Rob Bradford0fb79752012-08-02 15:36:57 +0100931weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400932{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500933 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500934 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500935 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -0500936 struct weston_animation *animation, *next;
937 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +0200938 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300939 pixman_region32_t opaque, output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500940
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +0200941 weston_compositor_update_drag_surfaces(ec);
942
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500943 /* Rebuild the surface list and update surface transforms up front. */
944 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +0200945 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500946 wl_list_for_each(layer, &ec->layer_list, link) {
947 wl_list_for_each(es, &layer->surface_list, layer_link) {
948 weston_surface_update_transform(es);
949 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +0200950 if (es->output == output) {
951 wl_list_insert_list(&frame_callback_list,
952 &es->frame_callback_list);
953 wl_list_init(&es->frame_callback_list);
954 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500955 }
956 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +0200957
Kristian Høgsberg79af73e2012-08-03 15:45:23 -0400958 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800959 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -0400960 else
961 wl_list_for_each(es, &ec->surface_list, link)
962 weston_surface_move_to_plane(es, &ec->primary_plane);
963
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500964 pixman_region32_init(&opaque);
965
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400966 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400967 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500968
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400969 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -0400970
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400971 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400972 pixman_region32_intersect(&output_damage,
973 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300974 pixman_region32_subtract(&ec->primary_plane.damage,
975 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400976
Scott Moreauccbf29d2012-02-22 14:21:41 -0700977 if (output->dirty)
978 weston_output_update_matrix(output);
979
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400980 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400981
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500982 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500983
Kristian Høgsbergef044142011-06-21 15:02:12 -0400984 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100985
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -0400986 weston_compositor_repick(ec);
987 wl_event_loop_dispatch(ec->input_loop, 0);
988
Jonas Ådahldb773762012-06-13 00:01:21 +0200989 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500990 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400991 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500992 }
Jonas Ådahldb773762012-06-13 00:01:21 +0200993 wl_list_init(&frame_callback_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500994
Scott Moreaud64cf212012-06-08 19:40:54 -0600995 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -0600996 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -0600997 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -0600998 }
Kristian Høgsbergef044142011-06-21 15:02:12 -0400999}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001000
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001001static int
1002weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001003{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001004 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001005
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001006 wl_event_loop_dispatch(compositor->input_loop, 0);
1007
1008 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001009}
1010
1011WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001012weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001013{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001014 struct weston_compositor *compositor = output->compositor;
1015 struct wl_event_loop *loop =
1016 wl_display_get_event_loop(compositor->wl_display);
1017 int fd;
1018
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001019 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001020 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001021 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001022 return;
1023 }
1024
1025 output->repaint_scheduled = 0;
1026 if (compositor->input_loop_source)
1027 return;
1028
1029 fd = wl_event_loop_get_fd(compositor->input_loop);
1030 compositor->input_loop_source =
1031 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1032 weston_compositor_read_input, compositor);
1033}
1034
1035static void
1036idle_repaint(void *data)
1037{
1038 struct weston_output *output = data;
1039
1040 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001041}
1042
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001043WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001044weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1045{
1046 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001047 if (below != NULL)
1048 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001049}
1050
1051WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001052weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001053{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001054 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001055 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001056
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001057 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001058 return;
1059
Kristian Høgsbergef044142011-06-21 15:02:12 -04001060 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001061 output->repaint_needed = 1;
1062 if (output->repaint_scheduled)
1063 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001064
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001065 wl_event_loop_add_idle(loop, idle_repaint, output);
1066 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001067
1068 if (compositor->input_loop_source) {
1069 wl_event_source_remove(compositor->input_loop_source);
1070 compositor->input_loop_source = NULL;
1071 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001072}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001073
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001074WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001075weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1076{
1077 struct weston_output *output;
1078
1079 wl_list_for_each(output, &compositor->output_list, link)
1080 weston_output_schedule_repaint(output);
1081}
1082
1083WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001084weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001085{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001086 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001087 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001088
Scott Moreau9d1b1122012-06-08 19:40:53 -06001089 output = container_of(compositor->output_list.next,
1090 struct weston_output, link);
1091
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001092 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001093 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001094 return;
1095
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001096 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001097 surface = weston_surface_create(compositor);
1098 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001099 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001100 wl_list_insert(&compositor->fade_layer.surface_list,
1101 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03001102 weston_surface_update_transform(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001103 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001104 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001105 }
1106
1107 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001108 if (wl_list_empty(&compositor->fade.animation.link)) {
1109 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001110 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001111 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001112 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001113}
1114
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001115static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001116surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001117{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001118 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001119}
1120
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001121static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001122surface_attach(struct wl_client *client,
1123 struct wl_resource *resource,
1124 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1125{
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001126 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001127 struct wl_buffer *buffer = NULL;
1128
1129 if (buffer_resource)
1130 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001131
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001132 if (surface->pending.buffer)
1133 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001134
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001135 surface->pending.sx = sx;
1136 surface->pending.sy = sy;
1137 surface->pending.buffer = buffer;
1138 if (buffer) {
1139 wl_signal_add(&buffer->resource.destroy_signal,
1140 &surface->pending.buffer_destroy_listener);
1141 surface->pending.remove_contents = 0;
1142 } else {
1143 surface->pending.remove_contents = 1;
1144 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001145}
1146
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001147static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001148surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001149 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001150 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001151{
Pekka Paalanen8e159182012-10-10 12:49:25 +03001152 struct weston_surface *surface = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001153
Pekka Paalanen8e159182012-10-10 12:49:25 +03001154 pixman_region32_union_rect(&surface->pending.damage,
1155 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001156 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001157}
1158
Kristian Høgsberg33418202011-08-16 23:01:28 -04001159static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001160destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001161{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001162 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001163
1164 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001165 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001166}
1167
1168static void
1169surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001170 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001171{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001172 struct weston_frame_callback *cb;
1173 struct weston_surface *es = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001174
1175 cb = malloc(sizeof *cb);
1176 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001177 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001178 return;
1179 }
1180
1181 cb->resource.object.interface = &wl_callback_interface;
1182 cb->resource.object.id = callback;
1183 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001184 cb->resource.client = client;
1185 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001186
1187 wl_client_add_resource(client, &cb->resource);
Jonas Ådahldb773762012-06-13 00:01:21 +02001188 wl_list_insert(es->frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001189}
1190
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001191static void
1192surface_set_opaque_region(struct wl_client *client,
1193 struct wl_resource *resource,
1194 struct wl_resource *region_resource)
1195{
1196 struct weston_surface *surface = resource->data;
1197 struct weston_region *region;
1198
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001199 if (region_resource) {
1200 region = region_resource->data;
Pekka Paalanen512dde82012-10-10 12:49:27 +03001201 pixman_region32_copy(&surface->pending.opaque,
1202 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001203 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001204 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001205 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001206}
1207
1208static void
1209surface_set_input_region(struct wl_client *client,
1210 struct wl_resource *resource,
1211 struct wl_resource *region_resource)
1212{
1213 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001214 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001215
1216 if (region_resource) {
1217 region = region_resource->data;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001218 pixman_region32_copy(&surface->pending.input,
1219 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001220 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001221 pixman_region32_fini(&surface->pending.input);
1222 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001223 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001224}
1225
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001226static void
1227surface_commit(struct wl_client *client, struct wl_resource *resource)
1228{
1229 struct weston_surface *surface = resource->data;
1230
1231 /* wl_surface.attach */
1232 if (surface->pending.buffer || surface->pending.remove_contents)
1233 weston_surface_attach(surface, surface->pending.buffer);
1234
1235 if (surface->buffer && surface->configure)
1236 surface->configure(surface, surface->pending.sx,
1237 surface->pending.sy);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001238
1239 /* wl_surface.damage */
1240 pixman_region32_union(&surface->damage, &surface->damage,
1241 &surface->pending.damage);
1242 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001243
1244 /* wl_surface.set_opaque_region */
1245 pixman_region32_fini(&surface->opaque);
1246 pixman_region32_init_rect(&surface->opaque, 0, 0,
1247 surface->geometry.width,
1248 surface->geometry.height);
1249 pixman_region32_intersect(&surface->opaque,
1250 &surface->opaque, &surface->pending.opaque);
1251 surface->geometry.dirty = 1;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001252
1253 /* wl_surface.set_input_region */
1254 pixman_region32_fini(&surface->input);
1255 pixman_region32_init_rect(&surface->input, 0, 0,
1256 surface->geometry.width,
1257 surface->geometry.height);
1258 pixman_region32_intersect(&surface->input,
1259 &surface->input, &surface->pending.input);
1260
1261 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001262}
1263
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001264static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001265 surface_destroy,
1266 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001267 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001268 surface_frame,
1269 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001270 surface_set_input_region,
1271 surface_commit
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001272};
1273
1274static void
1275compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001276 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001277{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001278 struct weston_compositor *ec = resource->data;
1279 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001280
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001281 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001282 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001283 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001284 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001285 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001286
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001287 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001288
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001289 surface->surface.resource.object.id = id;
1290 surface->surface.resource.object.interface = &wl_surface_interface;
1291 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001292 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001293 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001294
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001295 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001296}
1297
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001298static void
1299destroy_region(struct wl_resource *resource)
1300{
1301 struct weston_region *region =
1302 container_of(resource, struct weston_region, resource);
1303
1304 pixman_region32_fini(&region->region);
1305 free(region);
1306}
1307
1308static void
1309region_destroy(struct wl_client *client, struct wl_resource *resource)
1310{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001311 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001312}
1313
1314static void
1315region_add(struct wl_client *client, struct wl_resource *resource,
1316 int32_t x, int32_t y, int32_t width, int32_t height)
1317{
1318 struct weston_region *region = resource->data;
1319
1320 pixman_region32_union_rect(&region->region, &region->region,
1321 x, y, width, height);
1322}
1323
1324static void
1325region_subtract(struct wl_client *client, struct wl_resource *resource,
1326 int32_t x, int32_t y, int32_t width, int32_t height)
1327{
1328 struct weston_region *region = resource->data;
1329 pixman_region32_t rect;
1330
1331 pixman_region32_init_rect(&rect, x, y, width, height);
1332 pixman_region32_subtract(&region->region, &region->region, &rect);
1333 pixman_region32_fini(&rect);
1334}
1335
1336static const struct wl_region_interface region_interface = {
1337 region_destroy,
1338 region_add,
1339 region_subtract
1340};
1341
1342static void
1343compositor_create_region(struct wl_client *client,
1344 struct wl_resource *resource, uint32_t id)
1345{
1346 struct weston_region *region;
1347
1348 region = malloc(sizeof *region);
1349 if (region == NULL) {
1350 wl_resource_post_no_memory(resource);
1351 return;
1352 }
1353
1354 region->resource.destroy = destroy_region;
1355
1356 region->resource.object.id = id;
1357 region->resource.object.interface = &wl_region_interface;
1358 region->resource.object.implementation =
1359 (void (**)(void)) &region_interface;
1360 region->resource.data = region;
1361
1362 pixman_region32_init(&region->region);
1363
1364 wl_client_add_resource(client, &region->resource);
1365}
1366
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001367static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001368 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001369 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001370};
1371
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001372WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001373weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001374{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001375 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1376 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001377
1378 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001379 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001380}
1381
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001382static void
1383weston_compositor_dpms_on(struct weston_compositor *compositor)
1384{
1385 struct weston_output *output;
1386
1387 wl_list_for_each(output, &compositor->output_list, link)
1388 if (output->set_dpms)
1389 output->set_dpms(output, WESTON_DPMS_ON);
1390}
1391
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001392WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001393weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001394{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001395 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1396 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001397 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001398 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001399 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001400 }
1401}
1402
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001403static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001404weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001405{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001406 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001407 compositor->idle_inhibit++;
1408}
1409
1410static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001411weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001412{
1413 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001414 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001415}
1416
1417static int
1418idle_handler(void *data)
1419{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001420 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001421
1422 if (compositor->idle_inhibit)
1423 return 1;
1424
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001425 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001426
1427 return 1;
1428}
1429
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001430WL_EXPORT void
1431weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1432{
1433 pixman_region32_init(&plane->damage);
1434 plane->x = x;
1435 plane->y = y;
1436}
1437
1438WL_EXPORT void
1439weston_plane_release(struct weston_plane *plane)
1440{
1441 pixman_region32_fini(&plane->damage);
1442}
1443
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001444static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001445weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001446
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001447static void
Daniel Stone37816df2012-05-16 18:45:18 +01001448clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001449{
Daniel Stone37816df2012-05-16 18:45:18 +01001450 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001451 struct weston_output *output, *prev = NULL;
1452 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001453
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001454 x = wl_fixed_to_int(*fx);
1455 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001456 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1457 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001458
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001459 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001460 if (pixman_region32_contains_point(&output->region,
1461 x, y, NULL))
1462 valid = 1;
1463 if (pixman_region32_contains_point(&output->region,
1464 old_x, old_y, NULL))
1465 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001466 }
Daniel Stone37816df2012-05-16 18:45:18 +01001467
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001468 if (!valid) {
1469 if (x < prev->x)
1470 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001471 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001472 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001473 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001474 if (y < prev->y)
1475 *fy = wl_fixed_from_int(prev->y);
1476 else if (y >= prev->y + prev->current->height)
1477 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001478 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001479 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001480}
1481
1482WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001483notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001484{
1485 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001486 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001487 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001488 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001489 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001490
1491 weston_compositor_activity(ec);
1492
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001493 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001494
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001495 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001496
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001497 pointer->x = x;
1498 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001499
1500 ix = wl_fixed_to_int(x);
1501 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001502
Scott Moreauccbf29d2012-02-22 14:21:41 -07001503 wl_list_for_each(output, &ec->output_list, link)
1504 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001505 pixman_region32_contains_point(&output->region,
1506 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06001507 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001508
Daniel Stone37816df2012-05-16 18:45:18 +01001509 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001510 interface = pointer->grab->interface;
1511 interface->motion(pointer->grab, time,
1512 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001513
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001514 if (seat->sprite) {
1515 weston_surface_set_position(seat->sprite,
1516 ix - seat->hotspot_x,
1517 iy - seat->hotspot_y);
1518 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001519 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001520}
1521
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001522WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001523weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001524 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001525{
Daniel Stone37816df2012-05-16 18:45:18 +01001526 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001527
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001528 if (seat->seat.keyboard) {
1529 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1530 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04001531 }
1532
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001533 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001534}
1535
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001536WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001537notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001538 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001539{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001540 struct weston_compositor *compositor = seat->compositor;
1541 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001542 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001543 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001544 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001545
Daniel Stone4dbadb12012-05-30 16:31:51 +01001546 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001547 if (compositor->ping_handler && focus)
1548 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001549 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001550 if (pointer->button_count == 0) {
1551 pointer->grab_button = button;
1552 pointer->grab_time = time;
1553 pointer->grab_x = pointer->x;
1554 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001555 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001556 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001557 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001558 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001559 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001560 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001561
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001562 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001563 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001564
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001565 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001566
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001567 if (pointer->button_count == 1)
1568 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001569 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001570}
1571
Scott Moreau210d0792012-03-22 10:47:01 -06001572WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001573notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01001574 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001575{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001576 struct weston_compositor *compositor = seat->compositor;
1577 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001578 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001579 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001580 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1581
1582 if (compositor->ping_handler && focus)
1583 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001584
1585 weston_compositor_activity(compositor);
1586
Scott Moreau6a3633d2012-03-20 08:47:59 -06001587 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001588 weston_compositor_run_axis_binding(compositor, seat,
1589 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06001590 else
1591 return;
1592
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001593 if (pointer->focus_resource)
1594 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01001595 value);
Scott Moreau210d0792012-03-22 10:47:01 -06001596}
1597
Daniel Stone05d58682012-06-22 13:21:38 +01001598WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001599notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001600{
Daniel Stone05d58682012-06-22 13:21:38 +01001601 struct wl_keyboard *keyboard = &seat->keyboard;
1602 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01001603 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001604 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01001605 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01001606 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01001607
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001608 /* Serialize and update our internal state, checking to see if it's
1609 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01001610 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1611 XKB_STATE_DEPRESSED);
1612 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1613 XKB_STATE_LATCHED);
1614 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1615 XKB_STATE_LOCKED);
1616 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01001617 XKB_STATE_EFFECTIVE);
1618
Daniel Stone71c38772012-06-22 13:21:30 +01001619 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
1620 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
1621 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
1622 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01001623 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01001624
Daniel Stone71c38772012-06-22 13:21:30 +01001625 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
1626 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
1627 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
1628 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001629
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001630 /* And update the modifier_state for bindings. */
1631 mods_lookup = mods_depressed | mods_latched;
1632 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01001633 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001634 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01001635 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001636 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01001637 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001638 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04001639 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
1640 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01001641
Daniel Stone8c8164f2012-05-30 16:31:45 +01001642 /* Finally, notify the compositor that LEDs have changed. */
1643 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001644 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001645 leds |= LED_NUM_LOCK;
1646 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001647 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001648 leds |= LED_CAPS_LOCK;
1649 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001650 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001651 leds |= LED_SCROLL_LOCK;
1652 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01001653 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01001654 seat->xkb_state.leds = leds;
1655
Daniel Stone05d58682012-06-22 13:21:38 +01001656 if (changed) {
1657 grab->interface->modifiers(grab,
1658 serial,
1659 keyboard->modifiers.mods_depressed,
1660 keyboard->modifiers.mods_latched,
1661 keyboard->modifiers.mods_locked,
1662 keyboard->modifiers.group);
1663 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001664}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001665
Daniel Stone05d58682012-06-22 13:21:38 +01001666static void
1667update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001668 enum wl_keyboard_key_state state)
1669{
1670 enum xkb_key_direction direction;
1671
1672 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1673 direction = XKB_KEY_DOWN;
1674 else
1675 direction = XKB_KEY_UP;
1676
1677 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1678 * broken keycode system, which starts at 8. */
1679 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1680
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001681 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001682}
1683
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001684WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001685notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001686 enum wl_keyboard_key_state state,
1687 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001688{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001689 struct weston_compositor *compositor = seat->compositor;
1690 struct wl_keyboard *keyboard = seat->seat.keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01001691 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001692 (struct weston_surface *) keyboard->focus;
1693 struct wl_keyboard_grab *grab = keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001694 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001695 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001696
Daniel Stonec9785ea2012-05-30 16:31:52 +01001697 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001698 if (compositor->ping_handler && focus)
1699 compositor->ping_handler(focus, serial);
1700
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001701 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001702 keyboard->grab_key = key;
1703 keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07001704 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001705 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001706 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001707
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001708 end = keyboard->keys.data + keyboard->keys.size;
1709 for (k = keyboard->keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01001710 if (*k == key) {
1711 /* Ignore server-generated repeats. */
1712 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1713 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001714 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01001715 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001716 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001717 keyboard->keys.size = (void *) end - keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001718 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001719 k = wl_array_add(&keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001720 *k = key;
1721 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001722
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001723 if (grab == &keyboard->default_grab) {
1724 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001725 state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001726 grab = keyboard->grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06001727 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001728
Daniel Stone7ace3902012-05-30 16:31:40 +01001729 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01001730
1731 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001732 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01001733 wl_display_get_serial(compositor->wl_display),
1734 key,
1735 state);
1736 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001737}
1738
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001739WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001740notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01001741 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001742{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001743 struct weston_compositor *compositor = seat->compositor;
1744 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001745
1746 if (output) {
Daniel Stone37816df2012-05-16 18:45:18 +01001747 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001748 x - pointer->x,
1749 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001750
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001751 pointer->x = x;
1752 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001753 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001754 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001755 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001756 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03001757 /* FIXME: We should call wl_pointer_set_focus(seat,
1758 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001759 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001760}
1761
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001762static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001763destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001764{
Daniel Stone37816df2012-05-16 18:45:18 +01001765 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001766
Daniel Stone37816df2012-05-16 18:45:18 +01001767 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001768 saved_kbd_focus_listener);
1769
Daniel Stone37816df2012-05-16 18:45:18 +01001770 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001771}
1772
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001773WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001774notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001775 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001776{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001777 struct weston_compositor *compositor = seat->compositor;
1778 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001779 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001780 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001781
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001782 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001783 wl_array_copy(&keyboard->keys, keys);
1784 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001785 weston_compositor_idle_inhibit(compositor);
1786 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001787 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001788 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001789 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001790
Daniel Stoneef172672012-06-22 13:21:32 +01001791 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001792 wl_array_for_each(k, &keyboard->keys) {
1793 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01001794 WL_KEYBOARD_KEY_STATE_PRESSED);
1795 }
1796
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001797 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001798
1799 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001800 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1801 wl_keyboard_set_focus(keyboard, surface);
1802 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001803 }
1804}
1805
1806WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001807notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01001808{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001809 struct weston_compositor *compositor = seat->compositor;
1810 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001811 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001812
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001813 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001814 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001815 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001816 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001817 WL_KEYBOARD_KEY_STATE_RELEASED);
1818 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001819
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001820 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001821
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001822 if (keyboard->focus) {
1823 seat->saved_kbd_focus = keyboard->focus;
1824 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01001825 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001826 wl_signal_add(&keyboard->focus->resource.destroy_signal,
1827 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001828 }
1829
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001830 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001831 /* FIXME: We really need keyboard grab cancel here to
1832 * let the grab shut down properly. As it is we leak
1833 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001834 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001835}
1836
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001837static void
Daniel Stone37816df2012-05-16 18:45:18 +01001838touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001839{
Daniel Stone37816df2012-05-16 18:45:18 +01001840 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001841 struct wl_resource *resource;
1842
Pekka Paalanen56464252012-07-31 13:21:08 +03001843 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001844 return;
1845
Pekka Paalanen56464252012-07-31 13:21:08 +03001846 if (seat->touch->focus_resource)
1847 wl_list_remove(&seat->touch->focus_listener.link);
1848 seat->touch->focus = NULL;
1849 seat->touch->focus_resource = NULL;
1850
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001851 if (surface) {
1852 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01001853 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04001854 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001855 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02001856 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001857 return;
1858 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001859
Daniel Stone37816df2012-05-16 18:45:18 +01001860 seat->touch->focus = surface;
1861 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03001862 wl_signal_add(&resource->destroy_signal,
1863 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001864 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001865}
1866
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001867/**
1868 * notify_touch - emulates button touches and notifies surfaces accordingly.
1869 *
1870 * It assumes always the correct cycle sequence until it gets here: touch_down
1871 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1872 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001873 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001874 */
1875WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001876notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001877 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001878{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001879 struct weston_compositor *ec = seat->compositor;
1880 struct wl_touch *touch = seat->seat.touch;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001881 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001882 wl_fixed_t sx, sy;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001883 uint32_t serial = 0;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001884
1885 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01001886 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001887 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001888
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001889 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001890
1891 /* the first finger down picks the surface, and all further go
1892 * to that surface for the remainder of the touch session i.e.
1893 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001894 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001895 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001896 touch_set_focus(seat, &es->surface);
1897 } else if (touch->focus) {
1898 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001899 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001900 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001901
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001902 if (touch->focus_resource && touch->focus)
1903 wl_touch_send_down(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001904 serial, time,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001905 &touch->focus->resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001906 touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001907 break;
Daniel Stone37816df2012-05-16 18:45:18 +01001908 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001909 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001910 if (!es)
1911 break;
1912
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001913 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001914 if (touch->focus_resource)
1915 wl_touch_send_motion(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001916 time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001917 break;
Daniel Stone37816df2012-05-16 18:45:18 +01001918 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001919 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001920 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001921
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001922 if (touch->focus_resource)
1923 wl_touch_send_up(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001924 serial, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001925 if (seat->num_tp == 0)
1926 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001927 break;
1928 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001929}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001930
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001931static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001932pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1933{
1934 struct weston_seat *seat = container_of(listener, struct weston_seat,
1935 sprite_destroy_listener);
1936
1937 seat->sprite = NULL;
1938}
1939
1940static void
1941pointer_cursor_surface_configure(struct weston_surface *es,
1942 int32_t dx, int32_t dy)
1943{
1944 struct weston_seat *seat = es->private;
1945 int x, y;
1946
1947 assert(es == seat->sprite);
1948
1949 seat->hotspot_x -= dx;
1950 seat->hotspot_y -= dy;
1951
1952 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
1953 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
1954
1955 weston_surface_configure(seat->sprite, x, y,
1956 es->buffer->width, es->buffer->height);
1957
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001958 empty_region(&es->pending.input);
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03001959
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001960 if (!weston_surface_is_mapped(es)) {
1961 wl_list_insert(&es->compositor->cursor_layer.surface_list,
1962 &es->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03001963 weston_surface_update_transform(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001964 }
1965}
1966
1967static void
1968pointer_unmap_sprite(struct weston_seat *seat)
1969{
1970 if (weston_surface_is_mapped(seat->sprite))
1971 weston_surface_unmap(seat->sprite);
1972
1973 wl_list_remove(&seat->sprite_destroy_listener.link);
1974 seat->sprite->configure = NULL;
1975 seat->sprite->private = NULL;
1976 seat->sprite = NULL;
1977}
1978
1979static void
1980pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1981 uint32_t serial, struct wl_resource *surface_resource,
1982 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001983{
Daniel Stone37816df2012-05-16 18:45:18 +01001984 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001985 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001986
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001987 if (surface_resource)
1988 surface = container_of(surface_resource->data,
1989 struct weston_surface, surface);
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04001990
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04001991 if (seat->seat.pointer->focus == NULL)
1992 return;
1993 if (seat->seat.pointer->focus->resource.client != client)
1994 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04001995 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001996 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03001997
1998 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03001999 if (surface->configure) {
2000 wl_resource_post_error(&surface->surface.resource,
2001 WL_DISPLAY_ERROR_INVALID_OBJECT,
2002 "surface->configure already "
2003 "set");
2004 return;
2005 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002006 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002007
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002008 if (seat->sprite)
2009 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002010
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002011 if (!surface)
2012 return;
2013
2014 wl_signal_add(&surface->surface.resource.destroy_signal,
2015 &seat->sprite_destroy_listener);
2016
2017 surface->configure = pointer_cursor_surface_configure;
2018 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002019 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002020 seat->hotspot_x = x;
2021 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002022
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04002023 if (surface->buffer)
2024 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002025}
2026
Daniel Stone37816df2012-05-16 18:45:18 +01002027static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002028 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002029};
2030
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002031static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002032handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002033{
Daniel Stone37816df2012-05-16 18:45:18 +01002034 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002035
Daniel Stone37816df2012-05-16 18:45:18 +01002036 seat = container_of(listener, struct weston_seat,
2037 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002038
Daniel Stone37816df2012-05-16 18:45:18 +01002039 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002040}
2041
Casey Dahlin9074db52012-04-19 22:50:09 -04002042static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002043{
2044 wl_list_remove(&resource->link);
2045 free(resource);
2046}
2047
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002048static void
Daniel Stone37816df2012-05-16 18:45:18 +01002049seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2050 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002051{
Daniel Stone37816df2012-05-16 18:45:18 +01002052 struct weston_seat *seat = resource->data;
2053 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002054
Daniel Stone37816df2012-05-16 18:45:18 +01002055 if (!seat->seat.pointer)
2056 return;
2057
2058 cr = wl_client_add_object(client, &wl_pointer_interface,
2059 &pointer_interface, id, seat);
2060 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002061 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002062
2063 if (seat->seat.pointer->focus &&
2064 seat->seat.pointer->focus->resource.client == client) {
2065 struct weston_surface *surface;
2066 wl_fixed_t sx, sy;
2067
2068 surface = (struct weston_surface *) seat->seat.pointer->focus;
2069 weston_surface_from_global_fixed(surface,
2070 seat->seat.pointer->x,
2071 seat->seat.pointer->y,
2072 &sx,
2073 &sy);
2074 wl_pointer_set_focus(seat->seat.pointer,
2075 seat->seat.pointer->focus,
2076 sx,
2077 sy);
2078 }
Daniel Stone37816df2012-05-16 18:45:18 +01002079}
2080
2081static void
2082seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2083 uint32_t id)
2084{
2085 struct weston_seat *seat = resource->data;
2086 struct wl_resource *cr;
2087
2088 if (!seat->seat.keyboard)
2089 return;
2090
2091 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2092 seat);
2093 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002094 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002095
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002096 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2097 seat->xkb_info.keymap_fd,
2098 seat->xkb_info.keymap_size);
2099
Daniel Stone17b13bd2012-05-30 16:31:39 +01002100 if (seat->seat.keyboard->focus &&
2101 seat->seat.keyboard->focus->resource.client == client) {
2102 wl_keyboard_set_focus(seat->seat.keyboard,
2103 seat->seat.keyboard->focus);
2104 }
Daniel Stone37816df2012-05-16 18:45:18 +01002105}
2106
2107static void
2108seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2109 uint32_t id)
2110{
2111 struct weston_seat *seat = resource->data;
2112 struct wl_resource *cr;
2113
2114 if (!seat->seat.touch)
2115 return;
2116
2117 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2118 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002119 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002120}
2121
2122static const struct wl_seat_interface seat_interface = {
2123 seat_get_pointer,
2124 seat_get_keyboard,
2125 seat_get_touch,
2126};
2127
2128static void
2129bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2130{
2131 struct wl_seat *seat = data;
2132 struct wl_resource *resource;
2133 enum wl_seat_capability caps = 0;
2134
2135 resource = wl_client_add_object(client, &wl_seat_interface,
2136 &seat_interface, id, data);
2137 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002138 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002139
2140 if (seat->pointer)
2141 caps |= WL_SEAT_CAPABILITY_POINTER;
2142 if (seat->keyboard)
2143 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2144 if (seat->touch)
2145 caps |= WL_SEAT_CAPABILITY_TOUCH;
2146
2147 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002148}
2149
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002150static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002151device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002152{
Daniel Stone37816df2012-05-16 18:45:18 +01002153 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002154
Daniel Stone37816df2012-05-16 18:45:18 +01002155 seat = container_of(listener, struct weston_seat,
2156 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002157
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002158 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002159}
2160
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002161static void weston_compositor_xkb_init(struct weston_compositor *ec,
2162 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002163{
Daniel Stonee379da92012-05-30 16:32:04 +01002164 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002165 ec->xkb_context = xkb_context_new(0);
2166 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002167 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002168 exit(1);
2169 }
Daniel Stone994679a2012-05-30 16:31:43 +01002170 }
2171
2172 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002173 ec->xkb_names = *names;
2174 if (!ec->xkb_names.rules)
2175 ec->xkb_names.rules = strdup("evdev");
2176 if (!ec->xkb_names.model)
2177 ec->xkb_names.model = strdup("pc105");
2178 if (!ec->xkb_names.layout)
2179 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002180}
2181
Daniel Stonee379da92012-05-30 16:32:04 +01002182static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2183{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002184 if (xkb_info->keymap)
2185 xkb_map_unref(xkb_info->keymap);
2186
2187 if (xkb_info->keymap_area)
2188 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2189 if (xkb_info->keymap_fd >= 0)
2190 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002191}
2192
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002193static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2194{
Daniel Stonee379da92012-05-30 16:32:04 +01002195 free((char *) ec->xkb_names.rules);
2196 free((char *) ec->xkb_names.model);
2197 free((char *) ec->xkb_names.layout);
2198 free((char *) ec->xkb_names.variant);
2199 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002200
Daniel Stonee379da92012-05-30 16:32:04 +01002201 xkb_info_destroy(&ec->xkb_info);
2202 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002203}
2204
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002205static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002206weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002207{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002208 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002209
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002210 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2211 XKB_MOD_NAME_SHIFT);
2212 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2213 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002214 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2215 XKB_MOD_NAME_CTRL);
2216 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2217 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002218 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2219 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002220 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2221 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002222 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002223
2224 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2225 XKB_LED_NAME_NUM);
2226 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2227 XKB_LED_NAME_CAPS);
2228 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2229 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002230
2231 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2232 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002233 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002234 exit(EXIT_FAILURE);
2235 }
2236 xkb_info->keymap_size = strlen(keymap_str) + 1;
2237
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002238 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002239 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002240 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002241 (unsigned long) xkb_info->keymap_size);
2242 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002243 }
2244
2245 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2246 PROT_READ | PROT_WRITE,
2247 MAP_SHARED, xkb_info->keymap_fd, 0);
2248 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002249 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002250 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002251 goto err_dev_zero;
2252 }
2253 strcpy(xkb_info->keymap_area, keymap_str);
2254 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002255
2256 return;
2257
2258err_dev_zero:
2259 close(xkb_info->keymap_fd);
2260 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002261err_keymap_str:
2262 free(keymap_str);
2263 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002264}
2265
2266static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002267weston_compositor_build_global_keymap(struct weston_compositor *ec)
2268{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002269 if (ec->xkb_info.keymap != NULL)
2270 return;
2271
Daniel Stonee379da92012-05-30 16:32:04 +01002272 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002273 &ec->xkb_names,
2274 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002275 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002276 weston_log("failed to compile global XKB keymap\n");
2277 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002278 "options %s",
2279 ec->xkb_names.rules, ec->xkb_names.model,
2280 ec->xkb_names.layout, ec->xkb_names.variant,
2281 ec->xkb_names.options);
2282 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002283 }
2284
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002285 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002286}
2287
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002288WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002289weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002290{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002291 if (seat->has_keyboard)
2292 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002293
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002294 if (keymap != NULL) {
2295 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002296 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002297 }
2298 else {
2299 weston_compositor_build_global_keymap(seat->compositor);
2300 seat->xkb_info = seat->compositor->xkb_info;
2301 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2302 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002303
2304 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2305 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002306 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01002307 exit(1);
2308 }
2309
Daniel Stone71c38772012-06-22 13:21:30 +01002310 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002311
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002312 wl_keyboard_init(&seat->keyboard);
2313 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2314
2315 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01002316}
2317
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002318WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002319weston_seat_init_pointer(struct weston_seat *seat)
2320{
2321 if (seat->has_pointer)
2322 return;
2323
2324 wl_pointer_init(&seat->pointer);
2325 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2326
2327 seat->has_pointer = 1;
2328}
2329
2330WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002331weston_seat_init_touch(struct weston_seat *seat)
2332{
2333 if (seat->has_touch)
2334 return;
2335
2336 wl_touch_init(&seat->touch);
2337 wl_seat_set_touch(&seat->seat, &seat->touch);
2338
2339 seat->has_touch = 1;
2340}
2341
2342WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002343weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002344{
Daniel Stone37816df2012-05-16 18:45:18 +01002345 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002346 seat->has_pointer = 0;
2347 seat->has_keyboard = 0;
2348 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002349
Daniel Stone37816df2012-05-16 18:45:18 +01002350 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2351 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002352
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002353 seat->sprite = NULL;
2354 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002355
Daniel Stone37816df2012-05-16 18:45:18 +01002356 seat->compositor = ec;
2357 seat->hotspot_x = 16;
2358 seat->hotspot_y = 16;
2359 seat->modifier_state = 0;
2360 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002361
Daniel Stone37816df2012-05-16 18:45:18 +01002362 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002363 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002364
Daniel Stone37816df2012-05-16 18:45:18 +01002365 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002366
Daniel Stone37816df2012-05-16 18:45:18 +01002367 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2368 wl_signal_add(&seat->seat.drag_icon_signal,
2369 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002370
2371 clipboard_create(seat);
Jan Arne Petersene829adc2012-08-10 16:47:22 +02002372 input_method_create(ec, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002373}
2374
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002375WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002376weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002377{
Daniel Stone37816df2012-05-16 18:45:18 +01002378 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002379 /* The global object is destroyed at wl_display_destroy() time. */
2380
Daniel Stone37816df2012-05-16 18:45:18 +01002381 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002382 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002383
Daniel Stone74419a22012-05-30 16:32:02 +01002384 if (seat->xkb_state.state != NULL)
2385 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01002386 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002387
Daniel Stone37816df2012-05-16 18:45:18 +01002388 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002389}
2390
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002391static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002392drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2393{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002394 empty_region(&es->pending.input);
2395
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002396 weston_surface_configure(es,
2397 es->geometry.x + sx, es->geometry.y + sy,
2398 es->buffer->width, es->buffer->height);
2399}
2400
2401static int
Daniel Stone37816df2012-05-16 18:45:18 +01002402device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002403 struct weston_surface *surface)
2404{
Daniel Stone37816df2012-05-16 18:45:18 +01002405 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002406
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002407 if (surface->configure) {
2408 wl_resource_post_error(&surface->surface.resource,
2409 WL_DISPLAY_ERROR_INVALID_OBJECT,
2410 "surface->configure already set");
2411 return 0;
2412 }
2413
Daniel Stone37816df2012-05-16 18:45:18 +01002414 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002415
Daniel Stone37816df2012-05-16 18:45:18 +01002416 weston_surface_set_position(ws->drag_surface,
2417 wl_fixed_to_double(seat->pointer->x),
2418 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002419
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002420 surface->configure = drag_surface_configure;
2421
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002422 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002423 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002424
2425 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002426}
2427
2428static void
Daniel Stone37816df2012-05-16 18:45:18 +01002429device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002430{
Daniel Stone37816df2012-05-16 18:45:18 +01002431 seat->drag_surface->configure = NULL;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002432 empty_region(&seat->drag_surface->pending.input);
Daniel Stone37816df2012-05-16 18:45:18 +01002433 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2434 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002435}
2436
2437static void
Daniel Stone37816df2012-05-16 18:45:18 +01002438device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002439{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002440 struct wl_list *list;
2441
Daniel Stone37816df2012-05-16 18:45:18 +01002442 if (weston_surface_is_mapped(seat->drag_surface) ||
2443 !seat->drag_surface->buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002444 return;
2445
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002446 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2447 list = &seat->sprite->layer_link;
2448 else
2449 list = &seat->compositor->cursor_layer.surface_list;
2450
2451 wl_list_insert(list, &seat->drag_surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002452 weston_surface_update_transform(seat->drag_surface);
Daniel Stone37816df2012-05-16 18:45:18 +01002453 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002454}
2455
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002456static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002457weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01002458 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002459{
2460 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002461
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002462 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002463 return;
2464
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002465 if (seat->drag_surface && seat->seat.drag_surface &&
2466 (&seat->drag_surface->surface.resource !=
2467 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002468 /* between calls to this funcion we got a new drag_surface */
2469 surface_changed = 1;
2470
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002471 if (!seat->seat.drag_surface || surface_changed) {
2472 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002473 if (!surface_changed)
2474 return;
2475 }
2476
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002477 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002478 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002479 seat->seat.drag_surface;
2480 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002481 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002482 }
2483
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002484 /* the client may not have attached a buffer to the drag surface
2485 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002486 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002487
2488 if (!dx && !dy)
2489 return;
2490
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002491 weston_surface_set_position(seat->drag_surface,
2492 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
2493 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002494}
2495
2496WL_EXPORT void
2497weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2498{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002499 struct weston_seat *seat;
2500
2501 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002502 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002503}
2504
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002505static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002506bind_output(struct wl_client *client,
2507 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002508{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002509 struct weston_output *output = data;
2510 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002511 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002512
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002513 resource = wl_client_add_object(client,
2514 &wl_output_interface, NULL, id, data);
2515
Casey Dahlin9074db52012-04-19 22:50:09 -04002516 wl_list_insert(&output->resource_list, &resource->link);
2517 resource->destroy = unbind_resource;
2518
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002519 wl_output_send_geometry(resource,
2520 output->x,
2521 output->y,
2522 output->mm_width,
2523 output->mm_height,
2524 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002525 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002526 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002527
2528 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002529 wl_output_send_mode(resource,
2530 mode->flags,
2531 mode->width,
2532 mode->height,
2533 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002534 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002535}
2536
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002537WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002538weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002539{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002540 struct weston_compositor *c = output->compositor;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002541 int i;
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002542
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002543 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002544
2545 for (i = 0; i < 2; i++)
2546 pixman_region32_fini(&output->buffer_damage[i]);
2547
Casey Dahlin9074db52012-04-19 22:50:09 -04002548 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002549
2550 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002551}
2552
Scott Moreau1bad5db2012-08-18 01:04:05 -06002553static void
2554weston_output_compute_transform(struct weston_output *output)
2555{
2556 struct weston_matrix transform;
2557 int flip;
2558
2559 weston_matrix_init(&transform);
2560
2561 switch(output->transform) {
2562 case WL_OUTPUT_TRANSFORM_FLIPPED:
2563 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2564 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2565 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2566 flip = -1;
2567 break;
2568 default:
2569 flip = 1;
2570 break;
2571 }
2572
2573 switch(output->transform) {
2574 case WL_OUTPUT_TRANSFORM_NORMAL:
2575 case WL_OUTPUT_TRANSFORM_FLIPPED:
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_90:
2582 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2583 transform.d[0] = 0;
2584 transform.d[1] = -flip;
2585 transform.d[4] = 1;
2586 transform.d[5] = 0;
2587 break;
2588 case WL_OUTPUT_TRANSFORM_180:
2589 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2590 transform.d[0] = -flip;
2591 transform.d[1] = 0;
2592 transform.d[4] = 0;
2593 transform.d[5] = -1;
2594 break;
2595 case WL_OUTPUT_TRANSFORM_270:
2596 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2597 transform.d[0] = 0;
2598 transform.d[1] = flip;
2599 transform.d[4] = -1;
2600 transform.d[5] = 0;
2601 break;
2602 default:
2603 break;
2604 }
2605
2606 weston_matrix_multiply(&output->matrix, &transform);
2607}
2608
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002609WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002610weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002611{
Scott Moreau850ca422012-05-21 15:21:25 -06002612 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002613 struct weston_matrix camera;
2614 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002615
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002616 weston_matrix_init(&output->matrix);
2617 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002618 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2619 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002620
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002621 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002622 2.0 / (output->width + output->border.left + output->border.right),
2623 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2624
2625 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002626
Scott Moreauccbf29d2012-02-22 14:21:41 -07002627 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002628 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002629 weston_matrix_init(&camera);
2630 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002631 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002632 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002633 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002634 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002635 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002636 weston_matrix_multiply(&output->matrix, &modelview);
2637 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002638
Scott Moreauccbf29d2012-02-22 14:21:41 -07002639 output->dirty = 0;
2640}
2641
Scott Moreau1bad5db2012-08-18 01:04:05 -06002642static void
2643weston_output_transform_init(struct weston_output *output, uint32_t transform)
2644{
2645 output->transform = transform;
2646
2647 switch (transform) {
2648 case WL_OUTPUT_TRANSFORM_90:
2649 case WL_OUTPUT_TRANSFORM_270:
2650 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2651 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2652 /* Swap width and height */
2653 output->width = output->current->height;
2654 output->height = output->current->width;
2655 break;
2656 case WL_OUTPUT_TRANSFORM_NORMAL:
2657 case WL_OUTPUT_TRANSFORM_180:
2658 case WL_OUTPUT_TRANSFORM_FLIPPED:
2659 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2660 output->width = output->current->width;
2661 output->height = output->current->height;
2662 break;
2663 default:
2664 break;
2665 }
2666}
2667
Scott Moreauccbf29d2012-02-22 14:21:41 -07002668WL_EXPORT void
2669weston_output_move(struct weston_output *output, int x, int y)
2670{
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002671 int i;
2672
Scott Moreauccbf29d2012-02-22 14:21:41 -07002673 output->x = x;
2674 output->y = y;
2675
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002676 output->current_buffer = 0;
2677 for (i = 0; i < 2; i++)
2678 pixman_region32_init(&output->buffer_damage[i]);
2679
Scott Moreauccbf29d2012-02-22 14:21:41 -07002680 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002681 output->width,
2682 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002683}
2684
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002685WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002686weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002687 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002688{
2689 output->compositor = c;
2690 output->x = x;
2691 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002692 output->border.top = 0;
2693 output->border.bottom = 0;
2694 output->border.left = 0;
2695 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002696 output->mm_width = width;
2697 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002698 output->dirty = 1;
2699
Scott Moreau1bad5db2012-08-18 01:04:05 -06002700 if (transform != WL_OUTPUT_TRANSFORM_NORMAL)
2701 output->disable_planes++;
2702
2703 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06002704 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002705
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002706 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002707 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002708
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002709 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002710 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002711 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002712
Casey Dahlin58ba1372012-04-19 22:50:08 -04002713 output->id = ffs(~output->compositor->output_id_pool) - 1;
2714 output->compositor->output_id_pool |= 1 << output->id;
2715
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002716 output->global =
2717 wl_display_add_global(c->wl_display, &wl_output_interface,
2718 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002719}
2720
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002721static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002722compositor_bind(struct wl_client *client,
2723 void *data, uint32_t version, uint32_t id)
2724{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002725 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002726
2727 wl_client_add_object(client, &wl_compositor_interface,
2728 &compositor_interface, id, compositor);
2729}
2730
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002731static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002732log_uname(void)
2733{
2734 struct utsname usys;
2735
2736 uname(&usys);
2737
2738 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2739 usys.version, usys.machine);
2740}
2741
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002742WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002743weston_compositor_init(struct weston_compositor *ec,
2744 struct wl_display *display,
2745 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04002746 char *argv[],
2747 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002748{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002749 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002750 struct xkb_rule_names xkb_names;
2751 const struct config_key keyboard_config_keys[] = {
2752 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2753 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2754 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2755 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2756 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2757 };
2758 const struct config_section cs[] = {
2759 { "keyboard",
2760 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
2761 };
2762
2763 memset(&xkb_names, 0, sizeof(xkb_names));
2764 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002765
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002766 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002767 wl_signal_init(&ec->destroy_signal);
2768 wl_signal_init(&ec->activate_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002769 wl_signal_init(&ec->kill_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002770 wl_signal_init(&ec->lock_signal);
2771 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002772 wl_signal_init(&ec->show_input_panel_signal);
2773 wl_signal_init(&ec->hide_input_panel_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002774 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002775
Casey Dahlin58ba1372012-04-19 22:50:08 -04002776 ec->output_id_pool = 0;
2777
Kristian Høgsberga8873122011-11-23 10:39:34 -05002778 if (!wl_display_add_global(display, &wl_compositor_interface,
2779 ec, compositor_bind))
2780 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002781
Daniel Stone725c2c32012-06-22 14:04:36 +01002782 wl_list_init(&ec->surface_list);
2783 wl_list_init(&ec->layer_list);
2784 wl_list_init(&ec->seat_list);
2785 wl_list_init(&ec->output_list);
2786 wl_list_init(&ec->key_binding_list);
2787 wl_list_init(&ec->button_binding_list);
2788 wl_list_init(&ec->axis_binding_list);
2789 wl_list_init(&ec->fade.animation.link);
2790
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002791 weston_plane_init(&ec->primary_plane, 0, 0);
2792
Daniel Stone725c2c32012-06-22 14:04:36 +01002793 weston_compositor_xkb_init(ec, &xkb_names);
2794
2795 ec->ping_handler = NULL;
2796
2797 screenshooter_create(ec);
2798 text_cursor_position_notifier_create(ec);
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +02002799 text_model_factory_create(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002800
2801 wl_data_device_manager_init(ec->wl_display);
2802
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002803 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002804
Daniel Stone725c2c32012-06-22 14:04:36 +01002805 loop = wl_display_get_event_loop(ec->wl_display);
2806 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2807 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2808
2809 ec->input_loop = wl_event_loop_create();
2810
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002811 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
2812 ec->fade.animation.frame = fade_frame;
2813
2814 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2815 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2816
2817 weston_compositor_schedule_repaint(ec);
2818
Daniel Stone725c2c32012-06-22 14:04:36 +01002819 return 0;
2820}
2821
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002822WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002823weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002824{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002825 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002826
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002827 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002828 if (ec->input_loop_source)
2829 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002830
Matt Roper361d2ad2011-08-29 13:52:23 -07002831 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002832 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002833 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002834
Daniel Stone325fc2d2012-05-30 16:31:58 +01002835 weston_binding_list_destroy_all(&ec->key_binding_list);
2836 weston_binding_list_destroy_all(&ec->button_binding_list);
2837 weston_binding_list_destroy_all(&ec->axis_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002838
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002839 weston_plane_release(&ec->primary_plane);
2840
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002841 wl_array_release(&ec->vertices);
2842 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05002843 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002844
2845 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07002846}
2847
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002848static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002849{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002850 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002851
Martin Minarik6d118362012-06-07 18:01:59 +02002852 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002853 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002854
2855 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002856}
2857
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002858static void
2859on_segv_signal(int s, siginfo_t *siginfo, void *context)
2860{
2861 void *buffer[32];
2862 int i, count;
2863 Dl_info info;
2864
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002865 /* This SIGSEGV handler will do a best-effort backtrace, and
2866 * then call the backend restore function, which will switch
2867 * back to the vt we launched from or ungrab X etc and then
2868 * raise SIGTRAP. If we run weston under gdb from X or a
2869 * different vt, and tell gdb "handle SIGSEGV nostop", this
2870 * will allow weston to switch back to gdb on crash and then
2871 * gdb will catch the crash with SIGTRAP. */
2872
Martin Minarik6d118362012-06-07 18:01:59 +02002873 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002874
2875 count = backtrace(buffer, ARRAY_LENGTH(buffer));
2876 for (i = 0; i < count; i++) {
2877 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02002878 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002879 (long) buffer[i],
2880 info.dli_sname ? info.dli_sname : "--",
2881 info.dli_fname);
2882 }
2883
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002884 segv_compositor->restore(segv_compositor);
2885
2886 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002887}
2888
2889
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002890static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04002891load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002892{
2893 char path[PATH_MAX];
2894 void *module, *init;
2895
2896 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07002897 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002898 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02002899 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002900
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002901 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
2902 if (module) {
2903 weston_log("Module '%s' already loaded\n", path);
2904 dlclose(module);
2905 return NULL;
2906 }
2907
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002908 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04002909 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002910 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002911 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002912 return NULL;
2913 }
2914
2915 init = dlsym(module, entrypoint);
2916 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002917 weston_log("Failed to lookup init function: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002918 return NULL;
2919 }
2920
2921 return init;
2922}
2923
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002924static int
2925load_modules(struct weston_compositor *ec, const char *modules)
2926{
2927 const char *p, *end;
2928 char buffer[256];
2929 int (*module_init)(struct weston_compositor *ec);
2930
2931 if (modules == NULL)
2932 return 0;
2933
2934 p = modules;
2935 while (*p) {
2936 end = strchrnul(p, ',');
2937 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
2938 module_init = load_module(buffer, "module_init");
2939 if (module_init)
2940 module_init(ec);
2941 p = end;
2942 while (*p == ',')
2943 p++;
2944
2945 }
2946
2947 return 0;
2948}
2949
Pekka Paalanen78a0b572012-06-06 16:59:44 +03002950static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02002951 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
2952
2953static const char xdg_wrong_message[] =
2954 "fatal: environment variable XDG_RUNTIME_DIR\n"
2955 "is set to \"%s\", which is not a directory.\n";
2956
2957static const char xdg_wrong_mode_message[] =
2958 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
2959 "correctly. Unix access mode must be 0700 but is %o,\n"
2960 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04002961 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02002962
2963static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03002964 "Refer to your distribution on how to get it, or\n"
2965 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
2966 "on how to implement it.\n";
2967
Martin Minarik37032f82012-06-18 20:15:18 +02002968static void
2969verify_xdg_runtime_dir(void)
2970{
2971 char *dir = getenv("XDG_RUNTIME_DIR");
2972 struct stat s;
2973
2974 if (!dir) {
2975 weston_log(xdg_error_message);
2976 weston_log_continue(xdg_detail_message);
2977 exit(EXIT_FAILURE);
2978 }
2979
2980 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
2981 weston_log(xdg_wrong_message, dir);
2982 weston_log_continue(xdg_detail_message);
2983 exit(EXIT_FAILURE);
2984 }
2985
2986 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
2987 weston_log(xdg_wrong_mode_message,
2988 dir, s.st_mode & 0777, s.st_uid);
2989 weston_log_continue(xdg_detail_message);
2990 }
2991}
2992
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04002993static int
2994usage(int error_code)
2995{
2996 fprintf(stderr,
2997 "Usage: weston [OPTIONS]\n\n"
2998 "This is weston version " VERSION ", the Wayland reference compositor.\n"
2999 "Weston supports multiple backends, and depending on which backend is in use\n"
3000 "different options will be accepted.\n\n"
3001
3002
3003 "Core options:\n\n"
3004 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3005 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3006 " -S, --socket=NAME\tName of socket to listen on\n"
3007 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003008 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003009 " --log==FILE\t\tLog to the given file\n"
3010 " -h, --help\t\tThis help message\n\n");
3011
3012 fprintf(stderr,
3013 "Options for drm-backend.so:\n\n"
3014 " --connector=ID\tBring up only this connector\n"
3015 " --seat=SEAT\t\tThe seat that weston should run on\n"
3016 " --tty=TTY\t\tThe tty to use\n"
3017 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3018
3019 fprintf(stderr,
3020 "Options for x11-backend.so:\n\n"
3021 " --width=WIDTH\t\tWidth of X window\n"
3022 " --height=HEIGHT\tHeight of X window\n"
3023 " --fullscreen\t\tRun in fullscreen mode\n"
3024 " --output-count=COUNT\tCreate multiple outputs\n"
3025 " --no-input\t\tDont create input devices\n\n");
3026
3027 fprintf(stderr,
3028 "Options for wayland-backend.so:\n\n"
3029 " --width=WIDTH\t\tWidth of Wayland surface\n"
3030 " --height=HEIGHT\tHeight of Wayland surface\n"
3031 " --display=DISPLAY\tWayland display to connect to\n\n");
3032
3033 exit(error_code);
3034}
3035
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003036int main(int argc, char *argv[])
3037{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003038 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003039 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003040 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003041 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003042 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003043 struct sigaction segv_action;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003044 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003045 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003046 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003047 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003048 char *backend = NULL;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003049 const char *modules = "desktop-shell.so", *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003050 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003051 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003052 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003053 char *socket_name = "wayland-0";
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003054 char *config_file;
3055
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003056 const struct config_key core_config_keys[] = {
3057 { "modules", CONFIG_KEY_STRING, &modules },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003058 };
3059
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003060 const struct config_section cs[] = {
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003061 { "core",
3062 core_config_keys, ARRAY_LENGTH(core_config_keys) },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003063 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003064
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003065 const struct weston_option core_options[] = {
3066 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3067 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3068 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003069 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003070 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003071 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003072 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003073
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003074 argc = parse_options(core_options,
3075 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003076
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003077 if (help)
3078 usage(EXIT_SUCCESS);
3079
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003080 weston_log_file_open(log);
3081
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003082 weston_log("%s\n"
3083 STAMP_SPACE "%s\n"
3084 STAMP_SPACE "Bug reports to: %s\n"
3085 STAMP_SPACE "Build: %s\n",
3086 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003087 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003088 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003089
Martin Minarik37032f82012-06-18 20:15:18 +02003090 verify_xdg_runtime_dir();
3091
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003092 display = wl_display_create();
3093
Tiago Vignatti2116b892011-08-08 05:52:59 -07003094 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003095 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3096 display);
3097 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3098 display);
3099 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3100 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003101
3102 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003103 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3104 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003105
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003106 if (!backend) {
3107 if (getenv("WAYLAND_DISPLAY"))
3108 backend = "wayland-backend.so";
3109 else if (getenv("DISPLAY"))
3110 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003111 else
3112 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003113 }
3114
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003115 config_file = config_file_path("weston.ini");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003116 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003117
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003118 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003119 if (!backend_init)
3120 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003121
Daniel Stonec1be8e52012-06-01 11:14:02 -04003122 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003123 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003124 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003125 exit(EXIT_FAILURE);
3126 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003127
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003128 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3129 segv_action.sa_sigaction = on_segv_signal;
3130 sigemptyset(&segv_action.sa_mask);
3131 sigaction(SIGSEGV, &segv_action, NULL);
3132 segv_compositor = ec;
3133
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003134 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03003135 weston_log("fatal: unhandled option: %s\n", argv[i]);
3136 if (argv[1]) {
3137 ret = EXIT_FAILURE;
3138 goto out;
3139 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003140
Daniel Stonec1be8e52012-06-01 11:14:02 -04003141 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01003142
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003143 ec->option_idle_time = idle_time;
3144 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003145
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003146 setenv("WAYLAND_DISPLAY", socket_name, 1);
3147
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003148 if (load_modules(ec, modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003149 goto out;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003150 if (load_modules(ec, option_modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003151 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003152
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003153 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003154 weston_log("fatal: failed to add socket: %m\n");
3155 ret = EXIT_FAILURE;
3156 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003157 }
3158
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003159 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003160 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003161
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003162 wl_display_run(display);
3163
3164 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003165 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05003166 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003167
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003168 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003169
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003170 for (i = ARRAY_LENGTH(signals); i;)
3171 wl_event_source_remove(signals[--i]);
3172
Daniel Stone855028f2012-05-01 20:37:10 +01003173 weston_compositor_xkb_destroy(ec);
3174
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003175 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003176 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003177
Martin Minarik19e6f262012-06-07 13:08:46 +02003178 weston_log_file_close();
3179
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003180 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003181}