blob: b17a1b31a6826e60b792d6a889891a2f240055a1 [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
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500206static const pixman_region32_data_t undef_region_data;
207
208static void
209undef_region(pixman_region32_t *region)
210{
Kristian Høgsberg66a099b2012-05-29 16:49:45 -0400211 if (region->data != &undef_region_data)
212 pixman_region32_fini(region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500213 region->data = (pixman_region32_data_t *) &undef_region_data;
214}
215
216static int
217region_is_undefined(pixman_region32_t *region)
218{
219 return region->data == &undef_region_data;
220}
221
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200222static void
223empty_region(pixman_region32_t *region)
224{
225 if (!region_is_undefined(region))
226 pixman_region32_fini(region);
227
228 pixman_region32_init(region);
229}
230
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500231WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500232weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500233{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500234 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400235
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200236 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400237 if (surface == NULL)
238 return NULL;
239
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400240 wl_signal_init(&surface->surface.resource.destroy_signal);
Kristian Høgsberg48891542012-02-23 17:38:33 -0500241
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500242 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500243 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500244
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400245 surface->surface.resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400246
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500247 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400248 surface->alpha = 1.0;
Juan Zhao46436612012-03-20 01:48:46 +0800249 surface->pitch = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400250
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200251 surface->num_textures = 0;
252 surface->num_images = 0;
253
Benjamin Franzke06286262011-05-06 19:12:33 +0200254 surface->buffer = NULL;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400255 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400256 surface->plane = &compositor->primary_plane;
Benjamin Franzke06286262011-05-06 19:12:33 +0200257
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400258 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500259 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500260 pixman_region32_init(&surface->clip);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500261 undef_region(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200262 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500263 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400264
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400265 surface->buffer_destroy_listener.notify =
266 surface_handle_buffer_destroy;
Benjamin Franzke06286262011-05-06 19:12:33 +0200267
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200268 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200269 wl_list_insert(&surface->geometry.transformation_list,
270 &surface->transform.position.link);
271 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200272 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200273 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500274
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300275 surface->pending.buffer_destroy_listener.notify =
276 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300277 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300278
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400279 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500280}
281
Alex Wu8811bf92012-02-28 18:07:54 +0800282WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500283weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200284 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500285{
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500286 surface->color[0] = red;
287 surface->color[1] = green;
288 surface->color[2] = blue;
289 surface->color[3] = alpha;
290 surface->shader = &surface->compositor->solid_shader;
291}
292
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400293WL_EXPORT void
294weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200295 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200296{
297 if (surface->transform.enabled) {
298 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
299
300 weston_matrix_transform(&surface->transform.matrix, &v);
301
302 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200303 weston_log("warning: numerical instability in "
Pekka Paalanenece8a012012-02-08 15:23:15 +0200304 "weston_surface_to_global(), divisor = %g\n",
305 v.f[3]);
306 *x = 0;
307 *y = 0;
308 return;
309 }
310
311 *x = v.f[0] / v.f[3];
312 *y = v.f[1] / v.f[3];
313 } else {
314 *x = sx + surface->geometry.x;
315 *y = sy + surface->geometry.y;
316 }
317}
318
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500319WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400320weston_surface_move_to_plane(struct weston_surface *surface,
321 struct weston_plane *plane)
322{
323 if (surface->plane == plane)
324 return;
325
326 weston_surface_damage_below(surface);
327 surface->plane = plane;
328 weston_surface_damage(surface);
329}
330
331WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500332weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200333{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500334 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200335
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500336 pixman_region32_init(&damage);
337 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
338 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400339 pixman_region32_union(&surface->plane->damage,
340 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500341 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200342}
343
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400344static struct wl_resource *
345find_resource_for_client(struct wl_list *list, struct wl_client *client)
346{
347 struct wl_resource *r;
348
349 wl_list_for_each(r, list, link) {
350 if (r->client == client)
351 return r;
352 }
353
354 return NULL;
355}
356
357static void
358weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
359{
360 uint32_t different = es->output_mask ^ mask;
361 uint32_t entered = mask & different;
362 uint32_t left = es->output_mask & different;
363 struct weston_output *output;
364 struct wl_resource *resource = NULL;
365 struct wl_client *client = es->surface.resource.client;
366
367 es->output_mask = mask;
368 if (es->surface.resource.client == NULL)
369 return;
370 if (different == 0)
371 return;
372
373 wl_list_for_each(output, &es->compositor->output_list, link) {
374 if (1 << output->id & different)
375 resource =
376 find_resource_for_client(&output->resource_list,
377 client);
378 if (resource == NULL)
379 continue;
380 if (1 << output->id & entered)
381 wl_surface_send_enter(&es->surface.resource, resource);
382 if (1 << output->id & left)
383 wl_surface_send_leave(&es->surface.resource, resource);
384 }
385}
386
387static void
388weston_surface_assign_output(struct weston_surface *es)
389{
390 struct weston_compositor *ec = es->compositor;
391 struct weston_output *output, *new_output;
392 pixman_region32_t region;
393 uint32_t max, area, mask;
394 pixman_box32_t *e;
395
396 new_output = NULL;
397 max = 0;
398 mask = 0;
399 pixman_region32_init(&region);
400 wl_list_for_each(output, &ec->output_list, link) {
401 pixman_region32_intersect(&region, &es->transform.boundingbox,
402 &output->region);
403
404 e = pixman_region32_extents(&region);
405 area = (e->x2 - e->x1) * (e->y2 - e->y1);
406
407 if (area > 0)
408 mask |= 1 << output->id;
409
410 if (area >= max) {
411 new_output = output;
412 max = area;
413 }
414 }
415 pixman_region32_fini(&region);
416
417 es->output = new_output;
418 weston_surface_update_output_mask(es, mask);
419}
420
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200421static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200422surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
423 int32_t width, int32_t height,
424 pixman_region32_t *bbox)
425{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200426 float min_x = HUGE_VALF, min_y = HUGE_VALF;
427 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200428 int32_t s[4][2] = {
429 { sx, sy },
430 { sx, sy + height },
431 { sx + width, sy },
432 { sx + width, sy + height }
433 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200434 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200435 int i;
436
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300437 if (width == 0 || height == 0) {
438 /* avoid rounding empty bbox to 1x1 */
439 pixman_region32_init(bbox);
440 return;
441 }
442
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200443 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200444 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400445 weston_surface_to_global_float(surface,
446 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200447 if (x < min_x)
448 min_x = x;
449 if (x > max_x)
450 max_x = x;
451 if (y < min_y)
452 min_y = y;
453 if (y > max_y)
454 max_y = y;
455 }
456
Pekka Paalanen219b9822012-02-08 15:38:37 +0200457 int_x = floorf(min_x);
458 int_y = floorf(min_y);
459 pixman_region32_init_rect(bbox, int_x, int_y,
460 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200461}
462
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200463static void
464weston_surface_update_transform_disable(struct weston_surface *surface)
465{
466 surface->transform.enabled = 0;
467
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200468 /* round off fractions when not transformed */
469 surface->geometry.x = roundf(surface->geometry.x);
470 surface->geometry.y = roundf(surface->geometry.y);
471
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200472 pixman_region32_init_rect(&surface->transform.boundingbox,
473 surface->geometry.x,
474 surface->geometry.y,
475 surface->geometry.width,
476 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500477
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400478 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500479 pixman_region32_copy(&surface->transform.opaque,
480 &surface->opaque);
481 pixman_region32_translate(&surface->transform.opaque,
482 surface->geometry.x,
483 surface->geometry.y);
484 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200485}
486
487static int
488weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200489{
490 struct weston_matrix *matrix = &surface->transform.matrix;
491 struct weston_matrix *inverse = &surface->transform.inverse;
492 struct weston_transform *tform;
493
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200494 surface->transform.enabled = 1;
495
496 /* Otherwise identity matrix, but with x and y translation. */
497 surface->transform.position.matrix.d[12] = surface->geometry.x;
498 surface->transform.position.matrix.d[13] = surface->geometry.y;
499
500 weston_matrix_init(matrix);
501 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
502 weston_matrix_multiply(matrix, &tform->matrix);
503
504 if (weston_matrix_invert(inverse, matrix) < 0) {
505 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200506 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200507 " transformation not invertible.\n", surface);
508 return -1;
509 }
510
511 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
512 surface->geometry.height,
513 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500514
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200515 return 0;
516}
517
518WL_EXPORT void
519weston_surface_update_transform(struct weston_surface *surface)
520{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200521 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200522 return;
523
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200524 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200525
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500526 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200527
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200528 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500529 pixman_region32_fini(&surface->transform.opaque);
530 pixman_region32_init(&surface->transform.opaque);
531
532 if (region_is_undefined(&surface->input))
533 pixman_region32_init_rect(&surface->input, 0, 0,
534 surface->geometry.width,
535 surface->geometry.height);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200536
Pekka Paalanencd403622012-01-25 13:37:39 +0200537 /* transform.position is always in transformation_list */
538 if (surface->geometry.transformation_list.next ==
539 &surface->transform.position.link &&
540 surface->geometry.transformation_list.prev ==
541 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200542 weston_surface_update_transform_disable(surface);
543 } else {
544 if (weston_surface_update_transform_enable(surface) < 0)
545 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200546 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200547
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400548 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200549
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300550 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200551}
552
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200553WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100554weston_surface_to_global_fixed(struct weston_surface *surface,
555 wl_fixed_t sx, wl_fixed_t sy,
556 wl_fixed_t *x, wl_fixed_t *y)
557{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200558 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100559
560 weston_surface_to_global_float(surface,
561 wl_fixed_to_double(sx),
562 wl_fixed_to_double(sy),
563 &xf, &yf);
564 *x = wl_fixed_from_double(xf);
565 *y = wl_fixed_from_double(yf);
566}
567
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400568WL_EXPORT void
569weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200570 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200571{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200572 if (surface->transform.enabled) {
573 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
574
575 weston_matrix_transform(&surface->transform.inverse, &v);
576
577 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200578 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200579 "weston_surface_from_global(), divisor = %g\n",
580 v.f[3]);
581 *sx = 0;
582 *sy = 0;
583 return;
584 }
585
Pekka Paalanencd403622012-01-25 13:37:39 +0200586 *sx = v.f[0] / v.f[3];
587 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200588 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200589 *sx = x - surface->geometry.x;
590 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200591 }
592}
593
594WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100595weston_surface_from_global_fixed(struct weston_surface *surface,
596 wl_fixed_t x, wl_fixed_t y,
597 wl_fixed_t *sx, wl_fixed_t *sy)
598{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200599 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100600
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400601 weston_surface_from_global_float(surface,
602 wl_fixed_to_double(x),
603 wl_fixed_to_double(y),
604 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100605 *sx = wl_fixed_from_double(sxf);
606 *sy = wl_fixed_from_double(syf);
607}
608
609WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200610weston_surface_from_global(struct weston_surface *surface,
611 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
612{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200613 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200614
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400615 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200616 *sx = floorf(sxf);
617 *sy = floorf(syf);
618}
619
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400620WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400621weston_surface_schedule_repaint(struct weston_surface *surface)
622{
623 struct weston_output *output;
624
625 wl_list_for_each(output, &surface->compositor->output_list, link)
626 if (surface->output_mask & (1 << output->id))
627 weston_output_schedule_repaint(output);
628}
629
630WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500631weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500632{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400633 pixman_region32_union_rect(&surface->damage, &surface->damage,
634 0, 0, surface->geometry.width,
635 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200636
Kristian Høgsberg98238702012-08-03 16:29:12 -0400637 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500638}
639
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400640WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500641weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200642 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400643{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200644 surface->geometry.x = x;
645 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200646 surface->geometry.width = width;
647 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200648 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400649}
650
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200651WL_EXPORT void
652weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200653 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200654{
655 surface->geometry.x = x;
656 surface->geometry.y = y;
657 surface->geometry.dirty = 1;
658}
659
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300660WL_EXPORT int
661weston_surface_is_mapped(struct weston_surface *surface)
662{
663 if (surface->output)
664 return 1;
665 else
666 return 0;
667}
668
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400669WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500670weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500671{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400672 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500673
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400674 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500675
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400676 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500677}
678
Tiago Vignatti9d393522012-02-10 16:26:19 +0200679static struct weston_surface *
680weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100681 wl_fixed_t x, wl_fixed_t y,
682 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200683{
684 struct weston_surface *surface;
685
686 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100687 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500688 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100689 wl_fixed_to_int(*sx),
690 wl_fixed_to_int(*sy),
691 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200692 return surface;
693 }
694
695 return NULL;
696}
697
698static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400699weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500700{
Scott Moreau447013d2012-02-18 05:05:29 -0700701 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500702 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400703 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500704
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400705 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100706 return;
707
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400708 surface = weston_compositor_pick_surface(seat->compositor,
709 pointer->x,
710 pointer->y,
711 &pointer->current_x,
712 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500713
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400714 if (&surface->surface != pointer->current) {
715 interface = pointer->grab->interface;
716 pointer->current = &surface->surface;
717 interface->focus(pointer->grab, &surface->surface,
718 pointer->current_x,
719 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500720 }
721
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400722 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500723 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100724 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400725 pointer->x,
726 pointer->y,
727 &pointer->grab->x,
728 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500729}
730
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400731static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500732weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400733{
Daniel Stone37816df2012-05-16 18:45:18 +0100734 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400735
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500736 if (!compositor->focus)
737 return;
738
Daniel Stone37816df2012-05-16 18:45:18 +0100739 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400740 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400741}
742
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400743WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500744weston_surface_unmap(struct weston_surface *surface)
745{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100746 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500747
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500748 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500749 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500750 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500751
Daniel Stone4dab5db2012-05-30 16:31:53 +0100752 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100753 if (seat->seat.keyboard &&
754 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100755 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100756 if (seat->seat.pointer &&
757 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100758 wl_pointer_set_focus(seat->seat.pointer,
759 NULL,
760 wl_fixed_from_int(0),
761 wl_fixed_from_int(0));
762 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500763
Kristian Høgsberg98238702012-08-03 16:29:12 -0400764 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500765}
766
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400767struct weston_frame_callback {
768 struct wl_resource resource;
769 struct wl_list link;
770};
771
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500772static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400773destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500774{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500775 struct weston_surface *surface =
776 container_of(resource,
777 struct weston_surface, surface.resource);
778 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400779 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400780
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300781 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500782 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400783
Pekka Paalanen8e159182012-10-10 12:49:25 +0300784 pixman_region32_fini(&surface->pending.damage);
785
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300786 if (surface->pending.buffer)
787 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
788
Benjamin Franzke06286262011-05-06 19:12:33 +0200789 if (surface->buffer)
790 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400791
Kristian Høgsberg42263852012-09-06 21:59:29 -0400792 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200793
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200794 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200795 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500796 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500797 pixman_region32_fini(&surface->clip);
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500798 if (!region_is_undefined(&surface->input))
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500799 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200800
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400801 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
802 wl_resource_destroy(&cb->resource);
803
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400804 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500805}
806
Alex Wu8811bf92012-02-28 18:07:54 +0800807WL_EXPORT void
808weston_surface_destroy(struct weston_surface *surface)
809{
810 /* Not a valid way to destroy a client surface */
811 assert(surface->surface.resource.client == NULL);
812
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400813 wl_signal_emit(&surface->surface.resource.destroy_signal,
814 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800815 destroy_surface(&surface->surface.resource);
816}
817
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100818static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300819weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100820{
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300821 if (surface->buffer) {
822 weston_buffer_post_release(surface->buffer);
823 wl_list_remove(&surface->buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300824 }
825
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400826 if (buffer) {
827 buffer->busy_count++;
828 wl_signal_add(&buffer->resource.destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300829 &surface->buffer_destroy_listener);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300830
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300831 if (surface->geometry.width != buffer->width ||
832 surface->geometry.height != buffer->height) {
833 undef_region(&surface->input);
834 pixman_region32_fini(&surface->opaque);
835 pixman_region32_init(&surface->opaque);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400836 }
837 } else {
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300838 if (weston_surface_is_mapped(surface))
839 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300840 }
841
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300842 surface->compositor->renderer->attach(surface, buffer);
843 surface->buffer = buffer;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100844}
845
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500846WL_EXPORT void
847weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400848{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500849 wl_list_remove(&surface->layer_link);
850 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500851 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500852 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400853}
854
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400855WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500856weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400857{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500858 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400859
860 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500861 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400862}
863
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500864WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500865weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +0200866{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400867 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200868 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200869
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400870 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -0500871 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200872}
873
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400874WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500875weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400876{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500877 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400878
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400879 pixman_region32_union(&compositor->primary_plane.damage,
880 &compositor->primary_plane.damage,
881 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400882 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400883}
884
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400885static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500886fade_frame(struct weston_animation *animation,
887 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400888{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500889 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400890 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500891 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500892 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400893
Scott Moreau94b0b0c2012-06-14 01:01:56 -0600894 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -0600895 compositor->fade.spring.timestamp = msecs;
896
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500897 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500898 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500899 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
900 compositor->fade.spring.current);
901 weston_surface_damage(surface);
902
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500903 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400904 compositor->fade.spring.current =
905 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400906 wl_list_remove(&animation->link);
907 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200908
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500909 if (compositor->fade.spring.current < 0.001) {
910 destroy_surface(&surface->surface.resource);
911 compositor->fade.surface = NULL;
912 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500913 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400914 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200915 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400916 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400917}
918
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400919static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400920surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400921 pixman_region32_t *opaque)
922{
923 if (surface->buffer && wl_buffer_is_shm(surface->buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400924 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400925
926 if (surface->transform.enabled) {
927 pixman_box32_t *extents;
928
929 extents = pixman_region32_extents(&surface->damage);
930 surface_compute_bbox(surface, extents->x1, extents->y1,
931 extents->x2 - extents->x1,
932 extents->y2 - extents->y1,
933 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400934 pixman_region32_translate(&surface->damage,
935 -surface->plane->x,
936 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400937 } else {
938 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400939 surface->geometry.x - surface->plane->x,
940 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400941 }
942
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400943 pixman_region32_union(&surface->plane->damage,
944 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400945 empty_region(&surface->damage);
946 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400947 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400948}
949
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400950static void
Rob Bradford0fb79752012-08-02 15:36:57 +0100951weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400952{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500953 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500954 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500955 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -0500956 struct weston_animation *animation, *next;
957 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +0200958 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300959 pixman_region32_t opaque, output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500960
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +0200961 weston_compositor_update_drag_surfaces(ec);
962
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500963 /* Rebuild the surface list and update surface transforms up front. */
964 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +0200965 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500966 wl_list_for_each(layer, &ec->layer_list, link) {
967 wl_list_for_each(es, &layer->surface_list, layer_link) {
968 weston_surface_update_transform(es);
969 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +0200970 if (es->output == output) {
971 wl_list_insert_list(&frame_callback_list,
972 &es->frame_callback_list);
973 wl_list_init(&es->frame_callback_list);
974 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500975 }
976 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +0200977
Kristian Høgsberg79af73e2012-08-03 15:45:23 -0400978 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800979 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -0400980 else
981 wl_list_for_each(es, &ec->surface_list, link)
982 weston_surface_move_to_plane(es, &ec->primary_plane);
983
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500984 pixman_region32_init(&opaque);
985
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400986 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400987 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500988
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400989 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -0400990
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400991 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400992 pixman_region32_intersect(&output_damage,
993 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300994 pixman_region32_subtract(&ec->primary_plane.damage,
995 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400996
Scott Moreauccbf29d2012-02-22 14:21:41 -0700997 if (output->dirty)
998 weston_output_update_matrix(output);
999
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001000 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001001
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001002 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001003
Kristian Høgsbergef044142011-06-21 15:02:12 -04001004 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001005
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001006 weston_compositor_repick(ec);
1007 wl_event_loop_dispatch(ec->input_loop, 0);
1008
Jonas Ådahldb773762012-06-13 00:01:21 +02001009 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001010 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001011 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001012 }
Jonas Ådahldb773762012-06-13 00:01:21 +02001013 wl_list_init(&frame_callback_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001014
Scott Moreaud64cf212012-06-08 19:40:54 -06001015 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001016 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001017 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001018 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001019}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001020
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001021static int
1022weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001023{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001024 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001025
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001026 wl_event_loop_dispatch(compositor->input_loop, 0);
1027
1028 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001029}
1030
1031WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001032weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001033{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001034 struct weston_compositor *compositor = output->compositor;
1035 struct wl_event_loop *loop =
1036 wl_display_get_event_loop(compositor->wl_display);
1037 int fd;
1038
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001039 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001040 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001041 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001042 return;
1043 }
1044
1045 output->repaint_scheduled = 0;
1046 if (compositor->input_loop_source)
1047 return;
1048
1049 fd = wl_event_loop_get_fd(compositor->input_loop);
1050 compositor->input_loop_source =
1051 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1052 weston_compositor_read_input, compositor);
1053}
1054
1055static void
1056idle_repaint(void *data)
1057{
1058 struct weston_output *output = data;
1059
1060 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001061}
1062
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001063WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001064weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1065{
1066 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001067 if (below != NULL)
1068 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001069}
1070
1071WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001072weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001073{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001074 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001075 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001076
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001077 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001078 return;
1079
Kristian Høgsbergef044142011-06-21 15:02:12 -04001080 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001081 output->repaint_needed = 1;
1082 if (output->repaint_scheduled)
1083 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001084
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001085 wl_event_loop_add_idle(loop, idle_repaint, output);
1086 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001087
1088 if (compositor->input_loop_source) {
1089 wl_event_source_remove(compositor->input_loop_source);
1090 compositor->input_loop_source = NULL;
1091 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001092}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001093
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001094WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001095weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1096{
1097 struct weston_output *output;
1098
1099 wl_list_for_each(output, &compositor->output_list, link)
1100 weston_output_schedule_repaint(output);
1101}
1102
1103WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001104weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001105{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001106 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001107 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001108
Scott Moreau9d1b1122012-06-08 19:40:53 -06001109 output = container_of(compositor->output_list.next,
1110 struct weston_output, link);
1111
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001112 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001113 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001114 return;
1115
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001116 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001117 surface = weston_surface_create(compositor);
1118 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001119 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001120 wl_list_insert(&compositor->fade_layer.surface_list,
1121 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03001122 weston_surface_update_transform(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001123 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001124 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001125 }
1126
1127 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001128 if (wl_list_empty(&compositor->fade.animation.link)) {
1129 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001130 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001131 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001132 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001133}
1134
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001135static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001136surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001137{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001138 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001139}
1140
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001141static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001142surface_attach(struct wl_client *client,
1143 struct wl_resource *resource,
1144 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1145{
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001146 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001147 struct wl_buffer *buffer = NULL;
1148
1149 if (buffer_resource)
1150 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001151
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001152 if (surface->pending.buffer)
1153 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001154
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001155 surface->pending.sx = sx;
1156 surface->pending.sy = sy;
1157 surface->pending.buffer = buffer;
1158 if (buffer) {
1159 wl_signal_add(&buffer->resource.destroy_signal,
1160 &surface->pending.buffer_destroy_listener);
1161 surface->pending.remove_contents = 0;
1162 } else {
1163 surface->pending.remove_contents = 1;
1164 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001165}
1166
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001167static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001168surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001169 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001170 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001171{
Pekka Paalanen8e159182012-10-10 12:49:25 +03001172 struct weston_surface *surface = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001173
Pekka Paalanen8e159182012-10-10 12:49:25 +03001174 pixman_region32_union_rect(&surface->pending.damage,
1175 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001176 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001177}
1178
Kristian Høgsberg33418202011-08-16 23:01:28 -04001179static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001180destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001181{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001182 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001183
1184 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001185 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001186}
1187
1188static void
1189surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001190 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001191{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001192 struct weston_frame_callback *cb;
1193 struct weston_surface *es = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001194
1195 cb = malloc(sizeof *cb);
1196 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001197 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001198 return;
1199 }
1200
1201 cb->resource.object.interface = &wl_callback_interface;
1202 cb->resource.object.id = callback;
1203 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001204 cb->resource.client = client;
1205 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001206
1207 wl_client_add_resource(client, &cb->resource);
Jonas Ådahldb773762012-06-13 00:01:21 +02001208 wl_list_insert(es->frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001209}
1210
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001211static void
1212surface_set_opaque_region(struct wl_client *client,
1213 struct wl_resource *resource,
1214 struct wl_resource *region_resource)
1215{
1216 struct weston_surface *surface = resource->data;
1217 struct weston_region *region;
1218
1219 pixman_region32_fini(&surface->opaque);
1220
1221 if (region_resource) {
1222 region = region_resource->data;
1223 pixman_region32_init_rect(&surface->opaque, 0, 0,
1224 surface->geometry.width,
1225 surface->geometry.height);
1226 pixman_region32_intersect(&surface->opaque,
1227 &surface->opaque, &region->region);
1228 } else {
1229 pixman_region32_init(&surface->opaque);
1230 }
1231
1232 surface->geometry.dirty = 1;
1233}
1234
1235static void
1236surface_set_input_region(struct wl_client *client,
1237 struct wl_resource *resource,
1238 struct wl_resource *region_resource)
1239{
1240 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001241 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001242
1243 if (region_resource) {
1244 region = region_resource->data;
1245 pixman_region32_init_rect(&surface->input, 0, 0,
1246 surface->geometry.width,
1247 surface->geometry.height);
1248 pixman_region32_intersect(&surface->input,
1249 &surface->input, &region->region);
1250 } else {
1251 pixman_region32_init_rect(&surface->input, 0, 0,
1252 surface->geometry.width,
1253 surface->geometry.height);
1254 }
1255
Kristian Høgsberg98238702012-08-03 16:29:12 -04001256 weston_surface_schedule_repaint(surface);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001257}
1258
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001259static void
1260surface_commit(struct wl_client *client, struct wl_resource *resource)
1261{
1262 struct weston_surface *surface = resource->data;
1263
1264 /* wl_surface.attach */
1265 if (surface->pending.buffer || surface->pending.remove_contents)
1266 weston_surface_attach(surface, surface->pending.buffer);
1267
1268 if (surface->buffer && surface->configure)
1269 surface->configure(surface, surface->pending.sx,
1270 surface->pending.sy);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001271
1272 /* wl_surface.damage */
1273 pixman_region32_union(&surface->damage, &surface->damage,
1274 &surface->pending.damage);
1275 empty_region(&surface->pending.damage);
1276 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001277}
1278
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001279static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001280 surface_destroy,
1281 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001282 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001283 surface_frame,
1284 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001285 surface_set_input_region,
1286 surface_commit
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001287};
1288
1289static void
1290compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001291 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001292{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001293 struct weston_compositor *ec = resource->data;
1294 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001295
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001296 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001297 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001298 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001299 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001300 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001301
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001302 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001303
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001304 surface->surface.resource.object.id = id;
1305 surface->surface.resource.object.interface = &wl_surface_interface;
1306 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001307 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001308 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001309
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001310 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001311}
1312
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001313static void
1314destroy_region(struct wl_resource *resource)
1315{
1316 struct weston_region *region =
1317 container_of(resource, struct weston_region, resource);
1318
1319 pixman_region32_fini(&region->region);
1320 free(region);
1321}
1322
1323static void
1324region_destroy(struct wl_client *client, struct wl_resource *resource)
1325{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001326 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001327}
1328
1329static void
1330region_add(struct wl_client *client, struct wl_resource *resource,
1331 int32_t x, int32_t y, int32_t width, int32_t height)
1332{
1333 struct weston_region *region = resource->data;
1334
1335 pixman_region32_union_rect(&region->region, &region->region,
1336 x, y, width, height);
1337}
1338
1339static void
1340region_subtract(struct wl_client *client, struct wl_resource *resource,
1341 int32_t x, int32_t y, int32_t width, int32_t height)
1342{
1343 struct weston_region *region = resource->data;
1344 pixman_region32_t rect;
1345
1346 pixman_region32_init_rect(&rect, x, y, width, height);
1347 pixman_region32_subtract(&region->region, &region->region, &rect);
1348 pixman_region32_fini(&rect);
1349}
1350
1351static const struct wl_region_interface region_interface = {
1352 region_destroy,
1353 region_add,
1354 region_subtract
1355};
1356
1357static void
1358compositor_create_region(struct wl_client *client,
1359 struct wl_resource *resource, uint32_t id)
1360{
1361 struct weston_region *region;
1362
1363 region = malloc(sizeof *region);
1364 if (region == NULL) {
1365 wl_resource_post_no_memory(resource);
1366 return;
1367 }
1368
1369 region->resource.destroy = destroy_region;
1370
1371 region->resource.object.id = id;
1372 region->resource.object.interface = &wl_region_interface;
1373 region->resource.object.implementation =
1374 (void (**)(void)) &region_interface;
1375 region->resource.data = region;
1376
1377 pixman_region32_init(&region->region);
1378
1379 wl_client_add_resource(client, &region->resource);
1380}
1381
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001382static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001383 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001384 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001385};
1386
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001387WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001388weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001389{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001390 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1391 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001392
1393 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001394 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001395}
1396
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001397static void
1398weston_compositor_dpms_on(struct weston_compositor *compositor)
1399{
1400 struct weston_output *output;
1401
1402 wl_list_for_each(output, &compositor->output_list, link)
1403 if (output->set_dpms)
1404 output->set_dpms(output, WESTON_DPMS_ON);
1405}
1406
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001407WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001408weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001409{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001410 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1411 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001412 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001413 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001414 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001415 }
1416}
1417
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001418static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001419weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001420{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001421 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001422 compositor->idle_inhibit++;
1423}
1424
1425static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001426weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001427{
1428 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001429 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001430}
1431
1432static int
1433idle_handler(void *data)
1434{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001435 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001436
1437 if (compositor->idle_inhibit)
1438 return 1;
1439
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001440 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001441
1442 return 1;
1443}
1444
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001445WL_EXPORT void
1446weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1447{
1448 pixman_region32_init(&plane->damage);
1449 plane->x = x;
1450 plane->y = y;
1451}
1452
1453WL_EXPORT void
1454weston_plane_release(struct weston_plane *plane)
1455{
1456 pixman_region32_fini(&plane->damage);
1457}
1458
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001459static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001460weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001461
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001462static void
Daniel Stone37816df2012-05-16 18:45:18 +01001463clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001464{
Daniel Stone37816df2012-05-16 18:45:18 +01001465 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001466 struct weston_output *output, *prev = NULL;
1467 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001468
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001469 x = wl_fixed_to_int(*fx);
1470 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001471 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1472 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001473
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001474 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001475 if (pixman_region32_contains_point(&output->region,
1476 x, y, NULL))
1477 valid = 1;
1478 if (pixman_region32_contains_point(&output->region,
1479 old_x, old_y, NULL))
1480 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001481 }
Daniel Stone37816df2012-05-16 18:45:18 +01001482
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001483 if (!valid) {
1484 if (x < prev->x)
1485 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001486 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001487 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001488 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001489 if (y < prev->y)
1490 *fy = wl_fixed_from_int(prev->y);
1491 else if (y >= prev->y + prev->current->height)
1492 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001493 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001494 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001495}
1496
1497WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001498notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001499{
1500 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001501 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001502 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001503 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001504 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001505
1506 weston_compositor_activity(ec);
1507
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001508 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001509
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001510 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001511
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001512 pointer->x = x;
1513 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001514
1515 ix = wl_fixed_to_int(x);
1516 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001517
Scott Moreauccbf29d2012-02-22 14:21:41 -07001518 wl_list_for_each(output, &ec->output_list, link)
1519 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001520 pixman_region32_contains_point(&output->region,
1521 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06001522 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001523
Daniel Stone37816df2012-05-16 18:45:18 +01001524 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001525 interface = pointer->grab->interface;
1526 interface->motion(pointer->grab, time,
1527 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001528
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001529 if (seat->sprite) {
1530 weston_surface_set_position(seat->sprite,
1531 ix - seat->hotspot_x,
1532 iy - seat->hotspot_y);
1533 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001534 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001535}
1536
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001537WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001538weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001539 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001540{
Daniel Stone37816df2012-05-16 18:45:18 +01001541 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001542
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001543 if (seat->seat.keyboard) {
1544 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1545 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04001546 }
1547
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001548 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001549}
1550
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001551WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001552notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001553 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001554{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001555 struct weston_compositor *compositor = seat->compositor;
1556 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001557 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001558 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001559 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001560
Daniel Stone4dbadb12012-05-30 16:31:51 +01001561 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001562 if (compositor->ping_handler && focus)
1563 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001564 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001565 if (pointer->button_count == 0) {
1566 pointer->grab_button = button;
1567 pointer->grab_time = time;
1568 pointer->grab_x = pointer->x;
1569 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001570 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001571 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001572 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001573 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001574 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001575 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001576
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001577 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001578 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001579
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001580 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001581
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001582 if (pointer->button_count == 1)
1583 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001584 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001585}
1586
Scott Moreau210d0792012-03-22 10:47:01 -06001587WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001588notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01001589 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001590{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001591 struct weston_compositor *compositor = seat->compositor;
1592 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001593 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001594 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001595 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1596
1597 if (compositor->ping_handler && focus)
1598 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001599
1600 weston_compositor_activity(compositor);
1601
Scott Moreau6a3633d2012-03-20 08:47:59 -06001602 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001603 weston_compositor_run_axis_binding(compositor, seat,
1604 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06001605 else
1606 return;
1607
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001608 if (pointer->focus_resource)
1609 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01001610 value);
Scott Moreau210d0792012-03-22 10:47:01 -06001611}
1612
Daniel Stone05d58682012-06-22 13:21:38 +01001613WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001614notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001615{
Daniel Stone05d58682012-06-22 13:21:38 +01001616 struct wl_keyboard *keyboard = &seat->keyboard;
1617 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01001618 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001619 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01001620 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01001621 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01001622
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001623 /* Serialize and update our internal state, checking to see if it's
1624 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01001625 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1626 XKB_STATE_DEPRESSED);
1627 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1628 XKB_STATE_LATCHED);
1629 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1630 XKB_STATE_LOCKED);
1631 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01001632 XKB_STATE_EFFECTIVE);
1633
Daniel Stone71c38772012-06-22 13:21:30 +01001634 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
1635 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
1636 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
1637 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01001638 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01001639
Daniel Stone71c38772012-06-22 13:21:30 +01001640 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
1641 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
1642 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
1643 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001644
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001645 /* And update the modifier_state for bindings. */
1646 mods_lookup = mods_depressed | mods_latched;
1647 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01001648 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001649 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01001650 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001651 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01001652 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001653 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04001654 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
1655 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01001656
Daniel Stone8c8164f2012-05-30 16:31:45 +01001657 /* Finally, notify the compositor that LEDs have changed. */
1658 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001659 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001660 leds |= LED_NUM_LOCK;
1661 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001662 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001663 leds |= LED_CAPS_LOCK;
1664 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001665 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001666 leds |= LED_SCROLL_LOCK;
1667 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01001668 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01001669 seat->xkb_state.leds = leds;
1670
Daniel Stone05d58682012-06-22 13:21:38 +01001671 if (changed) {
1672 grab->interface->modifiers(grab,
1673 serial,
1674 keyboard->modifiers.mods_depressed,
1675 keyboard->modifiers.mods_latched,
1676 keyboard->modifiers.mods_locked,
1677 keyboard->modifiers.group);
1678 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001679}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001680
Daniel Stone05d58682012-06-22 13:21:38 +01001681static void
1682update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001683 enum wl_keyboard_key_state state)
1684{
1685 enum xkb_key_direction direction;
1686
1687 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1688 direction = XKB_KEY_DOWN;
1689 else
1690 direction = XKB_KEY_UP;
1691
1692 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1693 * broken keycode system, which starts at 8. */
1694 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1695
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001696 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001697}
1698
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001699WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001700notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001701 enum wl_keyboard_key_state state,
1702 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001703{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001704 struct weston_compositor *compositor = seat->compositor;
1705 struct wl_keyboard *keyboard = seat->seat.keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01001706 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001707 (struct weston_surface *) keyboard->focus;
1708 struct wl_keyboard_grab *grab = keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001709 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001710 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001711
Daniel Stonec9785ea2012-05-30 16:31:52 +01001712 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001713 if (compositor->ping_handler && focus)
1714 compositor->ping_handler(focus, serial);
1715
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001716 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001717 keyboard->grab_key = key;
1718 keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07001719 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001720 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001721 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001722
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001723 end = keyboard->keys.data + keyboard->keys.size;
1724 for (k = keyboard->keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01001725 if (*k == key) {
1726 /* Ignore server-generated repeats. */
1727 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1728 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001729 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01001730 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001731 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001732 keyboard->keys.size = (void *) end - keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001733 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001734 k = wl_array_add(&keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001735 *k = key;
1736 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001737
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001738 if (grab == &keyboard->default_grab) {
1739 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001740 state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001741 grab = keyboard->grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06001742 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001743
Daniel Stone7ace3902012-05-30 16:31:40 +01001744 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01001745
1746 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001747 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01001748 wl_display_get_serial(compositor->wl_display),
1749 key,
1750 state);
1751 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001752}
1753
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001754WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001755notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01001756 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001757{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001758 struct weston_compositor *compositor = seat->compositor;
1759 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001760
1761 if (output) {
Daniel Stone37816df2012-05-16 18:45:18 +01001762 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001763 x - pointer->x,
1764 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001765
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001766 pointer->x = x;
1767 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001768 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001769 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001770 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001771 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03001772 /* FIXME: We should call wl_pointer_set_focus(seat,
1773 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001774 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001775}
1776
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001777static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001778destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001779{
Daniel Stone37816df2012-05-16 18:45:18 +01001780 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001781
Daniel Stone37816df2012-05-16 18:45:18 +01001782 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001783 saved_kbd_focus_listener);
1784
Daniel Stone37816df2012-05-16 18:45:18 +01001785 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001786}
1787
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001788WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001789notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001790 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001791{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001792 struct weston_compositor *compositor = seat->compositor;
1793 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001794 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001795 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001796
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001797 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001798 wl_array_copy(&keyboard->keys, keys);
1799 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001800 weston_compositor_idle_inhibit(compositor);
1801 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001802 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001803 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001804 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001805
Daniel Stoneef172672012-06-22 13:21:32 +01001806 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001807 wl_array_for_each(k, &keyboard->keys) {
1808 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01001809 WL_KEYBOARD_KEY_STATE_PRESSED);
1810 }
1811
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001812 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001813
1814 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001815 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1816 wl_keyboard_set_focus(keyboard, surface);
1817 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001818 }
1819}
1820
1821WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001822notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01001823{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001824 struct weston_compositor *compositor = seat->compositor;
1825 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001826 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001827
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001828 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001829 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001830 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001831 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001832 WL_KEYBOARD_KEY_STATE_RELEASED);
1833 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001834
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001835 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001836
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001837 if (keyboard->focus) {
1838 seat->saved_kbd_focus = keyboard->focus;
1839 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01001840 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001841 wl_signal_add(&keyboard->focus->resource.destroy_signal,
1842 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001843 }
1844
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001845 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001846 /* FIXME: We really need keyboard grab cancel here to
1847 * let the grab shut down properly. As it is we leak
1848 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001849 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001850}
1851
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001852static void
Daniel Stone37816df2012-05-16 18:45:18 +01001853touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001854{
Daniel Stone37816df2012-05-16 18:45:18 +01001855 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001856 struct wl_resource *resource;
1857
Pekka Paalanen56464252012-07-31 13:21:08 +03001858 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001859 return;
1860
Pekka Paalanen56464252012-07-31 13:21:08 +03001861 if (seat->touch->focus_resource)
1862 wl_list_remove(&seat->touch->focus_listener.link);
1863 seat->touch->focus = NULL;
1864 seat->touch->focus_resource = NULL;
1865
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001866 if (surface) {
1867 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01001868 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04001869 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001870 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02001871 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001872 return;
1873 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001874
Daniel Stone37816df2012-05-16 18:45:18 +01001875 seat->touch->focus = surface;
1876 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03001877 wl_signal_add(&resource->destroy_signal,
1878 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001879 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001880}
1881
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001882/**
1883 * notify_touch - emulates button touches and notifies surfaces accordingly.
1884 *
1885 * It assumes always the correct cycle sequence until it gets here: touch_down
1886 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1887 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001888 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001889 */
1890WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001891notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001892 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001893{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001894 struct weston_compositor *ec = seat->compositor;
1895 struct wl_touch *touch = seat->seat.touch;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001896 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001897 wl_fixed_t sx, sy;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001898 uint32_t serial = 0;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001899
1900 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01001901 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001902 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001903
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001904 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001905
1906 /* the first finger down picks the surface, and all further go
1907 * to that surface for the remainder of the touch session i.e.
1908 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001909 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001910 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001911 touch_set_focus(seat, &es->surface);
1912 } else if (touch->focus) {
1913 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001914 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001915 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001916
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001917 if (touch->focus_resource && touch->focus)
1918 wl_touch_send_down(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001919 serial, time,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001920 &touch->focus->resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001921 touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001922 break;
Daniel Stone37816df2012-05-16 18:45:18 +01001923 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001924 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001925 if (!es)
1926 break;
1927
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001928 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001929 if (touch->focus_resource)
1930 wl_touch_send_motion(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001931 time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001932 break;
Daniel Stone37816df2012-05-16 18:45:18 +01001933 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001934 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001935 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001936
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001937 if (touch->focus_resource)
1938 wl_touch_send_up(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001939 serial, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001940 if (seat->num_tp == 0)
1941 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001942 break;
1943 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001944}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001945
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001946static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001947pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1948{
1949 struct weston_seat *seat = container_of(listener, struct weston_seat,
1950 sprite_destroy_listener);
1951
1952 seat->sprite = NULL;
1953}
1954
1955static void
1956pointer_cursor_surface_configure(struct weston_surface *es,
1957 int32_t dx, int32_t dy)
1958{
1959 struct weston_seat *seat = es->private;
1960 int x, y;
1961
1962 assert(es == seat->sprite);
1963
1964 seat->hotspot_x -= dx;
1965 seat->hotspot_y -= dy;
1966
1967 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
1968 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
1969
1970 weston_surface_configure(seat->sprite, x, y,
1971 es->buffer->width, es->buffer->height);
1972
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03001973 empty_region(&es->input);
1974
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001975 if (!weston_surface_is_mapped(es)) {
1976 wl_list_insert(&es->compositor->cursor_layer.surface_list,
1977 &es->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03001978 weston_surface_update_transform(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001979 }
1980}
1981
1982static void
1983pointer_unmap_sprite(struct weston_seat *seat)
1984{
1985 if (weston_surface_is_mapped(seat->sprite))
1986 weston_surface_unmap(seat->sprite);
1987
1988 wl_list_remove(&seat->sprite_destroy_listener.link);
1989 seat->sprite->configure = NULL;
1990 seat->sprite->private = NULL;
1991 seat->sprite = NULL;
1992}
1993
1994static void
1995pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1996 uint32_t serial, struct wl_resource *surface_resource,
1997 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001998{
Daniel Stone37816df2012-05-16 18:45:18 +01001999 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002000 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002001
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002002 if (surface_resource)
2003 surface = container_of(surface_resource->data,
2004 struct weston_surface, surface);
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002005
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04002006 if (seat->seat.pointer->focus == NULL)
2007 return;
2008 if (seat->seat.pointer->focus->resource.client != client)
2009 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002010 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002011 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002012
2013 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002014 if (surface->configure) {
2015 wl_resource_post_error(&surface->surface.resource,
2016 WL_DISPLAY_ERROR_INVALID_OBJECT,
2017 "surface->configure already "
2018 "set");
2019 return;
2020 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002021 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002022
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002023 if (seat->sprite)
2024 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002025
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002026 if (!surface)
2027 return;
2028
2029 wl_signal_add(&surface->surface.resource.destroy_signal,
2030 &seat->sprite_destroy_listener);
2031
2032 surface->configure = pointer_cursor_surface_configure;
2033 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002034 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002035 seat->hotspot_x = x;
2036 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002037
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04002038 if (surface->buffer)
2039 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002040}
2041
Daniel Stone37816df2012-05-16 18:45:18 +01002042static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002043 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002044};
2045
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002046static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002047handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002048{
Daniel Stone37816df2012-05-16 18:45:18 +01002049 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002050
Daniel Stone37816df2012-05-16 18:45:18 +01002051 seat = container_of(listener, struct weston_seat,
2052 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002053
Daniel Stone37816df2012-05-16 18:45:18 +01002054 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002055}
2056
Casey Dahlin9074db52012-04-19 22:50:09 -04002057static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002058{
2059 wl_list_remove(&resource->link);
2060 free(resource);
2061}
2062
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002063static void
Daniel Stone37816df2012-05-16 18:45:18 +01002064seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2065 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002066{
Daniel Stone37816df2012-05-16 18:45:18 +01002067 struct weston_seat *seat = resource->data;
2068 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002069
Daniel Stone37816df2012-05-16 18:45:18 +01002070 if (!seat->seat.pointer)
2071 return;
2072
2073 cr = wl_client_add_object(client, &wl_pointer_interface,
2074 &pointer_interface, id, seat);
2075 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002076 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002077
2078 if (seat->seat.pointer->focus &&
2079 seat->seat.pointer->focus->resource.client == client) {
2080 struct weston_surface *surface;
2081 wl_fixed_t sx, sy;
2082
2083 surface = (struct weston_surface *) seat->seat.pointer->focus;
2084 weston_surface_from_global_fixed(surface,
2085 seat->seat.pointer->x,
2086 seat->seat.pointer->y,
2087 &sx,
2088 &sy);
2089 wl_pointer_set_focus(seat->seat.pointer,
2090 seat->seat.pointer->focus,
2091 sx,
2092 sy);
2093 }
Daniel Stone37816df2012-05-16 18:45:18 +01002094}
2095
2096static void
2097seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2098 uint32_t id)
2099{
2100 struct weston_seat *seat = resource->data;
2101 struct wl_resource *cr;
2102
2103 if (!seat->seat.keyboard)
2104 return;
2105
2106 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2107 seat);
2108 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002109 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002110
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002111 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2112 seat->xkb_info.keymap_fd,
2113 seat->xkb_info.keymap_size);
2114
Daniel Stone17b13bd2012-05-30 16:31:39 +01002115 if (seat->seat.keyboard->focus &&
2116 seat->seat.keyboard->focus->resource.client == client) {
2117 wl_keyboard_set_focus(seat->seat.keyboard,
2118 seat->seat.keyboard->focus);
2119 }
Daniel Stone37816df2012-05-16 18:45:18 +01002120}
2121
2122static void
2123seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2124 uint32_t id)
2125{
2126 struct weston_seat *seat = resource->data;
2127 struct wl_resource *cr;
2128
2129 if (!seat->seat.touch)
2130 return;
2131
2132 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2133 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002134 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002135}
2136
2137static const struct wl_seat_interface seat_interface = {
2138 seat_get_pointer,
2139 seat_get_keyboard,
2140 seat_get_touch,
2141};
2142
2143static void
2144bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2145{
2146 struct wl_seat *seat = data;
2147 struct wl_resource *resource;
2148 enum wl_seat_capability caps = 0;
2149
2150 resource = wl_client_add_object(client, &wl_seat_interface,
2151 &seat_interface, id, data);
2152 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002153 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002154
2155 if (seat->pointer)
2156 caps |= WL_SEAT_CAPABILITY_POINTER;
2157 if (seat->keyboard)
2158 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2159 if (seat->touch)
2160 caps |= WL_SEAT_CAPABILITY_TOUCH;
2161
2162 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002163}
2164
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002165static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002166device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002167{
Daniel Stone37816df2012-05-16 18:45:18 +01002168 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002169
Daniel Stone37816df2012-05-16 18:45:18 +01002170 seat = container_of(listener, struct weston_seat,
2171 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002172
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002173 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002174}
2175
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002176static void weston_compositor_xkb_init(struct weston_compositor *ec,
2177 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002178{
Daniel Stonee379da92012-05-30 16:32:04 +01002179 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002180 ec->xkb_context = xkb_context_new(0);
2181 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002182 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002183 exit(1);
2184 }
Daniel Stone994679a2012-05-30 16:31:43 +01002185 }
2186
2187 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002188 ec->xkb_names = *names;
2189 if (!ec->xkb_names.rules)
2190 ec->xkb_names.rules = strdup("evdev");
2191 if (!ec->xkb_names.model)
2192 ec->xkb_names.model = strdup("pc105");
2193 if (!ec->xkb_names.layout)
2194 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002195}
2196
Daniel Stonee379da92012-05-30 16:32:04 +01002197static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2198{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002199 if (xkb_info->keymap)
2200 xkb_map_unref(xkb_info->keymap);
2201
2202 if (xkb_info->keymap_area)
2203 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2204 if (xkb_info->keymap_fd >= 0)
2205 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002206}
2207
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002208static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2209{
Daniel Stonee379da92012-05-30 16:32:04 +01002210 free((char *) ec->xkb_names.rules);
2211 free((char *) ec->xkb_names.model);
2212 free((char *) ec->xkb_names.layout);
2213 free((char *) ec->xkb_names.variant);
2214 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002215
Daniel Stonee379da92012-05-30 16:32:04 +01002216 xkb_info_destroy(&ec->xkb_info);
2217 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002218}
2219
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002220static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002221weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002222{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002223 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002224
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002225 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2226 XKB_MOD_NAME_SHIFT);
2227 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2228 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002229 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2230 XKB_MOD_NAME_CTRL);
2231 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2232 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002233 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2234 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002235 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2236 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002237 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002238
2239 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2240 XKB_LED_NAME_NUM);
2241 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2242 XKB_LED_NAME_CAPS);
2243 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2244 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002245
2246 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2247 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002248 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002249 exit(EXIT_FAILURE);
2250 }
2251 xkb_info->keymap_size = strlen(keymap_str) + 1;
2252
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002253 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002254 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002255 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002256 (unsigned long) xkb_info->keymap_size);
2257 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002258 }
2259
2260 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2261 PROT_READ | PROT_WRITE,
2262 MAP_SHARED, xkb_info->keymap_fd, 0);
2263 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002264 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002265 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002266 goto err_dev_zero;
2267 }
2268 strcpy(xkb_info->keymap_area, keymap_str);
2269 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002270
2271 return;
2272
2273err_dev_zero:
2274 close(xkb_info->keymap_fd);
2275 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002276err_keymap_str:
2277 free(keymap_str);
2278 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002279}
2280
2281static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002282weston_compositor_build_global_keymap(struct weston_compositor *ec)
2283{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002284 if (ec->xkb_info.keymap != NULL)
2285 return;
2286
Daniel Stonee379da92012-05-30 16:32:04 +01002287 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002288 &ec->xkb_names,
2289 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002290 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002291 weston_log("failed to compile global XKB keymap\n");
2292 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002293 "options %s",
2294 ec->xkb_names.rules, ec->xkb_names.model,
2295 ec->xkb_names.layout, ec->xkb_names.variant,
2296 ec->xkb_names.options);
2297 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002298 }
2299
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002300 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002301}
2302
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002303WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002304weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002305{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002306 if (seat->has_keyboard)
2307 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002308
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002309 if (keymap != NULL) {
2310 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002311 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002312 }
2313 else {
2314 weston_compositor_build_global_keymap(seat->compositor);
2315 seat->xkb_info = seat->compositor->xkb_info;
2316 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2317 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002318
2319 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2320 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002321 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01002322 exit(1);
2323 }
2324
Daniel Stone71c38772012-06-22 13:21:30 +01002325 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002326
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002327 wl_keyboard_init(&seat->keyboard);
2328 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2329
2330 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01002331}
2332
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002333WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002334weston_seat_init_pointer(struct weston_seat *seat)
2335{
2336 if (seat->has_pointer)
2337 return;
2338
2339 wl_pointer_init(&seat->pointer);
2340 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2341
2342 seat->has_pointer = 1;
2343}
2344
2345WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002346weston_seat_init_touch(struct weston_seat *seat)
2347{
2348 if (seat->has_touch)
2349 return;
2350
2351 wl_touch_init(&seat->touch);
2352 wl_seat_set_touch(&seat->seat, &seat->touch);
2353
2354 seat->has_touch = 1;
2355}
2356
2357WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002358weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002359{
Daniel Stone37816df2012-05-16 18:45:18 +01002360 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002361 seat->has_pointer = 0;
2362 seat->has_keyboard = 0;
2363 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002364
Daniel Stone37816df2012-05-16 18:45:18 +01002365 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2366 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002367
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002368 seat->sprite = NULL;
2369 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002370
Daniel Stone37816df2012-05-16 18:45:18 +01002371 seat->compositor = ec;
2372 seat->hotspot_x = 16;
2373 seat->hotspot_y = 16;
2374 seat->modifier_state = 0;
2375 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002376
Daniel Stone37816df2012-05-16 18:45:18 +01002377 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002378 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002379
Daniel Stone37816df2012-05-16 18:45:18 +01002380 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002381
Daniel Stone37816df2012-05-16 18:45:18 +01002382 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2383 wl_signal_add(&seat->seat.drag_icon_signal,
2384 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002385
2386 clipboard_create(seat);
Jan Arne Petersene829adc2012-08-10 16:47:22 +02002387 input_method_create(ec, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002388}
2389
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002390WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002391weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002392{
Daniel Stone37816df2012-05-16 18:45:18 +01002393 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002394 /* The global object is destroyed at wl_display_destroy() time. */
2395
Daniel Stone37816df2012-05-16 18:45:18 +01002396 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002397 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002398
Daniel Stone74419a22012-05-30 16:32:02 +01002399 if (seat->xkb_state.state != NULL)
2400 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01002401 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002402
Daniel Stone37816df2012-05-16 18:45:18 +01002403 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002404}
2405
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002406static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002407drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2408{
2409 weston_surface_configure(es,
2410 es->geometry.x + sx, es->geometry.y + sy,
2411 es->buffer->width, es->buffer->height);
2412}
2413
2414static int
Daniel Stone37816df2012-05-16 18:45:18 +01002415device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002416 struct weston_surface *surface)
2417{
Daniel Stone37816df2012-05-16 18:45:18 +01002418 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002419
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002420 if (surface->configure) {
2421 wl_resource_post_error(&surface->surface.resource,
2422 WL_DISPLAY_ERROR_INVALID_OBJECT,
2423 "surface->configure already set");
2424 return 0;
2425 }
2426
Daniel Stone37816df2012-05-16 18:45:18 +01002427 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002428
Daniel Stone37816df2012-05-16 18:45:18 +01002429 weston_surface_set_position(ws->drag_surface,
2430 wl_fixed_to_double(seat->pointer->x),
2431 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002432
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002433 surface->configure = drag_surface_configure;
2434
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002435 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002436 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002437
2438 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002439}
2440
2441static void
Daniel Stone37816df2012-05-16 18:45:18 +01002442device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002443{
Daniel Stone37816df2012-05-16 18:45:18 +01002444 seat->drag_surface->configure = NULL;
2445 undef_region(&seat->drag_surface->input);
2446 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2447 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002448}
2449
2450static void
Daniel Stone37816df2012-05-16 18:45:18 +01002451device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002452{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002453 struct wl_list *list;
2454
Daniel Stone37816df2012-05-16 18:45:18 +01002455 if (weston_surface_is_mapped(seat->drag_surface) ||
2456 !seat->drag_surface->buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002457 return;
2458
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002459 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2460 list = &seat->sprite->layer_link;
2461 else
2462 list = &seat->compositor->cursor_layer.surface_list;
2463
2464 wl_list_insert(list, &seat->drag_surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002465 weston_surface_update_transform(seat->drag_surface);
Daniel Stone37816df2012-05-16 18:45:18 +01002466 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002467}
2468
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002469static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002470weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01002471 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002472{
2473 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002474
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002475 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002476 return;
2477
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002478 if (seat->drag_surface && seat->seat.drag_surface &&
2479 (&seat->drag_surface->surface.resource !=
2480 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002481 /* between calls to this funcion we got a new drag_surface */
2482 surface_changed = 1;
2483
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002484 if (!seat->seat.drag_surface || surface_changed) {
2485 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002486 if (!surface_changed)
2487 return;
2488 }
2489
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002490 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002491 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002492 seat->seat.drag_surface;
2493 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002494 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002495 }
2496
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002497 /* the client may not have attached a buffer to the drag surface
2498 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002499 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002500
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02002501 /* the client may have attached a buffer with a different size to
2502 * the drag surface, causing the input region to be reset */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002503 if (region_is_undefined(&seat->drag_surface->input))
2504 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02002505
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002506 if (!dx && !dy)
2507 return;
2508
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002509 weston_surface_set_position(seat->drag_surface,
2510 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
2511 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002512}
2513
2514WL_EXPORT void
2515weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2516{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002517 struct weston_seat *seat;
2518
2519 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002520 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002521}
2522
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002523static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002524bind_output(struct wl_client *client,
2525 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002526{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002527 struct weston_output *output = data;
2528 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002529 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002530
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002531 resource = wl_client_add_object(client,
2532 &wl_output_interface, NULL, id, data);
2533
Casey Dahlin9074db52012-04-19 22:50:09 -04002534 wl_list_insert(&output->resource_list, &resource->link);
2535 resource->destroy = unbind_resource;
2536
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002537 wl_output_send_geometry(resource,
2538 output->x,
2539 output->y,
2540 output->mm_width,
2541 output->mm_height,
2542 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002543 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002544 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002545
2546 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002547 wl_output_send_mode(resource,
2548 mode->flags,
2549 mode->width,
2550 mode->height,
2551 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002552 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002553}
2554
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002555WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002556weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002557{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002558 struct weston_compositor *c = output->compositor;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002559 int i;
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002560
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002561 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002562
2563 for (i = 0; i < 2; i++)
2564 pixman_region32_fini(&output->buffer_damage[i]);
2565
Casey Dahlin9074db52012-04-19 22:50:09 -04002566 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002567
2568 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002569}
2570
Scott Moreau1bad5db2012-08-18 01:04:05 -06002571static void
2572weston_output_compute_transform(struct weston_output *output)
2573{
2574 struct weston_matrix transform;
2575 int flip;
2576
2577 weston_matrix_init(&transform);
2578
2579 switch(output->transform) {
2580 case WL_OUTPUT_TRANSFORM_FLIPPED:
2581 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2582 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2583 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2584 flip = -1;
2585 break;
2586 default:
2587 flip = 1;
2588 break;
2589 }
2590
2591 switch(output->transform) {
2592 case WL_OUTPUT_TRANSFORM_NORMAL:
2593 case WL_OUTPUT_TRANSFORM_FLIPPED:
2594 transform.d[0] = flip;
2595 transform.d[1] = 0;
2596 transform.d[4] = 0;
2597 transform.d[5] = 1;
2598 break;
2599 case WL_OUTPUT_TRANSFORM_90:
2600 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2601 transform.d[0] = 0;
2602 transform.d[1] = -flip;
2603 transform.d[4] = 1;
2604 transform.d[5] = 0;
2605 break;
2606 case WL_OUTPUT_TRANSFORM_180:
2607 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2608 transform.d[0] = -flip;
2609 transform.d[1] = 0;
2610 transform.d[4] = 0;
2611 transform.d[5] = -1;
2612 break;
2613 case WL_OUTPUT_TRANSFORM_270:
2614 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2615 transform.d[0] = 0;
2616 transform.d[1] = flip;
2617 transform.d[4] = -1;
2618 transform.d[5] = 0;
2619 break;
2620 default:
2621 break;
2622 }
2623
2624 weston_matrix_multiply(&output->matrix, &transform);
2625}
2626
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002627WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002628weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002629{
Scott Moreau850ca422012-05-21 15:21:25 -06002630 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002631 struct weston_matrix camera;
2632 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002633
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002634 weston_matrix_init(&output->matrix);
2635 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002636 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2637 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002638
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002639 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002640 2.0 / (output->width + output->border.left + output->border.right),
2641 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2642
2643 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002644
Scott Moreauccbf29d2012-02-22 14:21:41 -07002645 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002646 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002647 weston_matrix_init(&camera);
2648 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002649 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002650 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002651 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002652 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002653 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002654 weston_matrix_multiply(&output->matrix, &modelview);
2655 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002656
Scott Moreauccbf29d2012-02-22 14:21:41 -07002657 output->dirty = 0;
2658}
2659
Scott Moreau1bad5db2012-08-18 01:04:05 -06002660static void
2661weston_output_transform_init(struct weston_output *output, uint32_t transform)
2662{
2663 output->transform = transform;
2664
2665 switch (transform) {
2666 case WL_OUTPUT_TRANSFORM_90:
2667 case WL_OUTPUT_TRANSFORM_270:
2668 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2669 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2670 /* Swap width and height */
2671 output->width = output->current->height;
2672 output->height = output->current->width;
2673 break;
2674 case WL_OUTPUT_TRANSFORM_NORMAL:
2675 case WL_OUTPUT_TRANSFORM_180:
2676 case WL_OUTPUT_TRANSFORM_FLIPPED:
2677 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2678 output->width = output->current->width;
2679 output->height = output->current->height;
2680 break;
2681 default:
2682 break;
2683 }
2684}
2685
Scott Moreauccbf29d2012-02-22 14:21:41 -07002686WL_EXPORT void
2687weston_output_move(struct weston_output *output, int x, int y)
2688{
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002689 int i;
2690
Scott Moreauccbf29d2012-02-22 14:21:41 -07002691 output->x = x;
2692 output->y = y;
2693
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002694 output->current_buffer = 0;
2695 for (i = 0; i < 2; i++)
2696 pixman_region32_init(&output->buffer_damage[i]);
2697
Scott Moreauccbf29d2012-02-22 14:21:41 -07002698 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002699 output->width,
2700 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002701}
2702
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002703WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002704weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002705 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002706{
2707 output->compositor = c;
2708 output->x = x;
2709 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002710 output->border.top = 0;
2711 output->border.bottom = 0;
2712 output->border.left = 0;
2713 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002714 output->mm_width = width;
2715 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002716 output->dirty = 1;
2717
Scott Moreau1bad5db2012-08-18 01:04:05 -06002718 if (transform != WL_OUTPUT_TRANSFORM_NORMAL)
2719 output->disable_planes++;
2720
2721 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06002722 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002723
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002724 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002725 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002726
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002727 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002728 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002729 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002730
Casey Dahlin58ba1372012-04-19 22:50:08 -04002731 output->id = ffs(~output->compositor->output_id_pool) - 1;
2732 output->compositor->output_id_pool |= 1 << output->id;
2733
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002734 output->global =
2735 wl_display_add_global(c->wl_display, &wl_output_interface,
2736 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002737}
2738
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002739static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002740compositor_bind(struct wl_client *client,
2741 void *data, uint32_t version, uint32_t id)
2742{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002743 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002744
2745 wl_client_add_object(client, &wl_compositor_interface,
2746 &compositor_interface, id, compositor);
2747}
2748
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002749static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002750log_uname(void)
2751{
2752 struct utsname usys;
2753
2754 uname(&usys);
2755
2756 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2757 usys.version, usys.machine);
2758}
2759
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002760WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002761weston_compositor_init(struct weston_compositor *ec,
2762 struct wl_display *display,
2763 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04002764 char *argv[],
2765 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002766{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002767 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002768 struct xkb_rule_names xkb_names;
2769 const struct config_key keyboard_config_keys[] = {
2770 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2771 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2772 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2773 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2774 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2775 };
2776 const struct config_section cs[] = {
2777 { "keyboard",
2778 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
2779 };
2780
2781 memset(&xkb_names, 0, sizeof(xkb_names));
2782 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002783
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002784 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002785 wl_signal_init(&ec->destroy_signal);
2786 wl_signal_init(&ec->activate_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002787 wl_signal_init(&ec->kill_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002788 wl_signal_init(&ec->lock_signal);
2789 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002790 wl_signal_init(&ec->show_input_panel_signal);
2791 wl_signal_init(&ec->hide_input_panel_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002792 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002793
Casey Dahlin58ba1372012-04-19 22:50:08 -04002794 ec->output_id_pool = 0;
2795
Kristian Høgsberga8873122011-11-23 10:39:34 -05002796 if (!wl_display_add_global(display, &wl_compositor_interface,
2797 ec, compositor_bind))
2798 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002799
Daniel Stone725c2c32012-06-22 14:04:36 +01002800 wl_list_init(&ec->surface_list);
2801 wl_list_init(&ec->layer_list);
2802 wl_list_init(&ec->seat_list);
2803 wl_list_init(&ec->output_list);
2804 wl_list_init(&ec->key_binding_list);
2805 wl_list_init(&ec->button_binding_list);
2806 wl_list_init(&ec->axis_binding_list);
2807 wl_list_init(&ec->fade.animation.link);
2808
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002809 weston_plane_init(&ec->primary_plane, 0, 0);
2810
Daniel Stone725c2c32012-06-22 14:04:36 +01002811 weston_compositor_xkb_init(ec, &xkb_names);
2812
2813 ec->ping_handler = NULL;
2814
2815 screenshooter_create(ec);
2816 text_cursor_position_notifier_create(ec);
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +02002817 text_model_factory_create(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002818
2819 wl_data_device_manager_init(ec->wl_display);
2820
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002821 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002822
Daniel Stone725c2c32012-06-22 14:04:36 +01002823 loop = wl_display_get_event_loop(ec->wl_display);
2824 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2825 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2826
2827 ec->input_loop = wl_event_loop_create();
2828
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002829 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
2830 ec->fade.animation.frame = fade_frame;
2831
2832 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2833 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2834
2835 weston_compositor_schedule_repaint(ec);
2836
Daniel Stone725c2c32012-06-22 14:04:36 +01002837 return 0;
2838}
2839
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002840WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002841weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002842{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002843 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002844
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002845 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002846 if (ec->input_loop_source)
2847 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002848
Matt Roper361d2ad2011-08-29 13:52:23 -07002849 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002850 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002851 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002852
Daniel Stone325fc2d2012-05-30 16:31:58 +01002853 weston_binding_list_destroy_all(&ec->key_binding_list);
2854 weston_binding_list_destroy_all(&ec->button_binding_list);
2855 weston_binding_list_destroy_all(&ec->axis_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002856
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002857 weston_plane_release(&ec->primary_plane);
2858
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002859 wl_array_release(&ec->vertices);
2860 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05002861 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002862
2863 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07002864}
2865
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002866static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002867{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002868 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002869
Martin Minarik6d118362012-06-07 18:01:59 +02002870 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002871 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002872
2873 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002874}
2875
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002876static void
2877on_segv_signal(int s, siginfo_t *siginfo, void *context)
2878{
2879 void *buffer[32];
2880 int i, count;
2881 Dl_info info;
2882
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002883 /* This SIGSEGV handler will do a best-effort backtrace, and
2884 * then call the backend restore function, which will switch
2885 * back to the vt we launched from or ungrab X etc and then
2886 * raise SIGTRAP. If we run weston under gdb from X or a
2887 * different vt, and tell gdb "handle SIGSEGV nostop", this
2888 * will allow weston to switch back to gdb on crash and then
2889 * gdb will catch the crash with SIGTRAP. */
2890
Martin Minarik6d118362012-06-07 18:01:59 +02002891 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002892
2893 count = backtrace(buffer, ARRAY_LENGTH(buffer));
2894 for (i = 0; i < count; i++) {
2895 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02002896 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002897 (long) buffer[i],
2898 info.dli_sname ? info.dli_sname : "--",
2899 info.dli_fname);
2900 }
2901
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002902 segv_compositor->restore(segv_compositor);
2903
2904 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002905}
2906
2907
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002908static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04002909load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002910{
2911 char path[PATH_MAX];
2912 void *module, *init;
2913
2914 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07002915 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002916 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02002917 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002918
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002919 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
2920 if (module) {
2921 weston_log("Module '%s' already loaded\n", path);
2922 dlclose(module);
2923 return NULL;
2924 }
2925
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002926 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04002927 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002928 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002929 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002930 return NULL;
2931 }
2932
2933 init = dlsym(module, entrypoint);
2934 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002935 weston_log("Failed to lookup init function: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002936 return NULL;
2937 }
2938
2939 return init;
2940}
2941
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002942static int
2943load_modules(struct weston_compositor *ec, const char *modules)
2944{
2945 const char *p, *end;
2946 char buffer[256];
2947 int (*module_init)(struct weston_compositor *ec);
2948
2949 if (modules == NULL)
2950 return 0;
2951
2952 p = modules;
2953 while (*p) {
2954 end = strchrnul(p, ',');
2955 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
2956 module_init = load_module(buffer, "module_init");
2957 if (module_init)
2958 module_init(ec);
2959 p = end;
2960 while (*p == ',')
2961 p++;
2962
2963 }
2964
2965 return 0;
2966}
2967
Pekka Paalanen78a0b572012-06-06 16:59:44 +03002968static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02002969 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
2970
2971static const char xdg_wrong_message[] =
2972 "fatal: environment variable XDG_RUNTIME_DIR\n"
2973 "is set to \"%s\", which is not a directory.\n";
2974
2975static const char xdg_wrong_mode_message[] =
2976 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
2977 "correctly. Unix access mode must be 0700 but is %o,\n"
2978 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04002979 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02002980
2981static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03002982 "Refer to your distribution on how to get it, or\n"
2983 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
2984 "on how to implement it.\n";
2985
Martin Minarik37032f82012-06-18 20:15:18 +02002986static void
2987verify_xdg_runtime_dir(void)
2988{
2989 char *dir = getenv("XDG_RUNTIME_DIR");
2990 struct stat s;
2991
2992 if (!dir) {
2993 weston_log(xdg_error_message);
2994 weston_log_continue(xdg_detail_message);
2995 exit(EXIT_FAILURE);
2996 }
2997
2998 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
2999 weston_log(xdg_wrong_message, dir);
3000 weston_log_continue(xdg_detail_message);
3001 exit(EXIT_FAILURE);
3002 }
3003
3004 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3005 weston_log(xdg_wrong_mode_message,
3006 dir, s.st_mode & 0777, s.st_uid);
3007 weston_log_continue(xdg_detail_message);
3008 }
3009}
3010
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003011static int
3012usage(int error_code)
3013{
3014 fprintf(stderr,
3015 "Usage: weston [OPTIONS]\n\n"
3016 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3017 "Weston supports multiple backends, and depending on which backend is in use\n"
3018 "different options will be accepted.\n\n"
3019
3020
3021 "Core options:\n\n"
3022 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3023 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3024 " -S, --socket=NAME\tName of socket to listen on\n"
3025 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003026 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003027 " --log==FILE\t\tLog to the given file\n"
3028 " -h, --help\t\tThis help message\n\n");
3029
3030 fprintf(stderr,
3031 "Options for drm-backend.so:\n\n"
3032 " --connector=ID\tBring up only this connector\n"
3033 " --seat=SEAT\t\tThe seat that weston should run on\n"
3034 " --tty=TTY\t\tThe tty to use\n"
3035 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3036
3037 fprintf(stderr,
3038 "Options for x11-backend.so:\n\n"
3039 " --width=WIDTH\t\tWidth of X window\n"
3040 " --height=HEIGHT\tHeight of X window\n"
3041 " --fullscreen\t\tRun in fullscreen mode\n"
3042 " --output-count=COUNT\tCreate multiple outputs\n"
3043 " --no-input\t\tDont create input devices\n\n");
3044
3045 fprintf(stderr,
3046 "Options for wayland-backend.so:\n\n"
3047 " --width=WIDTH\t\tWidth of Wayland surface\n"
3048 " --height=HEIGHT\tHeight of Wayland surface\n"
3049 " --display=DISPLAY\tWayland display to connect to\n\n");
3050
3051 exit(error_code);
3052}
3053
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003054int main(int argc, char *argv[])
3055{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003056 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003057 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003058 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003059 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003060 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003061 struct sigaction segv_action;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003062 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003063 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003064 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003065 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003066 char *backend = NULL;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003067 const char *modules = "desktop-shell.so", *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003068 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003069 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003070 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003071 char *socket_name = "wayland-0";
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003072 char *config_file;
3073
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003074 const struct config_key core_config_keys[] = {
3075 { "modules", CONFIG_KEY_STRING, &modules },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003076 };
3077
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003078 const struct config_section cs[] = {
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003079 { "core",
3080 core_config_keys, ARRAY_LENGTH(core_config_keys) },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003081 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003082
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003083 const struct weston_option core_options[] = {
3084 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3085 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3086 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003087 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003088 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003089 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003090 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003091
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003092 argc = parse_options(core_options,
3093 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003094
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003095 if (help)
3096 usage(EXIT_SUCCESS);
3097
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003098 weston_log_file_open(log);
3099
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003100 weston_log("%s\n"
3101 STAMP_SPACE "%s\n"
3102 STAMP_SPACE "Bug reports to: %s\n"
3103 STAMP_SPACE "Build: %s\n",
3104 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003105 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003106 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003107
Martin Minarik37032f82012-06-18 20:15:18 +02003108 verify_xdg_runtime_dir();
3109
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003110 display = wl_display_create();
3111
Tiago Vignatti2116b892011-08-08 05:52:59 -07003112 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003113 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3114 display);
3115 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3116 display);
3117 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3118 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003119
3120 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003121 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3122 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003123
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003124 if (!backend) {
3125 if (getenv("WAYLAND_DISPLAY"))
3126 backend = "wayland-backend.so";
3127 else if (getenv("DISPLAY"))
3128 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003129 else
3130 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003131 }
3132
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003133 config_file = config_file_path("weston.ini");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003134 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003135
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003136 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003137 if (!backend_init)
3138 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003139
Daniel Stonec1be8e52012-06-01 11:14:02 -04003140 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003141 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003142 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003143 exit(EXIT_FAILURE);
3144 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003145
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003146 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3147 segv_action.sa_sigaction = on_segv_signal;
3148 sigemptyset(&segv_action.sa_mask);
3149 sigaction(SIGSEGV, &segv_action, NULL);
3150 segv_compositor = ec;
3151
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003152 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03003153 weston_log("fatal: unhandled option: %s\n", argv[i]);
3154 if (argv[1]) {
3155 ret = EXIT_FAILURE;
3156 goto out;
3157 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003158
Daniel Stonec1be8e52012-06-01 11:14:02 -04003159 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01003160
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003161 ec->option_idle_time = idle_time;
3162 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003163
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003164 setenv("WAYLAND_DISPLAY", socket_name, 1);
3165
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003166 if (load_modules(ec, modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003167 goto out;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003168 if (load_modules(ec, option_modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003169 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003170
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003171 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003172 weston_log("fatal: failed to add socket: %m\n");
3173 ret = EXIT_FAILURE;
3174 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003175 }
3176
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003177 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003178 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003179
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003180 wl_display_run(display);
3181
3182 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003183 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05003184 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003185
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003186 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003187
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003188 for (i = ARRAY_LENGTH(signals); i;)
3189 wl_event_source_remove(signals[--i]);
3190
Daniel Stone855028f2012-05-01 20:37:10 +01003191 weston_compositor_xkb_destroy(ec);
3192
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003193 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003194 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003195
Martin Minarik19e6f262012-06-07 13:08:46 +02003196 weston_log_file_close();
3197
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003198 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003199}