blob: bd95e8c60880a7ffd609ca7b31e2cb1a68d0c64d [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
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100240 if (compositor->renderer->create_surface(surface) < 0) {
241 free(surface);
242 return NULL;
243 }
244
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200245 surface->num_textures = 0;
246 surface->num_images = 0;
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200247 pixman_region32_init(&surface->texture_damage);
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200248
Benjamin Franzke06286262011-05-06 19:12:33 +0200249 surface->buffer = NULL;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400250 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400251 surface->plane = &compositor->primary_plane;
Benjamin Franzke06286262011-05-06 19:12:33 +0200252
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400253 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500254 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500255 pixman_region32_init(&surface->clip);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300256 region_init_infinite(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200257 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500258 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400259
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400260 surface->buffer_destroy_listener.notify =
261 surface_handle_buffer_destroy;
Benjamin Franzke06286262011-05-06 19:12:33 +0200262
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200263 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200264 wl_list_insert(&surface->geometry.transformation_list,
265 &surface->transform.position.link);
266 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200267 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200268 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500269
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300270 surface->pending.buffer_destroy_listener.notify =
271 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300272 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300273 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300274 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300275 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300276
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400277 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500278}
279
Alex Wu8811bf92012-02-28 18:07:54 +0800280WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500281weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200282 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500283{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100284 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500285}
286
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400287WL_EXPORT void
288weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200289 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200290{
291 if (surface->transform.enabled) {
292 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
293
294 weston_matrix_transform(&surface->transform.matrix, &v);
295
296 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200297 weston_log("warning: numerical instability in "
Pekka Paalanenece8a012012-02-08 15:23:15 +0200298 "weston_surface_to_global(), divisor = %g\n",
299 v.f[3]);
300 *x = 0;
301 *y = 0;
302 return;
303 }
304
305 *x = v.f[0] / v.f[3];
306 *y = v.f[1] / v.f[3];
307 } else {
308 *x = sx + surface->geometry.x;
309 *y = sy + surface->geometry.y;
310 }
311}
312
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500313WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400314weston_surface_move_to_plane(struct weston_surface *surface,
315 struct weston_plane *plane)
316{
317 if (surface->plane == plane)
318 return;
319
320 weston_surface_damage_below(surface);
321 surface->plane = plane;
322 weston_surface_damage(surface);
323}
324
325WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500326weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200327{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500328 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200329
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500330 pixman_region32_init(&damage);
331 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
332 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400333 pixman_region32_union(&surface->plane->damage,
334 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500335 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200336}
337
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400338static struct wl_resource *
339find_resource_for_client(struct wl_list *list, struct wl_client *client)
340{
341 struct wl_resource *r;
342
343 wl_list_for_each(r, list, link) {
344 if (r->client == client)
345 return r;
346 }
347
348 return NULL;
349}
350
351static void
352weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
353{
354 uint32_t different = es->output_mask ^ mask;
355 uint32_t entered = mask & different;
356 uint32_t left = es->output_mask & different;
357 struct weston_output *output;
358 struct wl_resource *resource = NULL;
359 struct wl_client *client = es->surface.resource.client;
360
361 es->output_mask = mask;
362 if (es->surface.resource.client == NULL)
363 return;
364 if (different == 0)
365 return;
366
367 wl_list_for_each(output, &es->compositor->output_list, link) {
368 if (1 << output->id & different)
369 resource =
370 find_resource_for_client(&output->resource_list,
371 client);
372 if (resource == NULL)
373 continue;
374 if (1 << output->id & entered)
375 wl_surface_send_enter(&es->surface.resource, resource);
376 if (1 << output->id & left)
377 wl_surface_send_leave(&es->surface.resource, resource);
378 }
379}
380
381static void
382weston_surface_assign_output(struct weston_surface *es)
383{
384 struct weston_compositor *ec = es->compositor;
385 struct weston_output *output, *new_output;
386 pixman_region32_t region;
387 uint32_t max, area, mask;
388 pixman_box32_t *e;
389
390 new_output = NULL;
391 max = 0;
392 mask = 0;
393 pixman_region32_init(&region);
394 wl_list_for_each(output, &ec->output_list, link) {
395 pixman_region32_intersect(&region, &es->transform.boundingbox,
396 &output->region);
397
398 e = pixman_region32_extents(&region);
399 area = (e->x2 - e->x1) * (e->y2 - e->y1);
400
401 if (area > 0)
402 mask |= 1 << output->id;
403
404 if (area >= max) {
405 new_output = output;
406 max = area;
407 }
408 }
409 pixman_region32_fini(&region);
410
411 es->output = new_output;
412 weston_surface_update_output_mask(es, mask);
413}
414
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200415static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200416surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
417 int32_t width, int32_t height,
418 pixman_region32_t *bbox)
419{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200420 float min_x = HUGE_VALF, min_y = HUGE_VALF;
421 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200422 int32_t s[4][2] = {
423 { sx, sy },
424 { sx, sy + height },
425 { sx + width, sy },
426 { sx + width, sy + height }
427 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200428 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200429 int i;
430
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300431 if (width == 0 || height == 0) {
432 /* avoid rounding empty bbox to 1x1 */
433 pixman_region32_init(bbox);
434 return;
435 }
436
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200437 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200438 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400439 weston_surface_to_global_float(surface,
440 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200441 if (x < min_x)
442 min_x = x;
443 if (x > max_x)
444 max_x = x;
445 if (y < min_y)
446 min_y = y;
447 if (y > max_y)
448 max_y = y;
449 }
450
Pekka Paalanen219b9822012-02-08 15:38:37 +0200451 int_x = floorf(min_x);
452 int_y = floorf(min_y);
453 pixman_region32_init_rect(bbox, int_x, int_y,
454 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200455}
456
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200457static void
458weston_surface_update_transform_disable(struct weston_surface *surface)
459{
460 surface->transform.enabled = 0;
461
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200462 /* round off fractions when not transformed */
463 surface->geometry.x = roundf(surface->geometry.x);
464 surface->geometry.y = roundf(surface->geometry.y);
465
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200466 pixman_region32_init_rect(&surface->transform.boundingbox,
467 surface->geometry.x,
468 surface->geometry.y,
469 surface->geometry.width,
470 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500471
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400472 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500473 pixman_region32_copy(&surface->transform.opaque,
474 &surface->opaque);
475 pixman_region32_translate(&surface->transform.opaque,
476 surface->geometry.x,
477 surface->geometry.y);
478 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200479}
480
481static int
482weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200483{
484 struct weston_matrix *matrix = &surface->transform.matrix;
485 struct weston_matrix *inverse = &surface->transform.inverse;
486 struct weston_transform *tform;
487
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200488 surface->transform.enabled = 1;
489
490 /* Otherwise identity matrix, but with x and y translation. */
491 surface->transform.position.matrix.d[12] = surface->geometry.x;
492 surface->transform.position.matrix.d[13] = surface->geometry.y;
493
494 weston_matrix_init(matrix);
495 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
496 weston_matrix_multiply(matrix, &tform->matrix);
497
498 if (weston_matrix_invert(inverse, matrix) < 0) {
499 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200500 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200501 " transformation not invertible.\n", surface);
502 return -1;
503 }
504
505 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
506 surface->geometry.height,
507 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500508
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200509 return 0;
510}
511
512WL_EXPORT void
513weston_surface_update_transform(struct weston_surface *surface)
514{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200515 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200516 return;
517
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200518 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200519
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500520 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200521
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200522 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500523 pixman_region32_fini(&surface->transform.opaque);
524 pixman_region32_init(&surface->transform.opaque);
525
Pekka Paalanencd403622012-01-25 13:37:39 +0200526 /* transform.position is always in transformation_list */
527 if (surface->geometry.transformation_list.next ==
528 &surface->transform.position.link &&
529 surface->geometry.transformation_list.prev ==
530 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200531 weston_surface_update_transform_disable(surface);
532 } else {
533 if (weston_surface_update_transform_enable(surface) < 0)
534 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200535 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200536
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400537 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200538
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300539 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200540}
541
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200542WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100543weston_surface_to_global_fixed(struct weston_surface *surface,
544 wl_fixed_t sx, wl_fixed_t sy,
545 wl_fixed_t *x, wl_fixed_t *y)
546{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200547 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100548
549 weston_surface_to_global_float(surface,
550 wl_fixed_to_double(sx),
551 wl_fixed_to_double(sy),
552 &xf, &yf);
553 *x = wl_fixed_from_double(xf);
554 *y = wl_fixed_from_double(yf);
555}
556
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400557WL_EXPORT void
558weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200559 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200560{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200561 if (surface->transform.enabled) {
562 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
563
564 weston_matrix_transform(&surface->transform.inverse, &v);
565
566 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200567 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200568 "weston_surface_from_global(), divisor = %g\n",
569 v.f[3]);
570 *sx = 0;
571 *sy = 0;
572 return;
573 }
574
Pekka Paalanencd403622012-01-25 13:37:39 +0200575 *sx = v.f[0] / v.f[3];
576 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200577 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200578 *sx = x - surface->geometry.x;
579 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200580 }
581}
582
583WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100584weston_surface_from_global_fixed(struct weston_surface *surface,
585 wl_fixed_t x, wl_fixed_t y,
586 wl_fixed_t *sx, wl_fixed_t *sy)
587{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200588 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100589
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400590 weston_surface_from_global_float(surface,
591 wl_fixed_to_double(x),
592 wl_fixed_to_double(y),
593 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100594 *sx = wl_fixed_from_double(sxf);
595 *sy = wl_fixed_from_double(syf);
596}
597
598WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200599weston_surface_from_global(struct weston_surface *surface,
600 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
601{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200602 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200603
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400604 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200605 *sx = floorf(sxf);
606 *sy = floorf(syf);
607}
608
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400609WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400610weston_surface_schedule_repaint(struct weston_surface *surface)
611{
612 struct weston_output *output;
613
614 wl_list_for_each(output, &surface->compositor->output_list, link)
615 if (surface->output_mask & (1 << output->id))
616 weston_output_schedule_repaint(output);
617}
618
619WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500620weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500621{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400622 pixman_region32_union_rect(&surface->damage, &surface->damage,
623 0, 0, surface->geometry.width,
624 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200625
Kristian Høgsberg98238702012-08-03 16:29:12 -0400626 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500627}
628
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400629WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500630weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200631 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400632{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200633 surface->geometry.x = x;
634 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200635 surface->geometry.width = width;
636 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200637 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400638}
639
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200640WL_EXPORT void
641weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200642 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200643{
644 surface->geometry.x = x;
645 surface->geometry.y = y;
646 surface->geometry.dirty = 1;
647}
648
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300649WL_EXPORT int
650weston_surface_is_mapped(struct weston_surface *surface)
651{
652 if (surface->output)
653 return 1;
654 else
655 return 0;
656}
657
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400658WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500659weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500660{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400661 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500662
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400663 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500664
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400665 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500666}
667
Tiago Vignatti9d393522012-02-10 16:26:19 +0200668static struct weston_surface *
669weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100670 wl_fixed_t x, wl_fixed_t y,
671 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200672{
673 struct weston_surface *surface;
674
675 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100676 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500677 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100678 wl_fixed_to_int(*sx),
679 wl_fixed_to_int(*sy),
680 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200681 return surface;
682 }
683
684 return NULL;
685}
686
687static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400688weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500689{
Scott Moreau447013d2012-02-18 05:05:29 -0700690 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500691 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400692 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500693
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400694 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100695 return;
696
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400697 surface = weston_compositor_pick_surface(seat->compositor,
698 pointer->x,
699 pointer->y,
700 &pointer->current_x,
701 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500702
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400703 if (&surface->surface != pointer->current) {
704 interface = pointer->grab->interface;
705 pointer->current = &surface->surface;
706 interface->focus(pointer->grab, &surface->surface,
707 pointer->current_x,
708 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500709 }
710
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400711 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500712 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100713 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400714 pointer->x,
715 pointer->y,
716 &pointer->grab->x,
717 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500718}
719
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400720static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500721weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400722{
Daniel Stone37816df2012-05-16 18:45:18 +0100723 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400724
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500725 if (!compositor->focus)
726 return;
727
Daniel Stone37816df2012-05-16 18:45:18 +0100728 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400729 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400730}
731
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400732WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500733weston_surface_unmap(struct weston_surface *surface)
734{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100735 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500736
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500737 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500738 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500739 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500740
Daniel Stone4dab5db2012-05-30 16:31:53 +0100741 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100742 if (seat->seat.keyboard &&
743 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100744 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100745 if (seat->seat.pointer &&
746 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100747 wl_pointer_set_focus(seat->seat.pointer,
748 NULL,
749 wl_fixed_from_int(0),
750 wl_fixed_from_int(0));
751 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500752
Kristian Høgsberg98238702012-08-03 16:29:12 -0400753 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500754}
755
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400756struct weston_frame_callback {
757 struct wl_resource resource;
758 struct wl_list link;
759};
760
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500761static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400762destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500763{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500764 struct weston_surface *surface =
765 container_of(resource,
766 struct weston_surface, surface.resource);
767 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400768 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400769
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300770 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500771 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400772
Pekka Paalanenbc106382012-10-10 12:49:31 +0300773 wl_list_for_each_safe(cb, next,
774 &surface->pending.frame_callback_list, link)
775 wl_resource_destroy(&cb->resource);
776
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300777 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300778 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +0300779 pixman_region32_fini(&surface->pending.damage);
780
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300781 if (surface->pending.buffer)
782 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
783
Benjamin Franzke06286262011-05-06 19:12:33 +0200784 if (surface->buffer)
785 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400786
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200787 pixman_region32_fini(&surface->texture_damage);
Kristian Høgsberg42263852012-09-06 21:59:29 -0400788 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200789
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200790 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200791 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500792 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500793 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300794 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200795
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400796 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
797 wl_resource_destroy(&cb->resource);
798
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400799 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500800}
801
Alex Wu8811bf92012-02-28 18:07:54 +0800802WL_EXPORT void
803weston_surface_destroy(struct weston_surface *surface)
804{
805 /* Not a valid way to destroy a client surface */
806 assert(surface->surface.resource.client == NULL);
807
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400808 wl_signal_emit(&surface->surface.resource.destroy_signal,
809 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800810 destroy_surface(&surface->surface.resource);
811}
812
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100813static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300814weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100815{
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300816 if (surface->buffer) {
817 weston_buffer_post_release(surface->buffer);
818 wl_list_remove(&surface->buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300819 }
820
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400821 if (buffer) {
822 buffer->busy_count++;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300823 wl_signal_add(&buffer->resource.destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300824 &surface->buffer_destroy_listener);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400825 } else {
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300826 if (weston_surface_is_mapped(surface))
827 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300828 }
829
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300830 surface->compositor->renderer->attach(surface, buffer);
831 surface->buffer = buffer;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100832}
833
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500834WL_EXPORT void
835weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400836{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500837 wl_list_remove(&surface->layer_link);
838 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500839 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500840 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400841}
842
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400843WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500844weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400845{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500846 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400847
848 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500849 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400850}
851
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500852WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500853weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +0200854{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400855 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200856 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200857
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400858 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -0500859 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200860}
861
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400862WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500863weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400864{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500865 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400866
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400867 pixman_region32_union(&compositor->primary_plane.damage,
868 &compositor->primary_plane.damage,
869 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400870 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400871}
872
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400873static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500874fade_frame(struct weston_animation *animation,
875 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400876{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500877 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400878 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500879 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500880 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400881
Scott Moreau94b0b0c2012-06-14 01:01:56 -0600882 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -0600883 compositor->fade.spring.timestamp = msecs;
884
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500885 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500886 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500887 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
888 compositor->fade.spring.current);
889 weston_surface_damage(surface);
890
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500891 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400892 compositor->fade.spring.current =
893 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400894 wl_list_remove(&animation->link);
895 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200896
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500897 if (compositor->fade.spring.current < 0.001) {
898 destroy_surface(&surface->surface.resource);
899 compositor->fade.surface = NULL;
900 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500901 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400902 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200903 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400904 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400905}
906
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400907static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400908surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400909 pixman_region32_t *opaque)
910{
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200911 if (surface->buffer && wl_buffer_is_shm(surface->buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400912 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400913
914 if (surface->transform.enabled) {
915 pixman_box32_t *extents;
916
917 extents = pixman_region32_extents(&surface->damage);
918 surface_compute_bbox(surface, extents->x1, extents->y1,
919 extents->x2 - extents->x1,
920 extents->y2 - extents->y1,
921 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400922 pixman_region32_translate(&surface->damage,
923 -surface->plane->x,
924 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400925 } else {
926 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400927 surface->geometry.x - surface->plane->x,
928 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400929 }
930
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +0200931 pixman_region32_subtract(&surface->damage,
932 &surface->damage, &surface->plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400933 pixman_region32_union(&surface->plane->damage,
934 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400935 empty_region(&surface->damage);
936 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +0200937 pixman_region32_union(&surface->plane->opaque,
938 &surface->plane->opaque,
939 &surface->transform.opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400940 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400941}
942
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400943static void
Rob Bradford0fb79752012-08-02 15:36:57 +0100944weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400945{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500946 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500947 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500948 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -0500949 struct weston_animation *animation, *next;
950 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +0200951 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300952 pixman_region32_t opaque, output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500953
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +0200954 weston_compositor_update_drag_surfaces(ec);
955
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500956 /* Rebuild the surface list and update surface transforms up front. */
957 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +0200958 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500959 wl_list_for_each(layer, &ec->layer_list, link) {
960 wl_list_for_each(es, &layer->surface_list, layer_link) {
961 weston_surface_update_transform(es);
962 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +0200963 if (es->output == output) {
964 wl_list_insert_list(&frame_callback_list,
965 &es->frame_callback_list);
966 wl_list_init(&es->frame_callback_list);
967 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500968 }
969 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +0200970
Kristian Høgsberg79af73e2012-08-03 15:45:23 -0400971 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800972 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -0400973 else
974 wl_list_for_each(es, &ec->surface_list, link)
975 weston_surface_move_to_plane(es, &ec->primary_plane);
976
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500977 pixman_region32_init(&opaque);
978
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +0200979 pixman_region32_fini(&ec->primary_plane.opaque);
980 pixman_region32_init(&ec->primary_plane.opaque);
981
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400982 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400983 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500984
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400985 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -0400986
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400987 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400988 pixman_region32_intersect(&output_damage,
989 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300990 pixman_region32_subtract(&ec->primary_plane.damage,
991 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400992
Scott Moreauccbf29d2012-02-22 14:21:41 -0700993 if (output->dirty)
994 weston_output_update_matrix(output);
995
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400996 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400997
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500998 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500999
Kristian Høgsbergef044142011-06-21 15:02:12 -04001000 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001001
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001002 weston_compositor_repick(ec);
1003 wl_event_loop_dispatch(ec->input_loop, 0);
1004
Jonas Ådahldb773762012-06-13 00:01:21 +02001005 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001006 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001007 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001008 }
1009
Scott Moreaud64cf212012-06-08 19:40:54 -06001010 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001011 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001012 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001013 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001014}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001015
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001016static int
1017weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001018{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001019 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001020
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001021 wl_event_loop_dispatch(compositor->input_loop, 0);
1022
1023 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001024}
1025
1026WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001027weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001028{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001029 struct weston_compositor *compositor = output->compositor;
1030 struct wl_event_loop *loop =
1031 wl_display_get_event_loop(compositor->wl_display);
1032 int fd;
1033
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001034 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001035 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001036 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001037 return;
1038 }
1039
1040 output->repaint_scheduled = 0;
1041 if (compositor->input_loop_source)
1042 return;
1043
1044 fd = wl_event_loop_get_fd(compositor->input_loop);
1045 compositor->input_loop_source =
1046 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1047 weston_compositor_read_input, compositor);
1048}
1049
1050static void
1051idle_repaint(void *data)
1052{
1053 struct weston_output *output = data;
1054
1055 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001056}
1057
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001058WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001059weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1060{
1061 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001062 if (below != NULL)
1063 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001064}
1065
1066WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001067weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001068{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001069 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001070 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001071
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001072 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001073 return;
1074
Kristian Høgsbergef044142011-06-21 15:02:12 -04001075 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001076 output->repaint_needed = 1;
1077 if (output->repaint_scheduled)
1078 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001079
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001080 wl_event_loop_add_idle(loop, idle_repaint, output);
1081 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001082
1083 if (compositor->input_loop_source) {
1084 wl_event_source_remove(compositor->input_loop_source);
1085 compositor->input_loop_source = NULL;
1086 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001087}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001088
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001089WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001090weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1091{
1092 struct weston_output *output;
1093
1094 wl_list_for_each(output, &compositor->output_list, link)
1095 weston_output_schedule_repaint(output);
1096}
1097
1098WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001099weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001100{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001101 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001102 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001103
Scott Moreau9d1b1122012-06-08 19:40:53 -06001104 output = container_of(compositor->output_list.next,
1105 struct weston_output, link);
1106
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001107 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001108 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001109 return;
1110
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001111 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001112 surface = weston_surface_create(compositor);
John Kåre Alsakere2e3d072012-10-12 12:25:09 +02001113 if (!surface)
1114 return;
1115
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001116 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001117 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001118 wl_list_insert(&compositor->fade_layer.surface_list,
1119 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03001120 weston_surface_update_transform(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001121 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001122 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001123 }
1124
1125 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001126 if (wl_list_empty(&compositor->fade.animation.link)) {
1127 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001128 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001129 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001130 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001131}
1132
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001133static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001134surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001135{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001136 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001137}
1138
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001139static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001140surface_attach(struct wl_client *client,
1141 struct wl_resource *resource,
1142 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1143{
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001144 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001145 struct wl_buffer *buffer = NULL;
1146
1147 if (buffer_resource)
1148 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001149
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001150 if (surface->pending.buffer)
1151 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001152
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001153 surface->pending.sx = sx;
1154 surface->pending.sy = sy;
1155 surface->pending.buffer = buffer;
1156 if (buffer) {
1157 wl_signal_add(&buffer->resource.destroy_signal,
1158 &surface->pending.buffer_destroy_listener);
1159 surface->pending.remove_contents = 0;
1160 } else {
1161 surface->pending.remove_contents = 1;
1162 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001163}
1164
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001165static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001166surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001167 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001168 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001169{
Pekka Paalanen8e159182012-10-10 12:49:25 +03001170 struct weston_surface *surface = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001171
Pekka Paalanen8e159182012-10-10 12:49:25 +03001172 pixman_region32_union_rect(&surface->pending.damage,
1173 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001174 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001175}
1176
Kristian Høgsberg33418202011-08-16 23:01:28 -04001177static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001178destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001179{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001180 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001181
1182 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001183 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001184}
1185
1186static void
1187surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001188 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001189{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001190 struct weston_frame_callback *cb;
Pekka Paalanenbc106382012-10-10 12:49:31 +03001191 struct weston_surface *surface = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001192
1193 cb = malloc(sizeof *cb);
1194 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001195 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001196 return;
1197 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001198
Kristian Høgsberg33418202011-08-16 23:01:28 -04001199 cb->resource.object.interface = &wl_callback_interface;
1200 cb->resource.object.id = callback;
1201 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001202 cb->resource.client = client;
1203 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001204
1205 wl_client_add_resource(client, &cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001206 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001207}
1208
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001209static void
1210surface_set_opaque_region(struct wl_client *client,
1211 struct wl_resource *resource,
1212 struct wl_resource *region_resource)
1213{
1214 struct weston_surface *surface = resource->data;
1215 struct weston_region *region;
1216
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001217 if (region_resource) {
1218 region = region_resource->data;
Pekka Paalanen512dde82012-10-10 12:49:27 +03001219 pixman_region32_copy(&surface->pending.opaque,
1220 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001221 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001222 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001223 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001224}
1225
1226static void
1227surface_set_input_region(struct wl_client *client,
1228 struct wl_resource *resource,
1229 struct wl_resource *region_resource)
1230{
1231 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001232 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001233
1234 if (region_resource) {
1235 region = region_resource->data;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001236 pixman_region32_copy(&surface->pending.input,
1237 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001238 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001239 pixman_region32_fini(&surface->pending.input);
1240 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001241 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001242}
1243
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001244static void
1245surface_commit(struct wl_client *client, struct wl_resource *resource)
1246{
1247 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001248 pixman_region32_t opaque;
1249
1250 if (surface->pending.sx || surface->pending.sy ||
1251 (surface->pending.buffer &&
1252 (surface->pending.buffer->width != surface->geometry.width ||
1253 surface->pending.buffer->height != surface->geometry.height)))
1254 surface->geometry.dirty = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001255
1256 /* wl_surface.attach */
1257 if (surface->pending.buffer || surface->pending.remove_contents)
1258 weston_surface_attach(surface, surface->pending.buffer);
1259
1260 if (surface->buffer && surface->configure)
1261 surface->configure(surface, surface->pending.sx,
1262 surface->pending.sy);
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001263 surface->pending.sx = 0;
1264 surface->pending.sy = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001265
1266 /* wl_surface.damage */
1267 pixman_region32_union(&surface->damage, &surface->damage,
1268 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001269 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1270 0, 0,
1271 surface->geometry.width,
1272 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001273 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001274
1275 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001276 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001277 surface->geometry.width,
1278 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001279 pixman_region32_intersect(&opaque,
1280 &opaque, &surface->pending.opaque);
1281
1282 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1283 pixman_region32_copy(&surface->opaque, &opaque);
1284 surface->geometry.dirty = 1;
1285 }
1286
1287 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001288
1289 /* wl_surface.set_input_region */
1290 pixman_region32_fini(&surface->input);
1291 pixman_region32_init_rect(&surface->input, 0, 0,
1292 surface->geometry.width,
1293 surface->geometry.height);
1294 pixman_region32_intersect(&surface->input,
1295 &surface->input, &surface->pending.input);
1296
Pekka Paalanenbc106382012-10-10 12:49:31 +03001297 /* wl_surface.frame */
1298 wl_list_insert_list(&surface->frame_callback_list,
1299 &surface->pending.frame_callback_list);
1300 wl_list_init(&surface->pending.frame_callback_list);
1301
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001302 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001303}
1304
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001305static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001306 surface_destroy,
1307 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001308 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001309 surface_frame,
1310 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001311 surface_set_input_region,
1312 surface_commit
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001313};
1314
1315static void
1316compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001317 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001318{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001319 struct weston_compositor *ec = resource->data;
1320 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001321
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001322 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001323 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001324 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001325 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001326 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001327
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001328 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001329
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001330 surface->surface.resource.object.id = id;
1331 surface->surface.resource.object.interface = &wl_surface_interface;
1332 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001333 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001334 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001335
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001336 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001337}
1338
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001339static void
1340destroy_region(struct wl_resource *resource)
1341{
1342 struct weston_region *region =
1343 container_of(resource, struct weston_region, resource);
1344
1345 pixman_region32_fini(&region->region);
1346 free(region);
1347}
1348
1349static void
1350region_destroy(struct wl_client *client, struct wl_resource *resource)
1351{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001352 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001353}
1354
1355static void
1356region_add(struct wl_client *client, struct wl_resource *resource,
1357 int32_t x, int32_t y, int32_t width, int32_t height)
1358{
1359 struct weston_region *region = resource->data;
1360
1361 pixman_region32_union_rect(&region->region, &region->region,
1362 x, y, width, height);
1363}
1364
1365static void
1366region_subtract(struct wl_client *client, struct wl_resource *resource,
1367 int32_t x, int32_t y, int32_t width, int32_t height)
1368{
1369 struct weston_region *region = resource->data;
1370 pixman_region32_t rect;
1371
1372 pixman_region32_init_rect(&rect, x, y, width, height);
1373 pixman_region32_subtract(&region->region, &region->region, &rect);
1374 pixman_region32_fini(&rect);
1375}
1376
1377static const struct wl_region_interface region_interface = {
1378 region_destroy,
1379 region_add,
1380 region_subtract
1381};
1382
1383static void
1384compositor_create_region(struct wl_client *client,
1385 struct wl_resource *resource, uint32_t id)
1386{
1387 struct weston_region *region;
1388
1389 region = malloc(sizeof *region);
1390 if (region == NULL) {
1391 wl_resource_post_no_memory(resource);
1392 return;
1393 }
1394
1395 region->resource.destroy = destroy_region;
1396
1397 region->resource.object.id = id;
1398 region->resource.object.interface = &wl_region_interface;
1399 region->resource.object.implementation =
1400 (void (**)(void)) &region_interface;
1401 region->resource.data = region;
1402
1403 pixman_region32_init(&region->region);
1404
1405 wl_client_add_resource(client, &region->resource);
1406}
1407
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001408static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001409 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001410 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001411};
1412
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001413WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001414weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001415{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001416 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1417 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001418
1419 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001420 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001421}
1422
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001423static void
1424weston_compositor_dpms_on(struct weston_compositor *compositor)
1425{
1426 struct weston_output *output;
1427
1428 wl_list_for_each(output, &compositor->output_list, link)
1429 if (output->set_dpms)
1430 output->set_dpms(output, WESTON_DPMS_ON);
1431}
1432
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001433WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001434weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001435{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001436 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1437 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001438 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001439 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001440 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001441 }
1442}
1443
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001444static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001445weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001446{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001447 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001448 compositor->idle_inhibit++;
1449}
1450
1451static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001452weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001453{
1454 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001455 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001456}
1457
1458static int
1459idle_handler(void *data)
1460{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001461 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001462
1463 if (compositor->idle_inhibit)
1464 return 1;
1465
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001466 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001467
1468 return 1;
1469}
1470
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001471WL_EXPORT void
1472weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1473{
1474 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001475 pixman_region32_init(&plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001476 plane->x = x;
1477 plane->y = y;
1478}
1479
1480WL_EXPORT void
1481weston_plane_release(struct weston_plane *plane)
1482{
1483 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001484 pixman_region32_fini(&plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001485}
1486
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001487static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001488weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001489
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001490static void
Daniel Stone37816df2012-05-16 18:45:18 +01001491clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001492{
Daniel Stone37816df2012-05-16 18:45:18 +01001493 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001494 struct weston_output *output, *prev = NULL;
1495 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001496
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001497 x = wl_fixed_to_int(*fx);
1498 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001499 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1500 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001501
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001502 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001503 if (pixman_region32_contains_point(&output->region,
1504 x, y, NULL))
1505 valid = 1;
1506 if (pixman_region32_contains_point(&output->region,
1507 old_x, old_y, NULL))
1508 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001509 }
Daniel Stone37816df2012-05-16 18:45:18 +01001510
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001511 if (!valid) {
1512 if (x < prev->x)
1513 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001514 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001515 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001516 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001517 if (y < prev->y)
1518 *fy = wl_fixed_from_int(prev->y);
1519 else if (y >= prev->y + prev->current->height)
1520 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001521 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001522 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001523}
1524
1525WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001526notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001527{
1528 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001529 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001530 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001531 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001532 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001533
1534 weston_compositor_activity(ec);
1535
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001536 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001537
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001538 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001539
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001540 pointer->x = x;
1541 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001542
1543 ix = wl_fixed_to_int(x);
1544 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001545
Scott Moreauccbf29d2012-02-22 14:21:41 -07001546 wl_list_for_each(output, &ec->output_list, link)
1547 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001548 pixman_region32_contains_point(&output->region,
1549 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06001550 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001551
Daniel Stone37816df2012-05-16 18:45:18 +01001552 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001553 interface = pointer->grab->interface;
1554 interface->motion(pointer->grab, time,
1555 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001556
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001557 if (seat->sprite) {
1558 weston_surface_set_position(seat->sprite,
1559 ix - seat->hotspot_x,
1560 iy - seat->hotspot_y);
1561 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001562 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001563}
1564
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001565WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001566weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001567 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001568{
Daniel Stone37816df2012-05-16 18:45:18 +01001569 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001570
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001571 if (seat->seat.keyboard) {
1572 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1573 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04001574 }
1575
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001576 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001577}
1578
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001579WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001580notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001581 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001582{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001583 struct weston_compositor *compositor = seat->compositor;
1584 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001585 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001586 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001587 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001588
Daniel Stone4dbadb12012-05-30 16:31:51 +01001589 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001590 if (compositor->ping_handler && focus)
1591 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001592 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001593 if (pointer->button_count == 0) {
1594 pointer->grab_button = button;
1595 pointer->grab_time = time;
1596 pointer->grab_x = pointer->x;
1597 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001598 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001599 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001600 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001601 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001602 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001603 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001604
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001605 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001606 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001607
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001608 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001609
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001610 if (pointer->button_count == 1)
1611 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001612 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001613}
1614
Scott Moreau210d0792012-03-22 10:47:01 -06001615WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001616notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01001617 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001618{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001619 struct weston_compositor *compositor = seat->compositor;
1620 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001621 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001622 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001623 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1624
1625 if (compositor->ping_handler && focus)
1626 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001627
1628 weston_compositor_activity(compositor);
1629
Scott Moreau6a3633d2012-03-20 08:47:59 -06001630 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001631 weston_compositor_run_axis_binding(compositor, seat,
1632 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06001633 else
1634 return;
1635
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001636 if (pointer->focus_resource)
1637 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01001638 value);
Scott Moreau210d0792012-03-22 10:47:01 -06001639}
1640
Daniel Stone05d58682012-06-22 13:21:38 +01001641WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001642notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001643{
Daniel Stone05d58682012-06-22 13:21:38 +01001644 struct wl_keyboard *keyboard = &seat->keyboard;
1645 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01001646 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001647 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01001648 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01001649 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01001650
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001651 /* Serialize and update our internal state, checking to see if it's
1652 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01001653 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1654 XKB_STATE_DEPRESSED);
1655 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1656 XKB_STATE_LATCHED);
1657 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1658 XKB_STATE_LOCKED);
1659 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01001660 XKB_STATE_EFFECTIVE);
1661
Daniel Stone71c38772012-06-22 13:21:30 +01001662 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
1663 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
1664 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
1665 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01001666 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01001667
Daniel Stone71c38772012-06-22 13:21:30 +01001668 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
1669 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
1670 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
1671 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001672
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001673 /* And update the modifier_state for bindings. */
1674 mods_lookup = mods_depressed | mods_latched;
1675 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01001676 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001677 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01001678 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001679 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01001680 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001681 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04001682 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
1683 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01001684
Daniel Stone8c8164f2012-05-30 16:31:45 +01001685 /* Finally, notify the compositor that LEDs have changed. */
1686 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001687 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001688 leds |= LED_NUM_LOCK;
1689 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001690 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001691 leds |= LED_CAPS_LOCK;
1692 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001693 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001694 leds |= LED_SCROLL_LOCK;
1695 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01001696 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01001697 seat->xkb_state.leds = leds;
1698
Daniel Stone05d58682012-06-22 13:21:38 +01001699 if (changed) {
1700 grab->interface->modifiers(grab,
1701 serial,
1702 keyboard->modifiers.mods_depressed,
1703 keyboard->modifiers.mods_latched,
1704 keyboard->modifiers.mods_locked,
1705 keyboard->modifiers.group);
1706 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001707}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001708
Daniel Stone05d58682012-06-22 13:21:38 +01001709static void
1710update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001711 enum wl_keyboard_key_state state)
1712{
1713 enum xkb_key_direction direction;
1714
1715 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1716 direction = XKB_KEY_DOWN;
1717 else
1718 direction = XKB_KEY_UP;
1719
1720 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1721 * broken keycode system, which starts at 8. */
1722 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1723
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001724 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001725}
1726
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001727WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001728notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001729 enum wl_keyboard_key_state state,
1730 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001731{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001732 struct weston_compositor *compositor = seat->compositor;
1733 struct wl_keyboard *keyboard = seat->seat.keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01001734 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001735 (struct weston_surface *) keyboard->focus;
1736 struct wl_keyboard_grab *grab = keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001737 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001738 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001739
Daniel Stonec9785ea2012-05-30 16:31:52 +01001740 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001741 if (compositor->ping_handler && focus)
1742 compositor->ping_handler(focus, serial);
1743
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001744 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001745 keyboard->grab_key = key;
1746 keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07001747 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001748 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001749 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001750
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001751 end = keyboard->keys.data + keyboard->keys.size;
1752 for (k = keyboard->keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01001753 if (*k == key) {
1754 /* Ignore server-generated repeats. */
1755 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1756 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001757 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01001758 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001759 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001760 keyboard->keys.size = (void *) end - keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001761 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001762 k = wl_array_add(&keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001763 *k = key;
1764 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001765
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001766 if (grab == &keyboard->default_grab) {
1767 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001768 state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001769 grab = keyboard->grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06001770 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001771
Daniel Stone7ace3902012-05-30 16:31:40 +01001772 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01001773
1774 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001775 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01001776 wl_display_get_serial(compositor->wl_display),
1777 key,
1778 state);
1779 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001780}
1781
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001782WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001783notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01001784 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001785{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001786 struct weston_compositor *compositor = seat->compositor;
1787 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001788
1789 if (output) {
Kristian Høgsberg00fbbe62012-10-23 13:04:09 -04001790 clip_pointer_motion(seat, &x, &y);
Daniel Stone37816df2012-05-16 18:45:18 +01001791 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001792 x - pointer->x,
1793 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001794
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001795 pointer->x = x;
1796 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001797 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001798 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001799 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001800 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03001801 /* FIXME: We should call wl_pointer_set_focus(seat,
1802 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001803 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001804}
1805
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001806static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001807destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001808{
Daniel Stone37816df2012-05-16 18:45:18 +01001809 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001810
Daniel Stone37816df2012-05-16 18:45:18 +01001811 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001812 saved_kbd_focus_listener);
1813
Daniel Stone37816df2012-05-16 18:45:18 +01001814 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001815}
1816
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001817WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001818notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001819 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001820{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001821 struct weston_compositor *compositor = seat->compositor;
1822 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001823 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001824 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001825
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001826 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001827 wl_array_copy(&keyboard->keys, keys);
1828 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001829 weston_compositor_idle_inhibit(compositor);
1830 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001831 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001832 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001833 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001834
Daniel Stoneef172672012-06-22 13:21:32 +01001835 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001836 wl_array_for_each(k, &keyboard->keys) {
1837 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01001838 WL_KEYBOARD_KEY_STATE_PRESSED);
1839 }
1840
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001841 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001842
1843 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001844 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1845 wl_keyboard_set_focus(keyboard, surface);
1846 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001847 }
1848}
1849
1850WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001851notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01001852{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001853 struct weston_compositor *compositor = seat->compositor;
1854 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001855 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001856
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001857 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001858 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001859 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001860 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001861 WL_KEYBOARD_KEY_STATE_RELEASED);
1862 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001863
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001864 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001865
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001866 if (keyboard->focus) {
1867 seat->saved_kbd_focus = keyboard->focus;
1868 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01001869 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001870 wl_signal_add(&keyboard->focus->resource.destroy_signal,
1871 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001872 }
1873
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001874 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001875 /* FIXME: We really need keyboard grab cancel here to
1876 * let the grab shut down properly. As it is we leak
1877 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001878 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001879}
1880
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001881static void
Daniel Stone37816df2012-05-16 18:45:18 +01001882touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001883{
Daniel Stone37816df2012-05-16 18:45:18 +01001884 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001885 struct wl_resource *resource;
1886
Pekka Paalanen56464252012-07-31 13:21:08 +03001887 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001888 return;
1889
Pekka Paalanen56464252012-07-31 13:21:08 +03001890 if (seat->touch->focus_resource)
1891 wl_list_remove(&seat->touch->focus_listener.link);
1892 seat->touch->focus = NULL;
1893 seat->touch->focus_resource = NULL;
1894
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001895 if (surface) {
1896 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01001897 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04001898 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001899 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02001900 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001901 return;
1902 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001903
Daniel Stone37816df2012-05-16 18:45:18 +01001904 seat->touch->focus = surface;
1905 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03001906 wl_signal_add(&resource->destroy_signal,
1907 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001908 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001909}
1910
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001911/**
1912 * notify_touch - emulates button touches and notifies surfaces accordingly.
1913 *
1914 * It assumes always the correct cycle sequence until it gets here: touch_down
1915 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1916 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001917 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001918 */
1919WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001920notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001921 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001922{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001923 struct weston_compositor *ec = seat->compositor;
1924 struct wl_touch *touch = seat->seat.touch;
Matt Roper47c1b982012-10-10 16:56:53 -07001925 struct wl_touch_grab *grab = touch->grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001926 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001927 wl_fixed_t sx, sy;
Matt Roper47c1b982012-10-10 16:56:53 -07001928
1929 /* Update grab's global coordinates. */
1930 touch->grab_x = x;
1931 touch->grab_y = y;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001932
1933 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01001934 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001935 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001936
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001937 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001938
1939 /* the first finger down picks the surface, and all further go
1940 * to that surface for the remainder of the touch session i.e.
1941 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001942 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001943 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001944 touch_set_focus(seat, &es->surface);
1945 } else if (touch->focus) {
1946 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001947 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001948 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001949
Matt Roper47c1b982012-10-10 16:56:53 -07001950 grab->interface->down(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001951 break;
Daniel Stone37816df2012-05-16 18:45:18 +01001952 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001953 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001954 if (!es)
1955 break;
1956
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001957 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Matt Roper47c1b982012-10-10 16:56:53 -07001958 grab->interface->motion(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001959 break;
Daniel Stone37816df2012-05-16 18:45:18 +01001960 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001961 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001962 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001963
Matt Roper47c1b982012-10-10 16:56:53 -07001964 grab->interface->up(grab, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001965 if (seat->num_tp == 0)
1966 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001967 break;
1968 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001969}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001970
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001971static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001972pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1973{
1974 struct weston_seat *seat = container_of(listener, struct weston_seat,
1975 sprite_destroy_listener);
1976
1977 seat->sprite = NULL;
1978}
1979
1980static void
1981pointer_cursor_surface_configure(struct weston_surface *es,
1982 int32_t dx, int32_t dy)
1983{
1984 struct weston_seat *seat = es->private;
1985 int x, y;
1986
1987 assert(es == seat->sprite);
1988
1989 seat->hotspot_x -= dx;
1990 seat->hotspot_y -= dy;
1991
1992 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
1993 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
1994
1995 weston_surface_configure(seat->sprite, x, y,
1996 es->buffer->width, es->buffer->height);
1997
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001998 empty_region(&es->pending.input);
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03001999
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002000 if (!weston_surface_is_mapped(es)) {
2001 wl_list_insert(&es->compositor->cursor_layer.surface_list,
2002 &es->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002003 weston_surface_update_transform(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002004 }
2005}
2006
2007static void
2008pointer_unmap_sprite(struct weston_seat *seat)
2009{
2010 if (weston_surface_is_mapped(seat->sprite))
2011 weston_surface_unmap(seat->sprite);
2012
2013 wl_list_remove(&seat->sprite_destroy_listener.link);
2014 seat->sprite->configure = NULL;
2015 seat->sprite->private = NULL;
2016 seat->sprite = NULL;
2017}
2018
2019static void
2020pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2021 uint32_t serial, struct wl_resource *surface_resource,
2022 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002023{
Daniel Stone37816df2012-05-16 18:45:18 +01002024 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002025 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002026
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002027 if (surface_resource)
Pekka Paalanen6c71ee12012-10-10 12:49:30 +03002028 surface = surface_resource->data;
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002029
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04002030 if (seat->seat.pointer->focus == NULL)
2031 return;
2032 if (seat->seat.pointer->focus->resource.client != client)
2033 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002034 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002035 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002036
2037 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002038 if (surface->configure) {
2039 wl_resource_post_error(&surface->surface.resource,
2040 WL_DISPLAY_ERROR_INVALID_OBJECT,
2041 "surface->configure already "
2042 "set");
2043 return;
2044 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002045 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002046
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002047 if (seat->sprite)
2048 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002049
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002050 if (!surface)
2051 return;
2052
2053 wl_signal_add(&surface->surface.resource.destroy_signal,
2054 &seat->sprite_destroy_listener);
2055
2056 surface->configure = pointer_cursor_surface_configure;
2057 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002058 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002059 seat->hotspot_x = x;
2060 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002061
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04002062 if (surface->buffer)
2063 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002064}
2065
Daniel Stone37816df2012-05-16 18:45:18 +01002066static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002067 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002068};
2069
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002070static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002071handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002072{
Daniel Stone37816df2012-05-16 18:45:18 +01002073 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002074
Daniel Stone37816df2012-05-16 18:45:18 +01002075 seat = container_of(listener, struct weston_seat,
2076 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002077
Daniel Stone37816df2012-05-16 18:45:18 +01002078 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002079}
2080
Casey Dahlin9074db52012-04-19 22:50:09 -04002081static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002082{
2083 wl_list_remove(&resource->link);
2084 free(resource);
2085}
2086
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002087static void
Daniel Stone37816df2012-05-16 18:45:18 +01002088seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2089 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002090{
Daniel Stone37816df2012-05-16 18:45:18 +01002091 struct weston_seat *seat = resource->data;
2092 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002093
Daniel Stone37816df2012-05-16 18:45:18 +01002094 if (!seat->seat.pointer)
2095 return;
2096
2097 cr = wl_client_add_object(client, &wl_pointer_interface,
2098 &pointer_interface, id, seat);
2099 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002100 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002101
2102 if (seat->seat.pointer->focus &&
2103 seat->seat.pointer->focus->resource.client == client) {
2104 struct weston_surface *surface;
2105 wl_fixed_t sx, sy;
2106
2107 surface = (struct weston_surface *) seat->seat.pointer->focus;
2108 weston_surface_from_global_fixed(surface,
2109 seat->seat.pointer->x,
2110 seat->seat.pointer->y,
2111 &sx,
2112 &sy);
2113 wl_pointer_set_focus(seat->seat.pointer,
2114 seat->seat.pointer->focus,
2115 sx,
2116 sy);
2117 }
Daniel Stone37816df2012-05-16 18:45:18 +01002118}
2119
2120static void
2121seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2122 uint32_t id)
2123{
2124 struct weston_seat *seat = resource->data;
2125 struct wl_resource *cr;
2126
2127 if (!seat->seat.keyboard)
2128 return;
2129
2130 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2131 seat);
2132 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002133 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002134
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002135 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2136 seat->xkb_info.keymap_fd,
2137 seat->xkb_info.keymap_size);
2138
Daniel Stone17b13bd2012-05-30 16:31:39 +01002139 if (seat->seat.keyboard->focus &&
2140 seat->seat.keyboard->focus->resource.client == client) {
2141 wl_keyboard_set_focus(seat->seat.keyboard,
2142 seat->seat.keyboard->focus);
2143 }
Daniel Stone37816df2012-05-16 18:45:18 +01002144}
2145
2146static void
2147seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2148 uint32_t id)
2149{
2150 struct weston_seat *seat = resource->data;
2151 struct wl_resource *cr;
2152
2153 if (!seat->seat.touch)
2154 return;
2155
2156 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2157 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002158 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002159}
2160
2161static const struct wl_seat_interface seat_interface = {
2162 seat_get_pointer,
2163 seat_get_keyboard,
2164 seat_get_touch,
2165};
2166
2167static void
2168bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2169{
2170 struct wl_seat *seat = data;
2171 struct wl_resource *resource;
2172 enum wl_seat_capability caps = 0;
2173
2174 resource = wl_client_add_object(client, &wl_seat_interface,
2175 &seat_interface, id, data);
2176 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002177 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002178
2179 if (seat->pointer)
2180 caps |= WL_SEAT_CAPABILITY_POINTER;
2181 if (seat->keyboard)
2182 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2183 if (seat->touch)
2184 caps |= WL_SEAT_CAPABILITY_TOUCH;
2185
2186 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002187}
2188
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002189static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002190device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002191{
Daniel Stone37816df2012-05-16 18:45:18 +01002192 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002193
Daniel Stone37816df2012-05-16 18:45:18 +01002194 seat = container_of(listener, struct weston_seat,
2195 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002196
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002197 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002198}
2199
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002200static void weston_compositor_xkb_init(struct weston_compositor *ec,
2201 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002202{
Daniel Stonee379da92012-05-30 16:32:04 +01002203 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002204 ec->xkb_context = xkb_context_new(0);
2205 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002206 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002207 exit(1);
2208 }
Daniel Stone994679a2012-05-30 16:31:43 +01002209 }
2210
2211 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002212 ec->xkb_names = *names;
2213 if (!ec->xkb_names.rules)
2214 ec->xkb_names.rules = strdup("evdev");
2215 if (!ec->xkb_names.model)
2216 ec->xkb_names.model = strdup("pc105");
2217 if (!ec->xkb_names.layout)
2218 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002219}
2220
Daniel Stonee379da92012-05-30 16:32:04 +01002221static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2222{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002223 if (xkb_info->keymap)
2224 xkb_map_unref(xkb_info->keymap);
2225
2226 if (xkb_info->keymap_area)
2227 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2228 if (xkb_info->keymap_fd >= 0)
2229 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002230}
2231
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002232static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2233{
Daniel Stonee379da92012-05-30 16:32:04 +01002234 free((char *) ec->xkb_names.rules);
2235 free((char *) ec->xkb_names.model);
2236 free((char *) ec->xkb_names.layout);
2237 free((char *) ec->xkb_names.variant);
2238 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002239
Daniel Stonee379da92012-05-30 16:32:04 +01002240 xkb_info_destroy(&ec->xkb_info);
2241 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002242}
2243
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002244static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002245weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002246{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002247 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002248
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002249 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2250 XKB_MOD_NAME_SHIFT);
2251 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2252 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002253 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2254 XKB_MOD_NAME_CTRL);
2255 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2256 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002257 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2258 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002259 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2260 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002261 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002262
2263 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2264 XKB_LED_NAME_NUM);
2265 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2266 XKB_LED_NAME_CAPS);
2267 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2268 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002269
2270 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2271 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002272 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002273 exit(EXIT_FAILURE);
2274 }
2275 xkb_info->keymap_size = strlen(keymap_str) + 1;
2276
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002277 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002278 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002279 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002280 (unsigned long) xkb_info->keymap_size);
2281 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002282 }
2283
2284 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2285 PROT_READ | PROT_WRITE,
2286 MAP_SHARED, xkb_info->keymap_fd, 0);
2287 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002288 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002289 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002290 goto err_dev_zero;
2291 }
2292 strcpy(xkb_info->keymap_area, keymap_str);
2293 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002294
2295 return;
2296
2297err_dev_zero:
2298 close(xkb_info->keymap_fd);
2299 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002300err_keymap_str:
2301 free(keymap_str);
2302 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002303}
2304
2305static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002306weston_compositor_build_global_keymap(struct weston_compositor *ec)
2307{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002308 if (ec->xkb_info.keymap != NULL)
2309 return;
2310
Daniel Stonee379da92012-05-30 16:32:04 +01002311 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002312 &ec->xkb_names,
2313 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002314 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002315 weston_log("failed to compile global XKB keymap\n");
2316 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002317 "options %s",
2318 ec->xkb_names.rules, ec->xkb_names.model,
2319 ec->xkb_names.layout, ec->xkb_names.variant,
2320 ec->xkb_names.options);
2321 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002322 }
2323
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002324 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002325}
2326
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002327WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002328weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002329{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002330 if (seat->has_keyboard)
2331 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002332
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002333 if (keymap != NULL) {
2334 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002335 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002336 }
2337 else {
2338 weston_compositor_build_global_keymap(seat->compositor);
2339 seat->xkb_info = seat->compositor->xkb_info;
2340 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2341 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002342
2343 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2344 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002345 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01002346 exit(1);
2347 }
2348
Daniel Stone71c38772012-06-22 13:21:30 +01002349 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002350
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002351 wl_keyboard_init(&seat->keyboard);
2352 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2353
2354 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01002355}
2356
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002357WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002358weston_seat_init_pointer(struct weston_seat *seat)
2359{
2360 if (seat->has_pointer)
2361 return;
2362
2363 wl_pointer_init(&seat->pointer);
2364 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2365
2366 seat->has_pointer = 1;
2367}
2368
2369WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002370weston_seat_init_touch(struct weston_seat *seat)
2371{
2372 if (seat->has_touch)
2373 return;
2374
2375 wl_touch_init(&seat->touch);
2376 wl_seat_set_touch(&seat->seat, &seat->touch);
2377
2378 seat->has_touch = 1;
2379}
2380
2381WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002382weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002383{
Daniel Stone37816df2012-05-16 18:45:18 +01002384 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002385 seat->has_pointer = 0;
2386 seat->has_keyboard = 0;
2387 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002388
Daniel Stone37816df2012-05-16 18:45:18 +01002389 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2390 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002391
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002392 seat->sprite = NULL;
2393 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002394
Daniel Stone37816df2012-05-16 18:45:18 +01002395 seat->compositor = ec;
2396 seat->hotspot_x = 16;
2397 seat->hotspot_y = 16;
2398 seat->modifier_state = 0;
2399 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002400
Daniel Stone37816df2012-05-16 18:45:18 +01002401 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002402 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002403
Daniel Stone37816df2012-05-16 18:45:18 +01002404 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002405
Daniel Stone37816df2012-05-16 18:45:18 +01002406 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2407 wl_signal_add(&seat->seat.drag_icon_signal,
2408 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002409
2410 clipboard_create(seat);
Jan Arne Petersene829adc2012-08-10 16:47:22 +02002411 input_method_create(ec, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002412}
2413
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002414WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002415weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002416{
Daniel Stone37816df2012-05-16 18:45:18 +01002417 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002418 /* The global object is destroyed at wl_display_destroy() time. */
2419
Daniel Stone37816df2012-05-16 18:45:18 +01002420 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002421 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002422
Daniel Stone74419a22012-05-30 16:32:02 +01002423 if (seat->xkb_state.state != NULL)
2424 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01002425 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002426
Daniel Stone37816df2012-05-16 18:45:18 +01002427 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002428}
2429
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002430static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002431drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2432{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002433 empty_region(&es->pending.input);
2434
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002435 weston_surface_configure(es,
2436 es->geometry.x + sx, es->geometry.y + sy,
2437 es->buffer->width, es->buffer->height);
2438}
2439
2440static int
Daniel Stone37816df2012-05-16 18:45:18 +01002441device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002442 struct weston_surface *surface)
2443{
Daniel Stone37816df2012-05-16 18:45:18 +01002444 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002445
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002446 if (surface->configure) {
2447 wl_resource_post_error(&surface->surface.resource,
2448 WL_DISPLAY_ERROR_INVALID_OBJECT,
2449 "surface->configure already set");
2450 return 0;
2451 }
2452
Daniel Stone37816df2012-05-16 18:45:18 +01002453 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002454
Daniel Stone37816df2012-05-16 18:45:18 +01002455 weston_surface_set_position(ws->drag_surface,
2456 wl_fixed_to_double(seat->pointer->x),
2457 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002458
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002459 surface->configure = drag_surface_configure;
2460
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002461 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002462 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002463
2464 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002465}
2466
2467static void
Daniel Stone37816df2012-05-16 18:45:18 +01002468device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002469{
Ander Conselvan de Oliveira5fd55802012-10-11 14:06:19 +03002470 if (weston_surface_is_mapped(seat->drag_surface))
2471 weston_surface_unmap(seat->drag_surface);
2472
Daniel Stone37816df2012-05-16 18:45:18 +01002473 seat->drag_surface->configure = NULL;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002474 empty_region(&seat->drag_surface->pending.input);
Daniel Stone37816df2012-05-16 18:45:18 +01002475 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2476 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002477}
2478
2479static void
Daniel Stone37816df2012-05-16 18:45:18 +01002480device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002481{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002482 struct wl_list *list;
2483
Daniel Stone37816df2012-05-16 18:45:18 +01002484 if (weston_surface_is_mapped(seat->drag_surface) ||
2485 !seat->drag_surface->buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002486 return;
2487
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002488 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2489 list = &seat->sprite->layer_link;
2490 else
2491 list = &seat->compositor->cursor_layer.surface_list;
2492
2493 wl_list_insert(list, &seat->drag_surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002494 weston_surface_update_transform(seat->drag_surface);
Daniel Stone37816df2012-05-16 18:45:18 +01002495 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002496}
2497
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002498static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002499weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01002500 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002501{
2502 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002503
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002504 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002505 return;
2506
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002507 if (seat->drag_surface && seat->seat.drag_surface &&
2508 (&seat->drag_surface->surface.resource !=
2509 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002510 /* between calls to this funcion we got a new drag_surface */
2511 surface_changed = 1;
2512
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002513 if (!seat->seat.drag_surface || surface_changed) {
2514 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002515 if (!surface_changed)
2516 return;
2517 }
2518
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002519 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002520 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002521 seat->seat.drag_surface;
2522 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002523 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002524 }
2525
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002526 /* the client may not have attached a buffer to the drag surface
2527 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002528 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002529
2530 if (!dx && !dy)
2531 return;
2532
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002533 weston_surface_set_position(seat->drag_surface,
2534 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
2535 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002536}
2537
2538WL_EXPORT void
2539weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2540{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002541 struct weston_seat *seat;
2542
2543 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002544 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002545}
2546
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002547static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002548bind_output(struct wl_client *client,
2549 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002550{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002551 struct weston_output *output = data;
2552 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002553 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002554
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002555 resource = wl_client_add_object(client,
2556 &wl_output_interface, NULL, id, data);
2557
Casey Dahlin9074db52012-04-19 22:50:09 -04002558 wl_list_insert(&output->resource_list, &resource->link);
2559 resource->destroy = unbind_resource;
2560
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002561 wl_output_send_geometry(resource,
2562 output->x,
2563 output->y,
2564 output->mm_width,
2565 output->mm_height,
2566 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002567 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002568 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002569
2570 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002571 wl_output_send_mode(resource,
2572 mode->flags,
2573 mode->width,
2574 mode->height,
2575 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002576 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002577}
2578
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002579WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002580weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002581{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002582 struct weston_compositor *c = output->compositor;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002583 int i;
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002584
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002585 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002586
2587 for (i = 0; i < 2; i++)
2588 pixman_region32_fini(&output->buffer_damage[i]);
2589
Casey Dahlin9074db52012-04-19 22:50:09 -04002590 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002591
2592 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002593}
2594
Scott Moreau1bad5db2012-08-18 01:04:05 -06002595static void
2596weston_output_compute_transform(struct weston_output *output)
2597{
2598 struct weston_matrix transform;
2599 int flip;
2600
2601 weston_matrix_init(&transform);
2602
2603 switch(output->transform) {
2604 case WL_OUTPUT_TRANSFORM_FLIPPED:
2605 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2606 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2607 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2608 flip = -1;
2609 break;
2610 default:
2611 flip = 1;
2612 break;
2613 }
2614
2615 switch(output->transform) {
2616 case WL_OUTPUT_TRANSFORM_NORMAL:
2617 case WL_OUTPUT_TRANSFORM_FLIPPED:
2618 transform.d[0] = flip;
2619 transform.d[1] = 0;
2620 transform.d[4] = 0;
2621 transform.d[5] = 1;
2622 break;
2623 case WL_OUTPUT_TRANSFORM_90:
2624 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2625 transform.d[0] = 0;
2626 transform.d[1] = -flip;
2627 transform.d[4] = 1;
2628 transform.d[5] = 0;
2629 break;
2630 case WL_OUTPUT_TRANSFORM_180:
2631 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2632 transform.d[0] = -flip;
2633 transform.d[1] = 0;
2634 transform.d[4] = 0;
2635 transform.d[5] = -1;
2636 break;
2637 case WL_OUTPUT_TRANSFORM_270:
2638 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2639 transform.d[0] = 0;
2640 transform.d[1] = flip;
2641 transform.d[4] = -1;
2642 transform.d[5] = 0;
2643 break;
2644 default:
2645 break;
2646 }
2647
2648 weston_matrix_multiply(&output->matrix, &transform);
2649}
2650
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002651WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002652weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002653{
Scott Moreau850ca422012-05-21 15:21:25 -06002654 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002655 struct weston_matrix camera;
2656 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002657
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002658 weston_matrix_init(&output->matrix);
2659 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002660 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2661 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002662
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002663 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002664 2.0 / (output->width + output->border.left + output->border.right),
2665 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2666
2667 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002668
Scott Moreauccbf29d2012-02-22 14:21:41 -07002669 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002670 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002671 weston_matrix_init(&camera);
2672 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002673 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002674 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002675 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002676 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002677 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002678 weston_matrix_multiply(&output->matrix, &modelview);
2679 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002680
Scott Moreauccbf29d2012-02-22 14:21:41 -07002681 output->dirty = 0;
2682}
2683
Scott Moreau1bad5db2012-08-18 01:04:05 -06002684static void
2685weston_output_transform_init(struct weston_output *output, uint32_t transform)
2686{
2687 output->transform = transform;
2688
2689 switch (transform) {
2690 case WL_OUTPUT_TRANSFORM_90:
2691 case WL_OUTPUT_TRANSFORM_270:
2692 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2693 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2694 /* Swap width and height */
2695 output->width = output->current->height;
2696 output->height = output->current->width;
2697 break;
2698 case WL_OUTPUT_TRANSFORM_NORMAL:
2699 case WL_OUTPUT_TRANSFORM_180:
2700 case WL_OUTPUT_TRANSFORM_FLIPPED:
2701 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2702 output->width = output->current->width;
2703 output->height = output->current->height;
2704 break;
2705 default:
2706 break;
2707 }
2708}
2709
Scott Moreauccbf29d2012-02-22 14:21:41 -07002710WL_EXPORT void
2711weston_output_move(struct weston_output *output, int x, int y)
2712{
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002713 int i;
2714
Scott Moreauccbf29d2012-02-22 14:21:41 -07002715 output->x = x;
2716 output->y = y;
2717
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002718 output->current_buffer = 0;
2719 for (i = 0; i < 2; i++)
2720 pixman_region32_init(&output->buffer_damage[i]);
2721
Scott Moreauccbf29d2012-02-22 14:21:41 -07002722 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002723 output->width,
2724 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002725}
2726
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002727WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002728weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002729 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002730{
2731 output->compositor = c;
2732 output->x = x;
2733 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002734 output->border.top = 0;
2735 output->border.bottom = 0;
2736 output->border.left = 0;
2737 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002738 output->mm_width = width;
2739 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002740 output->dirty = 1;
2741
Scott Moreau1bad5db2012-08-18 01:04:05 -06002742 if (transform != WL_OUTPUT_TRANSFORM_NORMAL)
2743 output->disable_planes++;
2744
2745 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06002746 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002747
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002748 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002749 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002750
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002751 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002752 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002753 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002754
Casey Dahlin58ba1372012-04-19 22:50:08 -04002755 output->id = ffs(~output->compositor->output_id_pool) - 1;
2756 output->compositor->output_id_pool |= 1 << output->id;
2757
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002758 output->global =
2759 wl_display_add_global(c->wl_display, &wl_output_interface,
2760 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002761}
2762
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002763static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002764compositor_bind(struct wl_client *client,
2765 void *data, uint32_t version, uint32_t id)
2766{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002767 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002768
2769 wl_client_add_object(client, &wl_compositor_interface,
2770 &compositor_interface, id, compositor);
2771}
2772
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002773static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002774log_uname(void)
2775{
2776 struct utsname usys;
2777
2778 uname(&usys);
2779
2780 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2781 usys.version, usys.machine);
2782}
2783
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002784WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002785weston_compositor_init(struct weston_compositor *ec,
2786 struct wl_display *display,
2787 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04002788 char *argv[],
2789 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002790{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002791 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002792 struct xkb_rule_names xkb_names;
2793 const struct config_key keyboard_config_keys[] = {
2794 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2795 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2796 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2797 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2798 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2799 };
2800 const struct config_section cs[] = {
2801 { "keyboard",
2802 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
2803 };
2804
2805 memset(&xkb_names, 0, sizeof(xkb_names));
2806 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002807
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002808 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002809 wl_signal_init(&ec->destroy_signal);
2810 wl_signal_init(&ec->activate_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002811 wl_signal_init(&ec->kill_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002812 wl_signal_init(&ec->lock_signal);
2813 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002814 wl_signal_init(&ec->show_input_panel_signal);
2815 wl_signal_init(&ec->hide_input_panel_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002816 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002817
Casey Dahlin58ba1372012-04-19 22:50:08 -04002818 ec->output_id_pool = 0;
2819
Kristian Høgsberga8873122011-11-23 10:39:34 -05002820 if (!wl_display_add_global(display, &wl_compositor_interface,
2821 ec, compositor_bind))
2822 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002823
Daniel Stone725c2c32012-06-22 14:04:36 +01002824 wl_list_init(&ec->surface_list);
2825 wl_list_init(&ec->layer_list);
2826 wl_list_init(&ec->seat_list);
2827 wl_list_init(&ec->output_list);
2828 wl_list_init(&ec->key_binding_list);
2829 wl_list_init(&ec->button_binding_list);
2830 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002831 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002832 wl_list_init(&ec->fade.animation.link);
2833
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002834 weston_plane_init(&ec->primary_plane, 0, 0);
2835
Daniel Stone725c2c32012-06-22 14:04:36 +01002836 weston_compositor_xkb_init(ec, &xkb_names);
2837
2838 ec->ping_handler = NULL;
2839
2840 screenshooter_create(ec);
2841 text_cursor_position_notifier_create(ec);
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +02002842 text_model_factory_create(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002843
2844 wl_data_device_manager_init(ec->wl_display);
2845
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002846 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002847
Daniel Stone725c2c32012-06-22 14:04:36 +01002848 loop = wl_display_get_event_loop(ec->wl_display);
2849 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2850 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2851
2852 ec->input_loop = wl_event_loop_create();
2853
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002854 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
2855 ec->fade.animation.frame = fade_frame;
2856
2857 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2858 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2859
2860 weston_compositor_schedule_repaint(ec);
2861
Daniel Stone725c2c32012-06-22 14:04:36 +01002862 return 0;
2863}
2864
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002865WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002866weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002867{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002868 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002869
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002870 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002871 if (ec->input_loop_source)
2872 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002873
Matt Roper361d2ad2011-08-29 13:52:23 -07002874 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002875 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002876 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002877
Daniel Stone325fc2d2012-05-30 16:31:58 +01002878 weston_binding_list_destroy_all(&ec->key_binding_list);
2879 weston_binding_list_destroy_all(&ec->button_binding_list);
2880 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002881 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002882
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002883 weston_plane_release(&ec->primary_plane);
2884
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002885 wl_array_release(&ec->vertices);
2886 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05002887 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002888
2889 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07002890}
2891
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002892static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002893{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002894 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002895
Martin Minarik6d118362012-06-07 18:01:59 +02002896 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002897 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002898
2899 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002900}
2901
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002902static void
2903on_segv_signal(int s, siginfo_t *siginfo, void *context)
2904{
2905 void *buffer[32];
2906 int i, count;
2907 Dl_info info;
2908
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002909 /* This SIGSEGV handler will do a best-effort backtrace, and
2910 * then call the backend restore function, which will switch
2911 * back to the vt we launched from or ungrab X etc and then
2912 * raise SIGTRAP. If we run weston under gdb from X or a
2913 * different vt, and tell gdb "handle SIGSEGV nostop", this
2914 * will allow weston to switch back to gdb on crash and then
2915 * gdb will catch the crash with SIGTRAP. */
2916
Martin Minarik6d118362012-06-07 18:01:59 +02002917 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002918
2919 count = backtrace(buffer, ARRAY_LENGTH(buffer));
2920 for (i = 0; i < count; i++) {
2921 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02002922 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002923 (long) buffer[i],
2924 info.dli_sname ? info.dli_sname : "--",
2925 info.dli_fname);
2926 }
2927
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002928 segv_compositor->restore(segv_compositor);
2929
2930 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002931}
2932
2933
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002934static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04002935load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002936{
2937 char path[PATH_MAX];
2938 void *module, *init;
2939
2940 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07002941 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002942 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02002943 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002944
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002945 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
2946 if (module) {
2947 weston_log("Module '%s' already loaded\n", path);
2948 dlclose(module);
2949 return NULL;
2950 }
2951
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002952 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04002953 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002954 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002955 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002956 return NULL;
2957 }
2958
2959 init = dlsym(module, entrypoint);
2960 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002961 weston_log("Failed to lookup init function: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002962 return NULL;
2963 }
2964
2965 return init;
2966}
2967
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002968static int
2969load_modules(struct weston_compositor *ec, const char *modules)
2970{
2971 const char *p, *end;
2972 char buffer[256];
2973 int (*module_init)(struct weston_compositor *ec);
2974
2975 if (modules == NULL)
2976 return 0;
2977
2978 p = modules;
2979 while (*p) {
2980 end = strchrnul(p, ',');
2981 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
2982 module_init = load_module(buffer, "module_init");
2983 if (module_init)
2984 module_init(ec);
2985 p = end;
2986 while (*p == ',')
2987 p++;
2988
2989 }
2990
2991 return 0;
2992}
2993
Pekka Paalanen78a0b572012-06-06 16:59:44 +03002994static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02002995 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
2996
2997static const char xdg_wrong_message[] =
2998 "fatal: environment variable XDG_RUNTIME_DIR\n"
2999 "is set to \"%s\", which is not a directory.\n";
3000
3001static const char xdg_wrong_mode_message[] =
3002 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3003 "correctly. Unix access mode must be 0700 but is %o,\n"
3004 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003005 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003006
3007static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003008 "Refer to your distribution on how to get it, or\n"
3009 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3010 "on how to implement it.\n";
3011
Martin Minarik37032f82012-06-18 20:15:18 +02003012static void
3013verify_xdg_runtime_dir(void)
3014{
3015 char *dir = getenv("XDG_RUNTIME_DIR");
3016 struct stat s;
3017
3018 if (!dir) {
3019 weston_log(xdg_error_message);
3020 weston_log_continue(xdg_detail_message);
3021 exit(EXIT_FAILURE);
3022 }
3023
3024 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3025 weston_log(xdg_wrong_message, dir);
3026 weston_log_continue(xdg_detail_message);
3027 exit(EXIT_FAILURE);
3028 }
3029
3030 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3031 weston_log(xdg_wrong_mode_message,
3032 dir, s.st_mode & 0777, s.st_uid);
3033 weston_log_continue(xdg_detail_message);
3034 }
3035}
3036
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003037static int
3038usage(int error_code)
3039{
3040 fprintf(stderr,
3041 "Usage: weston [OPTIONS]\n\n"
3042 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3043 "Weston supports multiple backends, and depending on which backend is in use\n"
3044 "different options will be accepted.\n\n"
3045
3046
3047 "Core options:\n\n"
3048 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3049 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3050 " -S, --socket=NAME\tName of socket to listen on\n"
3051 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003052 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003053 " --log==FILE\t\tLog to the given file\n"
3054 " -h, --help\t\tThis help message\n\n");
3055
3056 fprintf(stderr,
3057 "Options for drm-backend.so:\n\n"
3058 " --connector=ID\tBring up only this connector\n"
3059 " --seat=SEAT\t\tThe seat that weston should run on\n"
3060 " --tty=TTY\t\tThe tty to use\n"
3061 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3062
3063 fprintf(stderr,
3064 "Options for x11-backend.so:\n\n"
3065 " --width=WIDTH\t\tWidth of X window\n"
3066 " --height=HEIGHT\tHeight of X window\n"
3067 " --fullscreen\t\tRun in fullscreen mode\n"
3068 " --output-count=COUNT\tCreate multiple outputs\n"
3069 " --no-input\t\tDont create input devices\n\n");
3070
3071 fprintf(stderr,
3072 "Options for wayland-backend.so:\n\n"
3073 " --width=WIDTH\t\tWidth of Wayland surface\n"
3074 " --height=HEIGHT\tHeight of Wayland surface\n"
3075 " --display=DISPLAY\tWayland display to connect to\n\n");
3076
3077 exit(error_code);
3078}
3079
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003080int main(int argc, char *argv[])
3081{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003082 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003083 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003084 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003085 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003086 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003087 struct sigaction segv_action;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003088 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003089 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003090 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003091 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003092 char *backend = NULL;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003093 const char *modules = "desktop-shell.so", *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003094 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003095 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003096 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003097 char *socket_name = "wayland-0";
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003098 char *config_file;
3099
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003100 const struct config_key core_config_keys[] = {
3101 { "modules", CONFIG_KEY_STRING, &modules },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003102 };
3103
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003104 const struct config_section cs[] = {
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003105 { "core",
3106 core_config_keys, ARRAY_LENGTH(core_config_keys) },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003107 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003108
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003109 const struct weston_option core_options[] = {
3110 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3111 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3112 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003113 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003114 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003115 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003116 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003117
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003118 argc = parse_options(core_options,
3119 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003120
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003121 if (help)
3122 usage(EXIT_SUCCESS);
3123
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003124 weston_log_file_open(log);
3125
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003126 weston_log("%s\n"
3127 STAMP_SPACE "%s\n"
3128 STAMP_SPACE "Bug reports to: %s\n"
3129 STAMP_SPACE "Build: %s\n",
3130 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003131 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003132 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003133
Martin Minarik37032f82012-06-18 20:15:18 +02003134 verify_xdg_runtime_dir();
3135
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003136 display = wl_display_create();
3137
Tiago Vignatti2116b892011-08-08 05:52:59 -07003138 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003139 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3140 display);
3141 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3142 display);
3143 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3144 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003145
3146 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003147 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3148 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003149
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003150 if (!backend) {
3151 if (getenv("WAYLAND_DISPLAY"))
3152 backend = "wayland-backend.so";
3153 else if (getenv("DISPLAY"))
3154 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003155 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003156 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003157 }
3158
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003159 config_file = config_file_path("weston.ini");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003160 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003161
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003162 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003163 if (!backend_init)
3164 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003165
Daniel Stonec1be8e52012-06-01 11:14:02 -04003166 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003167 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003168 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003169 exit(EXIT_FAILURE);
3170 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003171
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003172 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3173 segv_action.sa_sigaction = on_segv_signal;
3174 sigemptyset(&segv_action.sa_mask);
3175 sigaction(SIGSEGV, &segv_action, NULL);
3176 segv_compositor = ec;
3177
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003178 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03003179 weston_log("fatal: unhandled option: %s\n", argv[i]);
3180 if (argv[1]) {
3181 ret = EXIT_FAILURE;
3182 goto out;
3183 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003184
Daniel Stonec1be8e52012-06-01 11:14:02 -04003185 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01003186
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003187 ec->option_idle_time = idle_time;
3188 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003189
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003190 setenv("WAYLAND_DISPLAY", socket_name, 1);
3191
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003192 if (load_modules(ec, modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003193 goto out;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003194 if (load_modules(ec, option_modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003195 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003196
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003197 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003198 weston_log("fatal: failed to add socket: %m\n");
3199 ret = EXIT_FAILURE;
3200 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003201 }
3202
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003203 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003204 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003205
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003206 wl_display_run(display);
3207
3208 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003209 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05003210 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003211
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003212 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003213
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003214 for (i = ARRAY_LENGTH(signals); i;)
3215 wl_event_source_remove(signals[--i]);
3216
Daniel Stone855028f2012-05-01 20:37:10 +01003217 weston_compositor_xkb_destroy(ec);
3218
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003219 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003220 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003221
Martin Minarik19e6f262012-06-07 13:08:46 +02003222 weston_log_file_close();
3223
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003224 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003225}