blob: 56474a5e820ac499897ba9691607c2dae128b55b [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 Paalanen8ec4ab62012-10-10 12:49:32 +0300250 region_init_infinite(&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 Paalanenbc106382012-10-10 12:49:31 +0300269 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300270
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400271 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500272}
273
Alex Wu8811bf92012-02-28 18:07:54 +0800274WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500275weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200276 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500277{
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500278 surface->color[0] = red;
279 surface->color[1] = green;
280 surface->color[2] = blue;
281 surface->color[3] = alpha;
282 surface->shader = &surface->compositor->solid_shader;
283}
284
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400285WL_EXPORT void
286weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200287 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200288{
289 if (surface->transform.enabled) {
290 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
291
292 weston_matrix_transform(&surface->transform.matrix, &v);
293
294 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200295 weston_log("warning: numerical instability in "
Pekka Paalanenece8a012012-02-08 15:23:15 +0200296 "weston_surface_to_global(), divisor = %g\n",
297 v.f[3]);
298 *x = 0;
299 *y = 0;
300 return;
301 }
302
303 *x = v.f[0] / v.f[3];
304 *y = v.f[1] / v.f[3];
305 } else {
306 *x = sx + surface->geometry.x;
307 *y = sy + surface->geometry.y;
308 }
309}
310
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500311WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400312weston_surface_move_to_plane(struct weston_surface *surface,
313 struct weston_plane *plane)
314{
315 if (surface->plane == plane)
316 return;
317
318 weston_surface_damage_below(surface);
319 surface->plane = plane;
320 weston_surface_damage(surface);
321}
322
323WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500324weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200325{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500326 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200327
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500328 pixman_region32_init(&damage);
329 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
330 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400331 pixman_region32_union(&surface->plane->damage,
332 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500333 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200334}
335
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400336static struct wl_resource *
337find_resource_for_client(struct wl_list *list, struct wl_client *client)
338{
339 struct wl_resource *r;
340
341 wl_list_for_each(r, list, link) {
342 if (r->client == client)
343 return r;
344 }
345
346 return NULL;
347}
348
349static void
350weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
351{
352 uint32_t different = es->output_mask ^ mask;
353 uint32_t entered = mask & different;
354 uint32_t left = es->output_mask & different;
355 struct weston_output *output;
356 struct wl_resource *resource = NULL;
357 struct wl_client *client = es->surface.resource.client;
358
359 es->output_mask = mask;
360 if (es->surface.resource.client == NULL)
361 return;
362 if (different == 0)
363 return;
364
365 wl_list_for_each(output, &es->compositor->output_list, link) {
366 if (1 << output->id & different)
367 resource =
368 find_resource_for_client(&output->resource_list,
369 client);
370 if (resource == NULL)
371 continue;
372 if (1 << output->id & entered)
373 wl_surface_send_enter(&es->surface.resource, resource);
374 if (1 << output->id & left)
375 wl_surface_send_leave(&es->surface.resource, resource);
376 }
377}
378
379static void
380weston_surface_assign_output(struct weston_surface *es)
381{
382 struct weston_compositor *ec = es->compositor;
383 struct weston_output *output, *new_output;
384 pixman_region32_t region;
385 uint32_t max, area, mask;
386 pixman_box32_t *e;
387
388 new_output = NULL;
389 max = 0;
390 mask = 0;
391 pixman_region32_init(&region);
392 wl_list_for_each(output, &ec->output_list, link) {
393 pixman_region32_intersect(&region, &es->transform.boundingbox,
394 &output->region);
395
396 e = pixman_region32_extents(&region);
397 area = (e->x2 - e->x1) * (e->y2 - e->y1);
398
399 if (area > 0)
400 mask |= 1 << output->id;
401
402 if (area >= max) {
403 new_output = output;
404 max = area;
405 }
406 }
407 pixman_region32_fini(&region);
408
409 es->output = new_output;
410 weston_surface_update_output_mask(es, mask);
411}
412
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200413static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200414surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
415 int32_t width, int32_t height,
416 pixman_region32_t *bbox)
417{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200418 float min_x = HUGE_VALF, min_y = HUGE_VALF;
419 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200420 int32_t s[4][2] = {
421 { sx, sy },
422 { sx, sy + height },
423 { sx + width, sy },
424 { sx + width, sy + height }
425 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200426 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200427 int i;
428
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300429 if (width == 0 || height == 0) {
430 /* avoid rounding empty bbox to 1x1 */
431 pixman_region32_init(bbox);
432 return;
433 }
434
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200435 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200436 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400437 weston_surface_to_global_float(surface,
438 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200439 if (x < min_x)
440 min_x = x;
441 if (x > max_x)
442 max_x = x;
443 if (y < min_y)
444 min_y = y;
445 if (y > max_y)
446 max_y = y;
447 }
448
Pekka Paalanen219b9822012-02-08 15:38:37 +0200449 int_x = floorf(min_x);
450 int_y = floorf(min_y);
451 pixman_region32_init_rect(bbox, int_x, int_y,
452 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200453}
454
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200455static void
456weston_surface_update_transform_disable(struct weston_surface *surface)
457{
458 surface->transform.enabled = 0;
459
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200460 /* round off fractions when not transformed */
461 surface->geometry.x = roundf(surface->geometry.x);
462 surface->geometry.y = roundf(surface->geometry.y);
463
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200464 pixman_region32_init_rect(&surface->transform.boundingbox,
465 surface->geometry.x,
466 surface->geometry.y,
467 surface->geometry.width,
468 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500469
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400470 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500471 pixman_region32_copy(&surface->transform.opaque,
472 &surface->opaque);
473 pixman_region32_translate(&surface->transform.opaque,
474 surface->geometry.x,
475 surface->geometry.y);
476 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200477}
478
479static int
480weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200481{
482 struct weston_matrix *matrix = &surface->transform.matrix;
483 struct weston_matrix *inverse = &surface->transform.inverse;
484 struct weston_transform *tform;
485
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200486 surface->transform.enabled = 1;
487
488 /* Otherwise identity matrix, but with x and y translation. */
489 surface->transform.position.matrix.d[12] = surface->geometry.x;
490 surface->transform.position.matrix.d[13] = surface->geometry.y;
491
492 weston_matrix_init(matrix);
493 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
494 weston_matrix_multiply(matrix, &tform->matrix);
495
496 if (weston_matrix_invert(inverse, matrix) < 0) {
497 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200498 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200499 " transformation not invertible.\n", surface);
500 return -1;
501 }
502
503 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
504 surface->geometry.height,
505 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500506
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200507 return 0;
508}
509
510WL_EXPORT void
511weston_surface_update_transform(struct weston_surface *surface)
512{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200513 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200514 return;
515
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200516 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200517
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500518 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200519
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200520 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500521 pixman_region32_fini(&surface->transform.opaque);
522 pixman_region32_init(&surface->transform.opaque);
523
Pekka Paalanencd403622012-01-25 13:37:39 +0200524 /* transform.position is always in transformation_list */
525 if (surface->geometry.transformation_list.next ==
526 &surface->transform.position.link &&
527 surface->geometry.transformation_list.prev ==
528 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200529 weston_surface_update_transform_disable(surface);
530 } else {
531 if (weston_surface_update_transform_enable(surface) < 0)
532 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200533 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200534
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400535 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200536
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300537 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200538}
539
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200540WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100541weston_surface_to_global_fixed(struct weston_surface *surface,
542 wl_fixed_t sx, wl_fixed_t sy,
543 wl_fixed_t *x, wl_fixed_t *y)
544{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200545 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100546
547 weston_surface_to_global_float(surface,
548 wl_fixed_to_double(sx),
549 wl_fixed_to_double(sy),
550 &xf, &yf);
551 *x = wl_fixed_from_double(xf);
552 *y = wl_fixed_from_double(yf);
553}
554
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400555WL_EXPORT void
556weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200557 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200558{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200559 if (surface->transform.enabled) {
560 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
561
562 weston_matrix_transform(&surface->transform.inverse, &v);
563
564 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200565 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200566 "weston_surface_from_global(), divisor = %g\n",
567 v.f[3]);
568 *sx = 0;
569 *sy = 0;
570 return;
571 }
572
Pekka Paalanencd403622012-01-25 13:37:39 +0200573 *sx = v.f[0] / v.f[3];
574 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200575 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200576 *sx = x - surface->geometry.x;
577 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200578 }
579}
580
581WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100582weston_surface_from_global_fixed(struct weston_surface *surface,
583 wl_fixed_t x, wl_fixed_t y,
584 wl_fixed_t *sx, wl_fixed_t *sy)
585{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200586 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100587
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400588 weston_surface_from_global_float(surface,
589 wl_fixed_to_double(x),
590 wl_fixed_to_double(y),
591 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100592 *sx = wl_fixed_from_double(sxf);
593 *sy = wl_fixed_from_double(syf);
594}
595
596WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200597weston_surface_from_global(struct weston_surface *surface,
598 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
599{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200600 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200601
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400602 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200603 *sx = floorf(sxf);
604 *sy = floorf(syf);
605}
606
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400607WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400608weston_surface_schedule_repaint(struct weston_surface *surface)
609{
610 struct weston_output *output;
611
612 wl_list_for_each(output, &surface->compositor->output_list, link)
613 if (surface->output_mask & (1 << output->id))
614 weston_output_schedule_repaint(output);
615}
616
617WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500618weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500619{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400620 pixman_region32_union_rect(&surface->damage, &surface->damage,
621 0, 0, surface->geometry.width,
622 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200623
Kristian Høgsberg98238702012-08-03 16:29:12 -0400624 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500625}
626
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400627WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500628weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200629 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400630{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200631 surface->geometry.x = x;
632 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200633 surface->geometry.width = width;
634 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200635 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400636}
637
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200638WL_EXPORT void
639weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200640 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200641{
642 surface->geometry.x = x;
643 surface->geometry.y = y;
644 surface->geometry.dirty = 1;
645}
646
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300647WL_EXPORT int
648weston_surface_is_mapped(struct weston_surface *surface)
649{
650 if (surface->output)
651 return 1;
652 else
653 return 0;
654}
655
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400656WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500657weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500658{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400659 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500660
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400661 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500662
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400663 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500664}
665
Tiago Vignatti9d393522012-02-10 16:26:19 +0200666static struct weston_surface *
667weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100668 wl_fixed_t x, wl_fixed_t y,
669 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200670{
671 struct weston_surface *surface;
672
673 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100674 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500675 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100676 wl_fixed_to_int(*sx),
677 wl_fixed_to_int(*sy),
678 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200679 return surface;
680 }
681
682 return NULL;
683}
684
685static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400686weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500687{
Scott Moreau447013d2012-02-18 05:05:29 -0700688 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500689 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400690 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500691
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400692 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100693 return;
694
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400695 surface = weston_compositor_pick_surface(seat->compositor,
696 pointer->x,
697 pointer->y,
698 &pointer->current_x,
699 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500700
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400701 if (&surface->surface != pointer->current) {
702 interface = pointer->grab->interface;
703 pointer->current = &surface->surface;
704 interface->focus(pointer->grab, &surface->surface,
705 pointer->current_x,
706 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500707 }
708
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400709 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500710 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100711 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400712 pointer->x,
713 pointer->y,
714 &pointer->grab->x,
715 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500716}
717
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400718static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500719weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400720{
Daniel Stone37816df2012-05-16 18:45:18 +0100721 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400722
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500723 if (!compositor->focus)
724 return;
725
Daniel Stone37816df2012-05-16 18:45:18 +0100726 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400727 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400728}
729
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400730WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500731weston_surface_unmap(struct weston_surface *surface)
732{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100733 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500734
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500735 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500736 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500737 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500738
Daniel Stone4dab5db2012-05-30 16:31:53 +0100739 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100740 if (seat->seat.keyboard &&
741 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100742 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100743 if (seat->seat.pointer &&
744 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100745 wl_pointer_set_focus(seat->seat.pointer,
746 NULL,
747 wl_fixed_from_int(0),
748 wl_fixed_from_int(0));
749 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500750
Kristian Høgsberg98238702012-08-03 16:29:12 -0400751 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500752}
753
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400754struct weston_frame_callback {
755 struct wl_resource resource;
756 struct wl_list link;
757};
758
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500759static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400760destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500761{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500762 struct weston_surface *surface =
763 container_of(resource,
764 struct weston_surface, surface.resource);
765 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400766 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400767
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300768 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500769 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400770
Pekka Paalanenbc106382012-10-10 12:49:31 +0300771 wl_list_for_each_safe(cb, next,
772 &surface->pending.frame_callback_list, link)
773 wl_resource_destroy(&cb->resource);
774
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300775 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300776 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +0300777 pixman_region32_fini(&surface->pending.damage);
778
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300779 if (surface->pending.buffer)
780 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
781
Benjamin Franzke06286262011-05-06 19:12:33 +0200782 if (surface->buffer)
783 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400784
Kristian Høgsberg42263852012-09-06 21:59:29 -0400785 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200786
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200787 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200788 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500789 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500790 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300791 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200792
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400793 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
794 wl_resource_destroy(&cb->resource);
795
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400796 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500797}
798
Alex Wu8811bf92012-02-28 18:07:54 +0800799WL_EXPORT void
800weston_surface_destroy(struct weston_surface *surface)
801{
802 /* Not a valid way to destroy a client surface */
803 assert(surface->surface.resource.client == NULL);
804
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400805 wl_signal_emit(&surface->surface.resource.destroy_signal,
806 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800807 destroy_surface(&surface->surface.resource);
808}
809
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100810static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300811weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100812{
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300813 if (surface->buffer) {
814 weston_buffer_post_release(surface->buffer);
815 wl_list_remove(&surface->buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300816 }
817
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400818 if (buffer) {
819 buffer->busy_count++;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300820 wl_signal_add(&buffer->resource.destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300821 &surface->buffer_destroy_listener);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400822 } else {
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300823 if (weston_surface_is_mapped(surface))
824 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300825 }
826
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300827 surface->compositor->renderer->attach(surface, buffer);
828 surface->buffer = buffer;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100829}
830
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500831WL_EXPORT void
832weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400833{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500834 wl_list_remove(&surface->layer_link);
835 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500836 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500837 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400838}
839
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400840WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500841weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400842{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500843 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400844
845 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500846 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400847}
848
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500849WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500850weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +0200851{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400852 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200853 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200854
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400855 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -0500856 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200857}
858
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400859WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500860weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400861{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500862 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400863
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400864 pixman_region32_union(&compositor->primary_plane.damage,
865 &compositor->primary_plane.damage,
866 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400867 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400868}
869
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400870static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500871fade_frame(struct weston_animation *animation,
872 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400873{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500874 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400875 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500876 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500877 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400878
Scott Moreau94b0b0c2012-06-14 01:01:56 -0600879 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -0600880 compositor->fade.spring.timestamp = msecs;
881
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500882 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500883 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500884 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
885 compositor->fade.spring.current);
886 weston_surface_damage(surface);
887
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500888 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400889 compositor->fade.spring.current =
890 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400891 wl_list_remove(&animation->link);
892 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200893
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500894 if (compositor->fade.spring.current < 0.001) {
895 destroy_surface(&surface->surface.resource);
896 compositor->fade.surface = NULL;
897 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500898 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400899 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200900 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400901 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400902}
903
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400904static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400905surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400906 pixman_region32_t *opaque)
907{
908 if (surface->buffer && wl_buffer_is_shm(surface->buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400909 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400910
911 if (surface->transform.enabled) {
912 pixman_box32_t *extents;
913
914 extents = pixman_region32_extents(&surface->damage);
915 surface_compute_bbox(surface, extents->x1, extents->y1,
916 extents->x2 - extents->x1,
917 extents->y2 - extents->y1,
918 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400919 pixman_region32_translate(&surface->damage,
920 -surface->plane->x,
921 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400922 } else {
923 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400924 surface->geometry.x - surface->plane->x,
925 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400926 }
927
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400928 pixman_region32_union(&surface->plane->damage,
929 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400930 empty_region(&surface->damage);
931 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400932 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400933}
934
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400935static void
Rob Bradford0fb79752012-08-02 15:36:57 +0100936weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400937{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500938 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500939 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500940 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -0500941 struct weston_animation *animation, *next;
942 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +0200943 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300944 pixman_region32_t opaque, output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500945
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +0200946 weston_compositor_update_drag_surfaces(ec);
947
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500948 /* Rebuild the surface list and update surface transforms up front. */
949 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +0200950 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500951 wl_list_for_each(layer, &ec->layer_list, link) {
952 wl_list_for_each(es, &layer->surface_list, layer_link) {
953 weston_surface_update_transform(es);
954 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +0200955 if (es->output == output) {
956 wl_list_insert_list(&frame_callback_list,
957 &es->frame_callback_list);
958 wl_list_init(&es->frame_callback_list);
959 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500960 }
961 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +0200962
Kristian Høgsberg79af73e2012-08-03 15:45:23 -0400963 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800964 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -0400965 else
966 wl_list_for_each(es, &ec->surface_list, link)
967 weston_surface_move_to_plane(es, &ec->primary_plane);
968
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500969 pixman_region32_init(&opaque);
970
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400971 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400972 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500973
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400974 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -0400975
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400976 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400977 pixman_region32_intersect(&output_damage,
978 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300979 pixman_region32_subtract(&ec->primary_plane.damage,
980 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400981
Scott Moreauccbf29d2012-02-22 14:21:41 -0700982 if (output->dirty)
983 weston_output_update_matrix(output);
984
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400985 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400986
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500987 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500988
Kristian Høgsbergef044142011-06-21 15:02:12 -0400989 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100990
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -0400991 weston_compositor_repick(ec);
992 wl_event_loop_dispatch(ec->input_loop, 0);
993
Jonas Ådahldb773762012-06-13 00:01:21 +0200994 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500995 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400996 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500997 }
998
Scott Moreaud64cf212012-06-08 19:40:54 -0600999 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001000 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001001 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001002 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001003}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001004
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001005static int
1006weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001007{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001008 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001009
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001010 wl_event_loop_dispatch(compositor->input_loop, 0);
1011
1012 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001013}
1014
1015WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001016weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001017{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001018 struct weston_compositor *compositor = output->compositor;
1019 struct wl_event_loop *loop =
1020 wl_display_get_event_loop(compositor->wl_display);
1021 int fd;
1022
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001023 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001024 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001025 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001026 return;
1027 }
1028
1029 output->repaint_scheduled = 0;
1030 if (compositor->input_loop_source)
1031 return;
1032
1033 fd = wl_event_loop_get_fd(compositor->input_loop);
1034 compositor->input_loop_source =
1035 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1036 weston_compositor_read_input, compositor);
1037}
1038
1039static void
1040idle_repaint(void *data)
1041{
1042 struct weston_output *output = data;
1043
1044 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001045}
1046
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001047WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001048weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1049{
1050 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001051 if (below != NULL)
1052 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001053}
1054
1055WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001056weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001057{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001058 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001059 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001060
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001061 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001062 return;
1063
Kristian Høgsbergef044142011-06-21 15:02:12 -04001064 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001065 output->repaint_needed = 1;
1066 if (output->repaint_scheduled)
1067 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001068
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001069 wl_event_loop_add_idle(loop, idle_repaint, output);
1070 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001071
1072 if (compositor->input_loop_source) {
1073 wl_event_source_remove(compositor->input_loop_source);
1074 compositor->input_loop_source = NULL;
1075 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001076}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001077
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001078WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001079weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1080{
1081 struct weston_output *output;
1082
1083 wl_list_for_each(output, &compositor->output_list, link)
1084 weston_output_schedule_repaint(output);
1085}
1086
1087WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001088weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001089{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001090 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001091 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001092
Scott Moreau9d1b1122012-06-08 19:40:53 -06001093 output = container_of(compositor->output_list.next,
1094 struct weston_output, link);
1095
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001096 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001097 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001098 return;
1099
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001100 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001101 surface = weston_surface_create(compositor);
John Kåre Alsakere2e3d072012-10-12 12:25:09 +02001102 if (!surface)
1103 return;
1104
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001105 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001106 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001107 wl_list_insert(&compositor->fade_layer.surface_list,
1108 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03001109 weston_surface_update_transform(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001110 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001111 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001112 }
1113
1114 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001115 if (wl_list_empty(&compositor->fade.animation.link)) {
1116 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001117 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001118 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001119 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001120}
1121
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001122static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001123surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001124{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001125 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001126}
1127
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001128static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001129surface_attach(struct wl_client *client,
1130 struct wl_resource *resource,
1131 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1132{
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001133 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001134 struct wl_buffer *buffer = NULL;
1135
1136 if (buffer_resource)
1137 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001138
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001139 if (surface->pending.buffer)
1140 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001141
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001142 surface->pending.sx = sx;
1143 surface->pending.sy = sy;
1144 surface->pending.buffer = buffer;
1145 if (buffer) {
1146 wl_signal_add(&buffer->resource.destroy_signal,
1147 &surface->pending.buffer_destroy_listener);
1148 surface->pending.remove_contents = 0;
1149 } else {
1150 surface->pending.remove_contents = 1;
1151 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001152}
1153
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001154static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001155surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001156 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001157 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001158{
Pekka Paalanen8e159182012-10-10 12:49:25 +03001159 struct weston_surface *surface = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001160
Pekka Paalanen8e159182012-10-10 12:49:25 +03001161 pixman_region32_union_rect(&surface->pending.damage,
1162 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001163 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001164}
1165
Kristian Høgsberg33418202011-08-16 23:01:28 -04001166static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001167destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001168{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001169 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001170
1171 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001172 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001173}
1174
1175static void
1176surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001177 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001178{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001179 struct weston_frame_callback *cb;
Pekka Paalanenbc106382012-10-10 12:49:31 +03001180 struct weston_surface *surface = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001181
1182 cb = malloc(sizeof *cb);
1183 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001184 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001185 return;
1186 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001187
Kristian Høgsberg33418202011-08-16 23:01:28 -04001188 cb->resource.object.interface = &wl_callback_interface;
1189 cb->resource.object.id = callback;
1190 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001191 cb->resource.client = client;
1192 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001193
1194 wl_client_add_resource(client, &cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001195 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001196}
1197
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001198static void
1199surface_set_opaque_region(struct wl_client *client,
1200 struct wl_resource *resource,
1201 struct wl_resource *region_resource)
1202{
1203 struct weston_surface *surface = resource->data;
1204 struct weston_region *region;
1205
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001206 if (region_resource) {
1207 region = region_resource->data;
Pekka Paalanen512dde82012-10-10 12:49:27 +03001208 pixman_region32_copy(&surface->pending.opaque,
1209 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001210 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001211 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001212 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001213}
1214
1215static void
1216surface_set_input_region(struct wl_client *client,
1217 struct wl_resource *resource,
1218 struct wl_resource *region_resource)
1219{
1220 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001221 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001222
1223 if (region_resource) {
1224 region = region_resource->data;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001225 pixman_region32_copy(&surface->pending.input,
1226 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001227 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001228 pixman_region32_fini(&surface->pending.input);
1229 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001230 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001231}
1232
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001233static void
1234surface_commit(struct wl_client *client, struct wl_resource *resource)
1235{
1236 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001237 pixman_region32_t opaque;
1238
1239 if (surface->pending.sx || surface->pending.sy ||
1240 (surface->pending.buffer &&
1241 (surface->pending.buffer->width != surface->geometry.width ||
1242 surface->pending.buffer->height != surface->geometry.height)))
1243 surface->geometry.dirty = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001244
1245 /* wl_surface.attach */
1246 if (surface->pending.buffer || surface->pending.remove_contents)
1247 weston_surface_attach(surface, surface->pending.buffer);
1248
1249 if (surface->buffer && surface->configure)
1250 surface->configure(surface, surface->pending.sx,
1251 surface->pending.sy);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001252
1253 /* wl_surface.damage */
1254 pixman_region32_union(&surface->damage, &surface->damage,
1255 &surface->pending.damage);
1256 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001257
1258 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001259 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001260 surface->geometry.width,
1261 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001262 pixman_region32_intersect(&opaque,
1263 &opaque, &surface->pending.opaque);
1264
1265 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1266 pixman_region32_copy(&surface->opaque, &opaque);
1267 surface->geometry.dirty = 1;
1268 }
1269
1270 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001271
1272 /* wl_surface.set_input_region */
1273 pixman_region32_fini(&surface->input);
1274 pixman_region32_init_rect(&surface->input, 0, 0,
1275 surface->geometry.width,
1276 surface->geometry.height);
1277 pixman_region32_intersect(&surface->input,
1278 &surface->input, &surface->pending.input);
1279
Pekka Paalanenbc106382012-10-10 12:49:31 +03001280 /* wl_surface.frame */
1281 wl_list_insert_list(&surface->frame_callback_list,
1282 &surface->pending.frame_callback_list);
1283 wl_list_init(&surface->pending.frame_callback_list);
1284
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001285 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001286}
1287
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001288static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001289 surface_destroy,
1290 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001291 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001292 surface_frame,
1293 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001294 surface_set_input_region,
1295 surface_commit
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001296};
1297
1298static void
1299compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001300 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001301{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001302 struct weston_compositor *ec = resource->data;
1303 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001304
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001305 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001306 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001307 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001308 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001309 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001310
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001311 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001312
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001313 surface->surface.resource.object.id = id;
1314 surface->surface.resource.object.interface = &wl_surface_interface;
1315 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001316 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001317 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001318
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001319 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001320}
1321
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001322static void
1323destroy_region(struct wl_resource *resource)
1324{
1325 struct weston_region *region =
1326 container_of(resource, struct weston_region, resource);
1327
1328 pixman_region32_fini(&region->region);
1329 free(region);
1330}
1331
1332static void
1333region_destroy(struct wl_client *client, struct wl_resource *resource)
1334{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001335 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001336}
1337
1338static void
1339region_add(struct wl_client *client, struct wl_resource *resource,
1340 int32_t x, int32_t y, int32_t width, int32_t height)
1341{
1342 struct weston_region *region = resource->data;
1343
1344 pixman_region32_union_rect(&region->region, &region->region,
1345 x, y, width, height);
1346}
1347
1348static void
1349region_subtract(struct wl_client *client, struct wl_resource *resource,
1350 int32_t x, int32_t y, int32_t width, int32_t height)
1351{
1352 struct weston_region *region = resource->data;
1353 pixman_region32_t rect;
1354
1355 pixman_region32_init_rect(&rect, x, y, width, height);
1356 pixman_region32_subtract(&region->region, &region->region, &rect);
1357 pixman_region32_fini(&rect);
1358}
1359
1360static const struct wl_region_interface region_interface = {
1361 region_destroy,
1362 region_add,
1363 region_subtract
1364};
1365
1366static void
1367compositor_create_region(struct wl_client *client,
1368 struct wl_resource *resource, uint32_t id)
1369{
1370 struct weston_region *region;
1371
1372 region = malloc(sizeof *region);
1373 if (region == NULL) {
1374 wl_resource_post_no_memory(resource);
1375 return;
1376 }
1377
1378 region->resource.destroy = destroy_region;
1379
1380 region->resource.object.id = id;
1381 region->resource.object.interface = &wl_region_interface;
1382 region->resource.object.implementation =
1383 (void (**)(void)) &region_interface;
1384 region->resource.data = region;
1385
1386 pixman_region32_init(&region->region);
1387
1388 wl_client_add_resource(client, &region->resource);
1389}
1390
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001391static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001392 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001393 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001394};
1395
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001396WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001397weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001398{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001399 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1400 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001401
1402 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001403 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001404}
1405
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001406static void
1407weston_compositor_dpms_on(struct weston_compositor *compositor)
1408{
1409 struct weston_output *output;
1410
1411 wl_list_for_each(output, &compositor->output_list, link)
1412 if (output->set_dpms)
1413 output->set_dpms(output, WESTON_DPMS_ON);
1414}
1415
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001416WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001417weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001418{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001419 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1420 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001421 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001422 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001423 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001424 }
1425}
1426
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001427static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001428weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001429{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001430 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001431 compositor->idle_inhibit++;
1432}
1433
1434static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001435weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001436{
1437 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001438 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001439}
1440
1441static int
1442idle_handler(void *data)
1443{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001444 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001445
1446 if (compositor->idle_inhibit)
1447 return 1;
1448
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001449 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001450
1451 return 1;
1452}
1453
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001454WL_EXPORT void
1455weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1456{
1457 pixman_region32_init(&plane->damage);
1458 plane->x = x;
1459 plane->y = y;
1460}
1461
1462WL_EXPORT void
1463weston_plane_release(struct weston_plane *plane)
1464{
1465 pixman_region32_fini(&plane->damage);
1466}
1467
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001468static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001469weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001470
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001471static void
Daniel Stone37816df2012-05-16 18:45:18 +01001472clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001473{
Daniel Stone37816df2012-05-16 18:45:18 +01001474 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001475 struct weston_output *output, *prev = NULL;
1476 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001477
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001478 x = wl_fixed_to_int(*fx);
1479 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001480 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1481 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001482
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001483 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001484 if (pixman_region32_contains_point(&output->region,
1485 x, y, NULL))
1486 valid = 1;
1487 if (pixman_region32_contains_point(&output->region,
1488 old_x, old_y, NULL))
1489 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001490 }
Daniel Stone37816df2012-05-16 18:45:18 +01001491
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001492 if (!valid) {
1493 if (x < prev->x)
1494 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001495 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001496 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001497 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001498 if (y < prev->y)
1499 *fy = wl_fixed_from_int(prev->y);
1500 else if (y >= prev->y + prev->current->height)
1501 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001502 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001503 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001504}
1505
1506WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001507notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001508{
1509 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001510 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001511 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001512 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001513 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001514
1515 weston_compositor_activity(ec);
1516
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001517 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001518
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001519 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001520
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001521 pointer->x = x;
1522 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001523
1524 ix = wl_fixed_to_int(x);
1525 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001526
Scott Moreauccbf29d2012-02-22 14:21:41 -07001527 wl_list_for_each(output, &ec->output_list, link)
1528 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001529 pixman_region32_contains_point(&output->region,
1530 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06001531 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001532
Daniel Stone37816df2012-05-16 18:45:18 +01001533 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001534 interface = pointer->grab->interface;
1535 interface->motion(pointer->grab, time,
1536 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001537
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001538 if (seat->sprite) {
1539 weston_surface_set_position(seat->sprite,
1540 ix - seat->hotspot_x,
1541 iy - seat->hotspot_y);
1542 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001543 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001544}
1545
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001546WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001547weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001548 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001549{
Daniel Stone37816df2012-05-16 18:45:18 +01001550 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001551
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001552 if (seat->seat.keyboard) {
1553 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1554 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04001555 }
1556
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001557 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001558}
1559
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001560WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001561notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001562 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001563{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001564 struct weston_compositor *compositor = seat->compositor;
1565 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001566 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001567 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001568 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001569
Daniel Stone4dbadb12012-05-30 16:31:51 +01001570 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001571 if (compositor->ping_handler && focus)
1572 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001573 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001574 if (pointer->button_count == 0) {
1575 pointer->grab_button = button;
1576 pointer->grab_time = time;
1577 pointer->grab_x = pointer->x;
1578 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001579 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001580 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001581 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001582 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001583 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001584 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001585
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001586 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001587 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001588
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001589 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001590
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001591 if (pointer->button_count == 1)
1592 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001593 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001594}
1595
Scott Moreau210d0792012-03-22 10:47:01 -06001596WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001597notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01001598 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001599{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001600 struct weston_compositor *compositor = seat->compositor;
1601 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001602 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001603 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001604 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1605
1606 if (compositor->ping_handler && focus)
1607 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001608
1609 weston_compositor_activity(compositor);
1610
Scott Moreau6a3633d2012-03-20 08:47:59 -06001611 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001612 weston_compositor_run_axis_binding(compositor, seat,
1613 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06001614 else
1615 return;
1616
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001617 if (pointer->focus_resource)
1618 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01001619 value);
Scott Moreau210d0792012-03-22 10:47:01 -06001620}
1621
Daniel Stone05d58682012-06-22 13:21:38 +01001622WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001623notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001624{
Daniel Stone05d58682012-06-22 13:21:38 +01001625 struct wl_keyboard *keyboard = &seat->keyboard;
1626 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01001627 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001628 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01001629 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01001630 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01001631
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001632 /* Serialize and update our internal state, checking to see if it's
1633 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01001634 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1635 XKB_STATE_DEPRESSED);
1636 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1637 XKB_STATE_LATCHED);
1638 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1639 XKB_STATE_LOCKED);
1640 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01001641 XKB_STATE_EFFECTIVE);
1642
Daniel Stone71c38772012-06-22 13:21:30 +01001643 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
1644 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
1645 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
1646 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01001647 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01001648
Daniel Stone71c38772012-06-22 13:21:30 +01001649 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
1650 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
1651 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
1652 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001653
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001654 /* And update the modifier_state for bindings. */
1655 mods_lookup = mods_depressed | mods_latched;
1656 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01001657 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001658 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01001659 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001660 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01001661 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001662 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04001663 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
1664 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01001665
Daniel Stone8c8164f2012-05-30 16:31:45 +01001666 /* Finally, notify the compositor that LEDs have changed. */
1667 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001668 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001669 leds |= LED_NUM_LOCK;
1670 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001671 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001672 leds |= LED_CAPS_LOCK;
1673 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001674 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001675 leds |= LED_SCROLL_LOCK;
1676 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01001677 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01001678 seat->xkb_state.leds = leds;
1679
Daniel Stone05d58682012-06-22 13:21:38 +01001680 if (changed) {
1681 grab->interface->modifiers(grab,
1682 serial,
1683 keyboard->modifiers.mods_depressed,
1684 keyboard->modifiers.mods_latched,
1685 keyboard->modifiers.mods_locked,
1686 keyboard->modifiers.group);
1687 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001688}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001689
Daniel Stone05d58682012-06-22 13:21:38 +01001690static void
1691update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001692 enum wl_keyboard_key_state state)
1693{
1694 enum xkb_key_direction direction;
1695
1696 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1697 direction = XKB_KEY_DOWN;
1698 else
1699 direction = XKB_KEY_UP;
1700
1701 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1702 * broken keycode system, which starts at 8. */
1703 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1704
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001705 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001706}
1707
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001708WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001709notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001710 enum wl_keyboard_key_state state,
1711 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001712{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001713 struct weston_compositor *compositor = seat->compositor;
1714 struct wl_keyboard *keyboard = seat->seat.keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01001715 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001716 (struct weston_surface *) keyboard->focus;
1717 struct wl_keyboard_grab *grab = keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001718 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001719 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001720
Daniel Stonec9785ea2012-05-30 16:31:52 +01001721 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001722 if (compositor->ping_handler && focus)
1723 compositor->ping_handler(focus, serial);
1724
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001725 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001726 keyboard->grab_key = key;
1727 keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07001728 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001729 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001730 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001731
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001732 end = keyboard->keys.data + keyboard->keys.size;
1733 for (k = keyboard->keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01001734 if (*k == key) {
1735 /* Ignore server-generated repeats. */
1736 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1737 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001738 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01001739 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001740 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001741 keyboard->keys.size = (void *) end - keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001742 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001743 k = wl_array_add(&keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001744 *k = key;
1745 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001746
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001747 if (grab == &keyboard->default_grab) {
1748 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001749 state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001750 grab = keyboard->grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06001751 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001752
Daniel Stone7ace3902012-05-30 16:31:40 +01001753 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01001754
1755 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001756 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01001757 wl_display_get_serial(compositor->wl_display),
1758 key,
1759 state);
1760 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001761}
1762
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001763WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001764notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01001765 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001766{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001767 struct weston_compositor *compositor = seat->compositor;
1768 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001769
1770 if (output) {
Kristian Høgsberg00fbbe62012-10-23 13:04:09 -04001771 clip_pointer_motion(seat, &x, &y);
Daniel Stone37816df2012-05-16 18:45:18 +01001772 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001773 x - pointer->x,
1774 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001775
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001776 pointer->x = x;
1777 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001778 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001779 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001780 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001781 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03001782 /* FIXME: We should call wl_pointer_set_focus(seat,
1783 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001784 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001785}
1786
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001787static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001788destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001789{
Daniel Stone37816df2012-05-16 18:45:18 +01001790 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001791
Daniel Stone37816df2012-05-16 18:45:18 +01001792 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001793 saved_kbd_focus_listener);
1794
Daniel Stone37816df2012-05-16 18:45:18 +01001795 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001796}
1797
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001798WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001799notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001800 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001801{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001802 struct weston_compositor *compositor = seat->compositor;
1803 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001804 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001805 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001806
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001807 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001808 wl_array_copy(&keyboard->keys, keys);
1809 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001810 weston_compositor_idle_inhibit(compositor);
1811 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001812 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001813 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001814 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001815
Daniel Stoneef172672012-06-22 13:21:32 +01001816 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001817 wl_array_for_each(k, &keyboard->keys) {
1818 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01001819 WL_KEYBOARD_KEY_STATE_PRESSED);
1820 }
1821
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001822 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001823
1824 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001825 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1826 wl_keyboard_set_focus(keyboard, surface);
1827 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001828 }
1829}
1830
1831WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001832notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01001833{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001834 struct weston_compositor *compositor = seat->compositor;
1835 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001836 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001837
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001838 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001839 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001840 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001841 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001842 WL_KEYBOARD_KEY_STATE_RELEASED);
1843 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001844
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001845 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001846
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001847 if (keyboard->focus) {
1848 seat->saved_kbd_focus = keyboard->focus;
1849 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01001850 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001851 wl_signal_add(&keyboard->focus->resource.destroy_signal,
1852 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001853 }
1854
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001855 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001856 /* FIXME: We really need keyboard grab cancel here to
1857 * let the grab shut down properly. As it is we leak
1858 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001859 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001860}
1861
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001862static void
Daniel Stone37816df2012-05-16 18:45:18 +01001863touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001864{
Daniel Stone37816df2012-05-16 18:45:18 +01001865 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001866 struct wl_resource *resource;
1867
Pekka Paalanen56464252012-07-31 13:21:08 +03001868 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001869 return;
1870
Pekka Paalanen56464252012-07-31 13:21:08 +03001871 if (seat->touch->focus_resource)
1872 wl_list_remove(&seat->touch->focus_listener.link);
1873 seat->touch->focus = NULL;
1874 seat->touch->focus_resource = NULL;
1875
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001876 if (surface) {
1877 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01001878 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04001879 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001880 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02001881 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001882 return;
1883 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001884
Daniel Stone37816df2012-05-16 18:45:18 +01001885 seat->touch->focus = surface;
1886 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03001887 wl_signal_add(&resource->destroy_signal,
1888 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001889 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001890}
1891
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001892/**
1893 * notify_touch - emulates button touches and notifies surfaces accordingly.
1894 *
1895 * It assumes always the correct cycle sequence until it gets here: touch_down
1896 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1897 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001898 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001899 */
1900WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001901notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001902 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001903{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001904 struct weston_compositor *ec = seat->compositor;
1905 struct wl_touch *touch = seat->seat.touch;
Matt Roper47c1b982012-10-10 16:56:53 -07001906 struct wl_touch_grab *grab = touch->grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001907 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001908 wl_fixed_t sx, sy;
Matt Roper47c1b982012-10-10 16:56:53 -07001909
1910 /* Update grab's global coordinates. */
1911 touch->grab_x = x;
1912 touch->grab_y = y;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001913
1914 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01001915 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001916 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001917
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001918 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001919
1920 /* the first finger down picks the surface, and all further go
1921 * to that surface for the remainder of the touch session i.e.
1922 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001923 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001924 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001925 touch_set_focus(seat, &es->surface);
1926 } else if (touch->focus) {
1927 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001928 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001929 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001930
Matt Roper47c1b982012-10-10 16:56:53 -07001931 grab->interface->down(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001932 break;
Daniel Stone37816df2012-05-16 18:45:18 +01001933 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001934 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001935 if (!es)
1936 break;
1937
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001938 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Matt Roper47c1b982012-10-10 16:56:53 -07001939 grab->interface->motion(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001940 break;
Daniel Stone37816df2012-05-16 18:45:18 +01001941 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001942 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001943 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001944
Matt Roper47c1b982012-10-10 16:56:53 -07001945 grab->interface->up(grab, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001946 if (seat->num_tp == 0)
1947 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001948 break;
1949 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001950}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001951
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001952static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001953pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1954{
1955 struct weston_seat *seat = container_of(listener, struct weston_seat,
1956 sprite_destroy_listener);
1957
1958 seat->sprite = NULL;
1959}
1960
1961static void
1962pointer_cursor_surface_configure(struct weston_surface *es,
1963 int32_t dx, int32_t dy)
1964{
1965 struct weston_seat *seat = es->private;
1966 int x, y;
1967
1968 assert(es == seat->sprite);
1969
1970 seat->hotspot_x -= dx;
1971 seat->hotspot_y -= dy;
1972
1973 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
1974 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
1975
1976 weston_surface_configure(seat->sprite, x, y,
1977 es->buffer->width, es->buffer->height);
1978
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001979 empty_region(&es->pending.input);
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03001980
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001981 if (!weston_surface_is_mapped(es)) {
1982 wl_list_insert(&es->compositor->cursor_layer.surface_list,
1983 &es->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03001984 weston_surface_update_transform(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001985 }
1986}
1987
1988static void
1989pointer_unmap_sprite(struct weston_seat *seat)
1990{
1991 if (weston_surface_is_mapped(seat->sprite))
1992 weston_surface_unmap(seat->sprite);
1993
1994 wl_list_remove(&seat->sprite_destroy_listener.link);
1995 seat->sprite->configure = NULL;
1996 seat->sprite->private = NULL;
1997 seat->sprite = NULL;
1998}
1999
2000static void
2001pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2002 uint32_t serial, struct wl_resource *surface_resource,
2003 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002004{
Daniel Stone37816df2012-05-16 18:45:18 +01002005 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002006 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002007
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002008 if (surface_resource)
Pekka Paalanen6c71ee12012-10-10 12:49:30 +03002009 surface = surface_resource->data;
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002010
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04002011 if (seat->seat.pointer->focus == NULL)
2012 return;
2013 if (seat->seat.pointer->focus->resource.client != client)
2014 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002015 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002016 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002017
2018 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002019 if (surface->configure) {
2020 wl_resource_post_error(&surface->surface.resource,
2021 WL_DISPLAY_ERROR_INVALID_OBJECT,
2022 "surface->configure already "
2023 "set");
2024 return;
2025 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002026 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002027
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002028 if (seat->sprite)
2029 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002030
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002031 if (!surface)
2032 return;
2033
2034 wl_signal_add(&surface->surface.resource.destroy_signal,
2035 &seat->sprite_destroy_listener);
2036
2037 surface->configure = pointer_cursor_surface_configure;
2038 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002039 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002040 seat->hotspot_x = x;
2041 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002042
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04002043 if (surface->buffer)
2044 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002045}
2046
Daniel Stone37816df2012-05-16 18:45:18 +01002047static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002048 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002049};
2050
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002051static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002052handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002053{
Daniel Stone37816df2012-05-16 18:45:18 +01002054 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002055
Daniel Stone37816df2012-05-16 18:45:18 +01002056 seat = container_of(listener, struct weston_seat,
2057 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002058
Daniel Stone37816df2012-05-16 18:45:18 +01002059 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002060}
2061
Casey Dahlin9074db52012-04-19 22:50:09 -04002062static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002063{
2064 wl_list_remove(&resource->link);
2065 free(resource);
2066}
2067
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002068static void
Daniel Stone37816df2012-05-16 18:45:18 +01002069seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2070 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002071{
Daniel Stone37816df2012-05-16 18:45:18 +01002072 struct weston_seat *seat = resource->data;
2073 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002074
Daniel Stone37816df2012-05-16 18:45:18 +01002075 if (!seat->seat.pointer)
2076 return;
2077
2078 cr = wl_client_add_object(client, &wl_pointer_interface,
2079 &pointer_interface, id, seat);
2080 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002081 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002082
2083 if (seat->seat.pointer->focus &&
2084 seat->seat.pointer->focus->resource.client == client) {
2085 struct weston_surface *surface;
2086 wl_fixed_t sx, sy;
2087
2088 surface = (struct weston_surface *) seat->seat.pointer->focus;
2089 weston_surface_from_global_fixed(surface,
2090 seat->seat.pointer->x,
2091 seat->seat.pointer->y,
2092 &sx,
2093 &sy);
2094 wl_pointer_set_focus(seat->seat.pointer,
2095 seat->seat.pointer->focus,
2096 sx,
2097 sy);
2098 }
Daniel Stone37816df2012-05-16 18:45:18 +01002099}
2100
2101static void
2102seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2103 uint32_t id)
2104{
2105 struct weston_seat *seat = resource->data;
2106 struct wl_resource *cr;
2107
2108 if (!seat->seat.keyboard)
2109 return;
2110
2111 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2112 seat);
2113 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002114 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002115
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002116 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2117 seat->xkb_info.keymap_fd,
2118 seat->xkb_info.keymap_size);
2119
Daniel Stone17b13bd2012-05-30 16:31:39 +01002120 if (seat->seat.keyboard->focus &&
2121 seat->seat.keyboard->focus->resource.client == client) {
2122 wl_keyboard_set_focus(seat->seat.keyboard,
2123 seat->seat.keyboard->focus);
2124 }
Daniel Stone37816df2012-05-16 18:45:18 +01002125}
2126
2127static void
2128seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2129 uint32_t id)
2130{
2131 struct weston_seat *seat = resource->data;
2132 struct wl_resource *cr;
2133
2134 if (!seat->seat.touch)
2135 return;
2136
2137 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2138 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002139 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002140}
2141
2142static const struct wl_seat_interface seat_interface = {
2143 seat_get_pointer,
2144 seat_get_keyboard,
2145 seat_get_touch,
2146};
2147
2148static void
2149bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2150{
2151 struct wl_seat *seat = data;
2152 struct wl_resource *resource;
2153 enum wl_seat_capability caps = 0;
2154
2155 resource = wl_client_add_object(client, &wl_seat_interface,
2156 &seat_interface, id, data);
2157 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002158 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002159
2160 if (seat->pointer)
2161 caps |= WL_SEAT_CAPABILITY_POINTER;
2162 if (seat->keyboard)
2163 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2164 if (seat->touch)
2165 caps |= WL_SEAT_CAPABILITY_TOUCH;
2166
2167 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002168}
2169
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002170static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002171device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002172{
Daniel Stone37816df2012-05-16 18:45:18 +01002173 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002174
Daniel Stone37816df2012-05-16 18:45:18 +01002175 seat = container_of(listener, struct weston_seat,
2176 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002177
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002178 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002179}
2180
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002181static void weston_compositor_xkb_init(struct weston_compositor *ec,
2182 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002183{
Daniel Stonee379da92012-05-30 16:32:04 +01002184 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002185 ec->xkb_context = xkb_context_new(0);
2186 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002187 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002188 exit(1);
2189 }
Daniel Stone994679a2012-05-30 16:31:43 +01002190 }
2191
2192 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002193 ec->xkb_names = *names;
2194 if (!ec->xkb_names.rules)
2195 ec->xkb_names.rules = strdup("evdev");
2196 if (!ec->xkb_names.model)
2197 ec->xkb_names.model = strdup("pc105");
2198 if (!ec->xkb_names.layout)
2199 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002200}
2201
Daniel Stonee379da92012-05-30 16:32:04 +01002202static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2203{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002204 if (xkb_info->keymap)
2205 xkb_map_unref(xkb_info->keymap);
2206
2207 if (xkb_info->keymap_area)
2208 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2209 if (xkb_info->keymap_fd >= 0)
2210 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002211}
2212
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002213static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2214{
Daniel Stonee379da92012-05-30 16:32:04 +01002215 free((char *) ec->xkb_names.rules);
2216 free((char *) ec->xkb_names.model);
2217 free((char *) ec->xkb_names.layout);
2218 free((char *) ec->xkb_names.variant);
2219 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002220
Daniel Stonee379da92012-05-30 16:32:04 +01002221 xkb_info_destroy(&ec->xkb_info);
2222 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002223}
2224
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002225static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002226weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002227{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002228 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002229
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002230 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2231 XKB_MOD_NAME_SHIFT);
2232 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2233 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002234 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2235 XKB_MOD_NAME_CTRL);
2236 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2237 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002238 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2239 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002240 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2241 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002242 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002243
2244 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2245 XKB_LED_NAME_NUM);
2246 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2247 XKB_LED_NAME_CAPS);
2248 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2249 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002250
2251 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2252 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002253 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002254 exit(EXIT_FAILURE);
2255 }
2256 xkb_info->keymap_size = strlen(keymap_str) + 1;
2257
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002258 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002259 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002260 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002261 (unsigned long) xkb_info->keymap_size);
2262 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002263 }
2264
2265 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2266 PROT_READ | PROT_WRITE,
2267 MAP_SHARED, xkb_info->keymap_fd, 0);
2268 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002269 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002270 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002271 goto err_dev_zero;
2272 }
2273 strcpy(xkb_info->keymap_area, keymap_str);
2274 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002275
2276 return;
2277
2278err_dev_zero:
2279 close(xkb_info->keymap_fd);
2280 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002281err_keymap_str:
2282 free(keymap_str);
2283 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002284}
2285
2286static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002287weston_compositor_build_global_keymap(struct weston_compositor *ec)
2288{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002289 if (ec->xkb_info.keymap != NULL)
2290 return;
2291
Daniel Stonee379da92012-05-30 16:32:04 +01002292 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002293 &ec->xkb_names,
2294 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002295 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002296 weston_log("failed to compile global XKB keymap\n");
2297 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002298 "options %s",
2299 ec->xkb_names.rules, ec->xkb_names.model,
2300 ec->xkb_names.layout, ec->xkb_names.variant,
2301 ec->xkb_names.options);
2302 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002303 }
2304
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002305 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002306}
2307
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002308WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002309weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002310{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002311 if (seat->has_keyboard)
2312 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002313
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002314 if (keymap != NULL) {
2315 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002316 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002317 }
2318 else {
2319 weston_compositor_build_global_keymap(seat->compositor);
2320 seat->xkb_info = seat->compositor->xkb_info;
2321 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2322 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002323
2324 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2325 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002326 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01002327 exit(1);
2328 }
2329
Daniel Stone71c38772012-06-22 13:21:30 +01002330 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002331
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002332 wl_keyboard_init(&seat->keyboard);
2333 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2334
2335 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01002336}
2337
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002338WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002339weston_seat_init_pointer(struct weston_seat *seat)
2340{
2341 if (seat->has_pointer)
2342 return;
2343
2344 wl_pointer_init(&seat->pointer);
2345 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2346
2347 seat->has_pointer = 1;
2348}
2349
2350WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002351weston_seat_init_touch(struct weston_seat *seat)
2352{
2353 if (seat->has_touch)
2354 return;
2355
2356 wl_touch_init(&seat->touch);
2357 wl_seat_set_touch(&seat->seat, &seat->touch);
2358
2359 seat->has_touch = 1;
2360}
2361
2362WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002363weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002364{
Daniel Stone37816df2012-05-16 18:45:18 +01002365 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002366 seat->has_pointer = 0;
2367 seat->has_keyboard = 0;
2368 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002369
Daniel Stone37816df2012-05-16 18:45:18 +01002370 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2371 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002372
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002373 seat->sprite = NULL;
2374 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002375
Daniel Stone37816df2012-05-16 18:45:18 +01002376 seat->compositor = ec;
2377 seat->hotspot_x = 16;
2378 seat->hotspot_y = 16;
2379 seat->modifier_state = 0;
2380 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002381
Daniel Stone37816df2012-05-16 18:45:18 +01002382 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002383 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002384
Daniel Stone37816df2012-05-16 18:45:18 +01002385 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002386
Daniel Stone37816df2012-05-16 18:45:18 +01002387 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2388 wl_signal_add(&seat->seat.drag_icon_signal,
2389 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002390
2391 clipboard_create(seat);
Jan Arne Petersene829adc2012-08-10 16:47:22 +02002392 input_method_create(ec, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002393}
2394
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002395WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002396weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002397{
Daniel Stone37816df2012-05-16 18:45:18 +01002398 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002399 /* The global object is destroyed at wl_display_destroy() time. */
2400
Daniel Stone37816df2012-05-16 18:45:18 +01002401 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002402 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002403
Daniel Stone74419a22012-05-30 16:32:02 +01002404 if (seat->xkb_state.state != NULL)
2405 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01002406 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002407
Daniel Stone37816df2012-05-16 18:45:18 +01002408 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002409}
2410
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002411static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002412drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2413{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002414 empty_region(&es->pending.input);
2415
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002416 weston_surface_configure(es,
2417 es->geometry.x + sx, es->geometry.y + sy,
2418 es->buffer->width, es->buffer->height);
2419}
2420
2421static int
Daniel Stone37816df2012-05-16 18:45:18 +01002422device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002423 struct weston_surface *surface)
2424{
Daniel Stone37816df2012-05-16 18:45:18 +01002425 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002426
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002427 if (surface->configure) {
2428 wl_resource_post_error(&surface->surface.resource,
2429 WL_DISPLAY_ERROR_INVALID_OBJECT,
2430 "surface->configure already set");
2431 return 0;
2432 }
2433
Daniel Stone37816df2012-05-16 18:45:18 +01002434 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002435
Daniel Stone37816df2012-05-16 18:45:18 +01002436 weston_surface_set_position(ws->drag_surface,
2437 wl_fixed_to_double(seat->pointer->x),
2438 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002439
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002440 surface->configure = drag_surface_configure;
2441
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002442 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002443 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002444
2445 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002446}
2447
2448static void
Daniel Stone37816df2012-05-16 18:45:18 +01002449device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002450{
Ander Conselvan de Oliveira5fd55802012-10-11 14:06:19 +03002451 if (weston_surface_is_mapped(seat->drag_surface))
2452 weston_surface_unmap(seat->drag_surface);
2453
Daniel Stone37816df2012-05-16 18:45:18 +01002454 seat->drag_surface->configure = NULL;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002455 empty_region(&seat->drag_surface->pending.input);
Daniel Stone37816df2012-05-16 18:45:18 +01002456 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2457 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002458}
2459
2460static void
Daniel Stone37816df2012-05-16 18:45:18 +01002461device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002462{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002463 struct wl_list *list;
2464
Daniel Stone37816df2012-05-16 18:45:18 +01002465 if (weston_surface_is_mapped(seat->drag_surface) ||
2466 !seat->drag_surface->buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002467 return;
2468
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002469 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2470 list = &seat->sprite->layer_link;
2471 else
2472 list = &seat->compositor->cursor_layer.surface_list;
2473
2474 wl_list_insert(list, &seat->drag_surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002475 weston_surface_update_transform(seat->drag_surface);
Daniel Stone37816df2012-05-16 18:45:18 +01002476 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002477}
2478
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002479static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002480weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01002481 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002482{
2483 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002484
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002485 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002486 return;
2487
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002488 if (seat->drag_surface && seat->seat.drag_surface &&
2489 (&seat->drag_surface->surface.resource !=
2490 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002491 /* between calls to this funcion we got a new drag_surface */
2492 surface_changed = 1;
2493
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002494 if (!seat->seat.drag_surface || surface_changed) {
2495 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002496 if (!surface_changed)
2497 return;
2498 }
2499
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002500 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002501 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002502 seat->seat.drag_surface;
2503 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002504 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002505 }
2506
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002507 /* the client may not have attached a buffer to the drag surface
2508 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002509 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002510
2511 if (!dx && !dy)
2512 return;
2513
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002514 weston_surface_set_position(seat->drag_surface,
2515 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
2516 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002517}
2518
2519WL_EXPORT void
2520weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2521{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002522 struct weston_seat *seat;
2523
2524 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002525 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002526}
2527
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002528static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002529bind_output(struct wl_client *client,
2530 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002531{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002532 struct weston_output *output = data;
2533 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002534 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002535
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002536 resource = wl_client_add_object(client,
2537 &wl_output_interface, NULL, id, data);
2538
Casey Dahlin9074db52012-04-19 22:50:09 -04002539 wl_list_insert(&output->resource_list, &resource->link);
2540 resource->destroy = unbind_resource;
2541
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002542 wl_output_send_geometry(resource,
2543 output->x,
2544 output->y,
2545 output->mm_width,
2546 output->mm_height,
2547 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002548 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002549 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002550
2551 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002552 wl_output_send_mode(resource,
2553 mode->flags,
2554 mode->width,
2555 mode->height,
2556 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002557 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002558}
2559
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002560WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002561weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002562{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002563 struct weston_compositor *c = output->compositor;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002564 int i;
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002565
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002566 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002567
2568 for (i = 0; i < 2; i++)
2569 pixman_region32_fini(&output->buffer_damage[i]);
2570
Casey Dahlin9074db52012-04-19 22:50:09 -04002571 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002572
2573 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002574}
2575
Scott Moreau1bad5db2012-08-18 01:04:05 -06002576static void
2577weston_output_compute_transform(struct weston_output *output)
2578{
2579 struct weston_matrix transform;
2580 int flip;
2581
2582 weston_matrix_init(&transform);
2583
2584 switch(output->transform) {
2585 case WL_OUTPUT_TRANSFORM_FLIPPED:
2586 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2587 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2588 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2589 flip = -1;
2590 break;
2591 default:
2592 flip = 1;
2593 break;
2594 }
2595
2596 switch(output->transform) {
2597 case WL_OUTPUT_TRANSFORM_NORMAL:
2598 case WL_OUTPUT_TRANSFORM_FLIPPED:
2599 transform.d[0] = flip;
2600 transform.d[1] = 0;
2601 transform.d[4] = 0;
2602 transform.d[5] = 1;
2603 break;
2604 case WL_OUTPUT_TRANSFORM_90:
2605 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2606 transform.d[0] = 0;
2607 transform.d[1] = -flip;
2608 transform.d[4] = 1;
2609 transform.d[5] = 0;
2610 break;
2611 case WL_OUTPUT_TRANSFORM_180:
2612 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2613 transform.d[0] = -flip;
2614 transform.d[1] = 0;
2615 transform.d[4] = 0;
2616 transform.d[5] = -1;
2617 break;
2618 case WL_OUTPUT_TRANSFORM_270:
2619 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2620 transform.d[0] = 0;
2621 transform.d[1] = flip;
2622 transform.d[4] = -1;
2623 transform.d[5] = 0;
2624 break;
2625 default:
2626 break;
2627 }
2628
2629 weston_matrix_multiply(&output->matrix, &transform);
2630}
2631
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002632WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002633weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002634{
Scott Moreau850ca422012-05-21 15:21:25 -06002635 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002636 struct weston_matrix camera;
2637 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002638
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002639 weston_matrix_init(&output->matrix);
2640 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002641 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2642 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002643
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002644 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002645 2.0 / (output->width + output->border.left + output->border.right),
2646 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2647
2648 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002649
Scott Moreauccbf29d2012-02-22 14:21:41 -07002650 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002651 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002652 weston_matrix_init(&camera);
2653 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002654 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002655 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002656 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002657 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002658 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002659 weston_matrix_multiply(&output->matrix, &modelview);
2660 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002661
Scott Moreauccbf29d2012-02-22 14:21:41 -07002662 output->dirty = 0;
2663}
2664
Scott Moreau1bad5db2012-08-18 01:04:05 -06002665static void
2666weston_output_transform_init(struct weston_output *output, uint32_t transform)
2667{
2668 output->transform = transform;
2669
2670 switch (transform) {
2671 case WL_OUTPUT_TRANSFORM_90:
2672 case WL_OUTPUT_TRANSFORM_270:
2673 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2674 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2675 /* Swap width and height */
2676 output->width = output->current->height;
2677 output->height = output->current->width;
2678 break;
2679 case WL_OUTPUT_TRANSFORM_NORMAL:
2680 case WL_OUTPUT_TRANSFORM_180:
2681 case WL_OUTPUT_TRANSFORM_FLIPPED:
2682 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2683 output->width = output->current->width;
2684 output->height = output->current->height;
2685 break;
2686 default:
2687 break;
2688 }
2689}
2690
Scott Moreauccbf29d2012-02-22 14:21:41 -07002691WL_EXPORT void
2692weston_output_move(struct weston_output *output, int x, int y)
2693{
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002694 int i;
2695
Scott Moreauccbf29d2012-02-22 14:21:41 -07002696 output->x = x;
2697 output->y = y;
2698
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002699 output->current_buffer = 0;
2700 for (i = 0; i < 2; i++)
2701 pixman_region32_init(&output->buffer_damage[i]);
2702
Scott Moreauccbf29d2012-02-22 14:21:41 -07002703 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002704 output->width,
2705 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002706}
2707
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002708WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002709weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002710 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002711{
2712 output->compositor = c;
2713 output->x = x;
2714 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002715 output->border.top = 0;
2716 output->border.bottom = 0;
2717 output->border.left = 0;
2718 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002719 output->mm_width = width;
2720 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002721 output->dirty = 1;
2722
Scott Moreau1bad5db2012-08-18 01:04:05 -06002723 if (transform != WL_OUTPUT_TRANSFORM_NORMAL)
2724 output->disable_planes++;
2725
2726 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06002727 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002728
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002729 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002730 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002731
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002732 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002733 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002734 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002735
Casey Dahlin58ba1372012-04-19 22:50:08 -04002736 output->id = ffs(~output->compositor->output_id_pool) - 1;
2737 output->compositor->output_id_pool |= 1 << output->id;
2738
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002739 output->global =
2740 wl_display_add_global(c->wl_display, &wl_output_interface,
2741 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002742}
2743
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002744static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002745compositor_bind(struct wl_client *client,
2746 void *data, uint32_t version, uint32_t id)
2747{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002748 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002749
2750 wl_client_add_object(client, &wl_compositor_interface,
2751 &compositor_interface, id, compositor);
2752}
2753
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002754static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002755log_uname(void)
2756{
2757 struct utsname usys;
2758
2759 uname(&usys);
2760
2761 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2762 usys.version, usys.machine);
2763}
2764
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002765WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002766weston_compositor_init(struct weston_compositor *ec,
2767 struct wl_display *display,
2768 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04002769 char *argv[],
2770 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002771{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002772 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002773 struct xkb_rule_names xkb_names;
2774 const struct config_key keyboard_config_keys[] = {
2775 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2776 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2777 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2778 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2779 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2780 };
2781 const struct config_section cs[] = {
2782 { "keyboard",
2783 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
2784 };
2785
2786 memset(&xkb_names, 0, sizeof(xkb_names));
2787 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002788
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002789 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002790 wl_signal_init(&ec->destroy_signal);
2791 wl_signal_init(&ec->activate_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002792 wl_signal_init(&ec->kill_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002793 wl_signal_init(&ec->lock_signal);
2794 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002795 wl_signal_init(&ec->show_input_panel_signal);
2796 wl_signal_init(&ec->hide_input_panel_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002797 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002798
Casey Dahlin58ba1372012-04-19 22:50:08 -04002799 ec->output_id_pool = 0;
2800
Kristian Høgsberga8873122011-11-23 10:39:34 -05002801 if (!wl_display_add_global(display, &wl_compositor_interface,
2802 ec, compositor_bind))
2803 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002804
Daniel Stone725c2c32012-06-22 14:04:36 +01002805 wl_list_init(&ec->surface_list);
2806 wl_list_init(&ec->layer_list);
2807 wl_list_init(&ec->seat_list);
2808 wl_list_init(&ec->output_list);
2809 wl_list_init(&ec->key_binding_list);
2810 wl_list_init(&ec->button_binding_list);
2811 wl_list_init(&ec->axis_binding_list);
2812 wl_list_init(&ec->fade.animation.link);
2813
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002814 weston_plane_init(&ec->primary_plane, 0, 0);
2815
Daniel Stone725c2c32012-06-22 14:04:36 +01002816 weston_compositor_xkb_init(ec, &xkb_names);
2817
2818 ec->ping_handler = NULL;
2819
2820 screenshooter_create(ec);
2821 text_cursor_position_notifier_create(ec);
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +02002822 text_model_factory_create(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002823
2824 wl_data_device_manager_init(ec->wl_display);
2825
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002826 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002827
Daniel Stone725c2c32012-06-22 14:04:36 +01002828 loop = wl_display_get_event_loop(ec->wl_display);
2829 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2830 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2831
2832 ec->input_loop = wl_event_loop_create();
2833
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002834 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
2835 ec->fade.animation.frame = fade_frame;
2836
2837 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2838 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2839
2840 weston_compositor_schedule_repaint(ec);
2841
Daniel Stone725c2c32012-06-22 14:04:36 +01002842 return 0;
2843}
2844
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002845WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002846weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002847{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002848 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002849
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002850 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002851 if (ec->input_loop_source)
2852 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002853
Matt Roper361d2ad2011-08-29 13:52:23 -07002854 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002855 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002856 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002857
Daniel Stone325fc2d2012-05-30 16:31:58 +01002858 weston_binding_list_destroy_all(&ec->key_binding_list);
2859 weston_binding_list_destroy_all(&ec->button_binding_list);
2860 weston_binding_list_destroy_all(&ec->axis_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002861
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002862 weston_plane_release(&ec->primary_plane);
2863
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002864 wl_array_release(&ec->vertices);
2865 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05002866 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002867
2868 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07002869}
2870
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002871static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002872{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002873 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002874
Martin Minarik6d118362012-06-07 18:01:59 +02002875 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002876 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002877
2878 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002879}
2880
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002881static void
2882on_segv_signal(int s, siginfo_t *siginfo, void *context)
2883{
2884 void *buffer[32];
2885 int i, count;
2886 Dl_info info;
2887
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002888 /* This SIGSEGV handler will do a best-effort backtrace, and
2889 * then call the backend restore function, which will switch
2890 * back to the vt we launched from or ungrab X etc and then
2891 * raise SIGTRAP. If we run weston under gdb from X or a
2892 * different vt, and tell gdb "handle SIGSEGV nostop", this
2893 * will allow weston to switch back to gdb on crash and then
2894 * gdb will catch the crash with SIGTRAP. */
2895
Martin Minarik6d118362012-06-07 18:01:59 +02002896 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002897
2898 count = backtrace(buffer, ARRAY_LENGTH(buffer));
2899 for (i = 0; i < count; i++) {
2900 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02002901 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002902 (long) buffer[i],
2903 info.dli_sname ? info.dli_sname : "--",
2904 info.dli_fname);
2905 }
2906
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002907 segv_compositor->restore(segv_compositor);
2908
2909 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002910}
2911
2912
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002913static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04002914load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002915{
2916 char path[PATH_MAX];
2917 void *module, *init;
2918
2919 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07002920 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002921 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02002922 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002923
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002924 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
2925 if (module) {
2926 weston_log("Module '%s' already loaded\n", path);
2927 dlclose(module);
2928 return NULL;
2929 }
2930
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002931 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04002932 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002933 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002934 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002935 return NULL;
2936 }
2937
2938 init = dlsym(module, entrypoint);
2939 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002940 weston_log("Failed to lookup init function: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002941 return NULL;
2942 }
2943
2944 return init;
2945}
2946
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002947static int
2948load_modules(struct weston_compositor *ec, const char *modules)
2949{
2950 const char *p, *end;
2951 char buffer[256];
2952 int (*module_init)(struct weston_compositor *ec);
2953
2954 if (modules == NULL)
2955 return 0;
2956
2957 p = modules;
2958 while (*p) {
2959 end = strchrnul(p, ',');
2960 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
2961 module_init = load_module(buffer, "module_init");
2962 if (module_init)
2963 module_init(ec);
2964 p = end;
2965 while (*p == ',')
2966 p++;
2967
2968 }
2969
2970 return 0;
2971}
2972
Pekka Paalanen78a0b572012-06-06 16:59:44 +03002973static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02002974 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
2975
2976static const char xdg_wrong_message[] =
2977 "fatal: environment variable XDG_RUNTIME_DIR\n"
2978 "is set to \"%s\", which is not a directory.\n";
2979
2980static const char xdg_wrong_mode_message[] =
2981 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
2982 "correctly. Unix access mode must be 0700 but is %o,\n"
2983 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04002984 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02002985
2986static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03002987 "Refer to your distribution on how to get it, or\n"
2988 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
2989 "on how to implement it.\n";
2990
Martin Minarik37032f82012-06-18 20:15:18 +02002991static void
2992verify_xdg_runtime_dir(void)
2993{
2994 char *dir = getenv("XDG_RUNTIME_DIR");
2995 struct stat s;
2996
2997 if (!dir) {
2998 weston_log(xdg_error_message);
2999 weston_log_continue(xdg_detail_message);
3000 exit(EXIT_FAILURE);
3001 }
3002
3003 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3004 weston_log(xdg_wrong_message, dir);
3005 weston_log_continue(xdg_detail_message);
3006 exit(EXIT_FAILURE);
3007 }
3008
3009 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3010 weston_log(xdg_wrong_mode_message,
3011 dir, s.st_mode & 0777, s.st_uid);
3012 weston_log_continue(xdg_detail_message);
3013 }
3014}
3015
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003016static int
3017usage(int error_code)
3018{
3019 fprintf(stderr,
3020 "Usage: weston [OPTIONS]\n\n"
3021 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3022 "Weston supports multiple backends, and depending on which backend is in use\n"
3023 "different options will be accepted.\n\n"
3024
3025
3026 "Core options:\n\n"
3027 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3028 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3029 " -S, --socket=NAME\tName of socket to listen on\n"
3030 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003031 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003032 " --log==FILE\t\tLog to the given file\n"
3033 " -h, --help\t\tThis help message\n\n");
3034
3035 fprintf(stderr,
3036 "Options for drm-backend.so:\n\n"
3037 " --connector=ID\tBring up only this connector\n"
3038 " --seat=SEAT\t\tThe seat that weston should run on\n"
3039 " --tty=TTY\t\tThe tty to use\n"
3040 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3041
3042 fprintf(stderr,
3043 "Options for x11-backend.so:\n\n"
3044 " --width=WIDTH\t\tWidth of X window\n"
3045 " --height=HEIGHT\tHeight of X window\n"
3046 " --fullscreen\t\tRun in fullscreen mode\n"
3047 " --output-count=COUNT\tCreate multiple outputs\n"
3048 " --no-input\t\tDont create input devices\n\n");
3049
3050 fprintf(stderr,
3051 "Options for wayland-backend.so:\n\n"
3052 " --width=WIDTH\t\tWidth of Wayland surface\n"
3053 " --height=HEIGHT\tHeight of Wayland surface\n"
3054 " --display=DISPLAY\tWayland display to connect to\n\n");
3055
3056 exit(error_code);
3057}
3058
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003059int main(int argc, char *argv[])
3060{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003061 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003062 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003063 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003064 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003065 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003066 struct sigaction segv_action;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003067 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003068 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003069 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003070 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003071 char *backend = NULL;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003072 const char *modules = "desktop-shell.so", *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003073 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003074 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003075 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003076 char *socket_name = "wayland-0";
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003077 char *config_file;
3078
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003079 const struct config_key core_config_keys[] = {
3080 { "modules", CONFIG_KEY_STRING, &modules },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003081 };
3082
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003083 const struct config_section cs[] = {
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003084 { "core",
3085 core_config_keys, ARRAY_LENGTH(core_config_keys) },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003086 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003087
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003088 const struct weston_option core_options[] = {
3089 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3090 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3091 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003092 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003093 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003094 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003095 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003096
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003097 argc = parse_options(core_options,
3098 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003099
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003100 if (help)
3101 usage(EXIT_SUCCESS);
3102
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003103 weston_log_file_open(log);
3104
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003105 weston_log("%s\n"
3106 STAMP_SPACE "%s\n"
3107 STAMP_SPACE "Bug reports to: %s\n"
3108 STAMP_SPACE "Build: %s\n",
3109 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003110 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003111 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003112
Martin Minarik37032f82012-06-18 20:15:18 +02003113 verify_xdg_runtime_dir();
3114
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003115 display = wl_display_create();
3116
Tiago Vignatti2116b892011-08-08 05:52:59 -07003117 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003118 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3119 display);
3120 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3121 display);
3122 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3123 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003124
3125 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003126 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3127 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003128
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003129 if (!backend) {
3130 if (getenv("WAYLAND_DISPLAY"))
3131 backend = "wayland-backend.so";
3132 else if (getenv("DISPLAY"))
3133 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003134 else
3135 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003136 }
3137
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003138 config_file = config_file_path("weston.ini");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003139 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003140
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003141 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003142 if (!backend_init)
3143 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003144
Daniel Stonec1be8e52012-06-01 11:14:02 -04003145 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003146 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003147 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003148 exit(EXIT_FAILURE);
3149 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003150
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003151 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3152 segv_action.sa_sigaction = on_segv_signal;
3153 sigemptyset(&segv_action.sa_mask);
3154 sigaction(SIGSEGV, &segv_action, NULL);
3155 segv_compositor = ec;
3156
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003157 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03003158 weston_log("fatal: unhandled option: %s\n", argv[i]);
3159 if (argv[1]) {
3160 ret = EXIT_FAILURE;
3161 goto out;
3162 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003163
Daniel Stonec1be8e52012-06-01 11:14:02 -04003164 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01003165
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003166 ec->option_idle_time = idle_time;
3167 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003168
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003169 setenv("WAYLAND_DISPLAY", socket_name, 1);
3170
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003171 if (load_modules(ec, modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003172 goto out;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003173 if (load_modules(ec, option_modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003174 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003175
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003176 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003177 weston_log("fatal: failed to add socket: %m\n");
3178 ret = EXIT_FAILURE;
3179 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003180 }
3181
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003182 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003183 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003184
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003185 wl_display_run(display);
3186
3187 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003188 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05003189 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003190
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003191 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003192
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003193 for (i = ARRAY_LENGTH(signals); i;)
3194 wl_event_source_remove(signals[--i]);
3195
Daniel Stone855028f2012-05-01 20:37:10 +01003196 weston_compositor_xkb_destroy(ec);
3197
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003198 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003199 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003200
Martin Minarik19e6f262012-06-07 13:08:46 +02003201 weston_log_file_close();
3202
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003203 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003204}