blob: 79b66510bbcf4bd9c4334e100db1f6f423b14080 [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;
277
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400278 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500279}
280
Alex Wu8811bf92012-02-28 18:07:54 +0800281WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500282weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200283 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500284{
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500285 surface->color[0] = red;
286 surface->color[1] = green;
287 surface->color[2] = blue;
288 surface->color[3] = alpha;
289 surface->shader = &surface->compositor->solid_shader;
290}
291
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400292WL_EXPORT void
293weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200294 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200295{
296 if (surface->transform.enabled) {
297 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
298
299 weston_matrix_transform(&surface->transform.matrix, &v);
300
301 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200302 weston_log("warning: numerical instability in "
Pekka Paalanenece8a012012-02-08 15:23:15 +0200303 "weston_surface_to_global(), divisor = %g\n",
304 v.f[3]);
305 *x = 0;
306 *y = 0;
307 return;
308 }
309
310 *x = v.f[0] / v.f[3];
311 *y = v.f[1] / v.f[3];
312 } else {
313 *x = sx + surface->geometry.x;
314 *y = sy + surface->geometry.y;
315 }
316}
317
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500318WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400319weston_surface_move_to_plane(struct weston_surface *surface,
320 struct weston_plane *plane)
321{
322 if (surface->plane == plane)
323 return;
324
325 weston_surface_damage_below(surface);
326 surface->plane = plane;
327 weston_surface_damage(surface);
328}
329
330WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500331weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200332{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500333 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200334
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500335 pixman_region32_init(&damage);
336 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
337 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400338 pixman_region32_union(&surface->plane->damage,
339 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500340 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200341}
342
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400343static struct wl_resource *
344find_resource_for_client(struct wl_list *list, struct wl_client *client)
345{
346 struct wl_resource *r;
347
348 wl_list_for_each(r, list, link) {
349 if (r->client == client)
350 return r;
351 }
352
353 return NULL;
354}
355
356static void
357weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
358{
359 uint32_t different = es->output_mask ^ mask;
360 uint32_t entered = mask & different;
361 uint32_t left = es->output_mask & different;
362 struct weston_output *output;
363 struct wl_resource *resource = NULL;
364 struct wl_client *client = es->surface.resource.client;
365
366 es->output_mask = mask;
367 if (es->surface.resource.client == NULL)
368 return;
369 if (different == 0)
370 return;
371
372 wl_list_for_each(output, &es->compositor->output_list, link) {
373 if (1 << output->id & different)
374 resource =
375 find_resource_for_client(&output->resource_list,
376 client);
377 if (resource == NULL)
378 continue;
379 if (1 << output->id & entered)
380 wl_surface_send_enter(&es->surface.resource, resource);
381 if (1 << output->id & left)
382 wl_surface_send_leave(&es->surface.resource, resource);
383 }
384}
385
386static void
387weston_surface_assign_output(struct weston_surface *es)
388{
389 struct weston_compositor *ec = es->compositor;
390 struct weston_output *output, *new_output;
391 pixman_region32_t region;
392 uint32_t max, area, mask;
393 pixman_box32_t *e;
394
395 new_output = NULL;
396 max = 0;
397 mask = 0;
398 pixman_region32_init(&region);
399 wl_list_for_each(output, &ec->output_list, link) {
400 pixman_region32_intersect(&region, &es->transform.boundingbox,
401 &output->region);
402
403 e = pixman_region32_extents(&region);
404 area = (e->x2 - e->x1) * (e->y2 - e->y1);
405
406 if (area > 0)
407 mask |= 1 << output->id;
408
409 if (area >= max) {
410 new_output = output;
411 max = area;
412 }
413 }
414 pixman_region32_fini(&region);
415
416 es->output = new_output;
417 weston_surface_update_output_mask(es, mask);
418}
419
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200420static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200421surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
422 int32_t width, int32_t height,
423 pixman_region32_t *bbox)
424{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200425 float min_x = HUGE_VALF, min_y = HUGE_VALF;
426 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200427 int32_t s[4][2] = {
428 { sx, sy },
429 { sx, sy + height },
430 { sx + width, sy },
431 { sx + width, sy + height }
432 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200433 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200434 int i;
435
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300436 if (width == 0 || height == 0) {
437 /* avoid rounding empty bbox to 1x1 */
438 pixman_region32_init(bbox);
439 return;
440 }
441
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200442 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200443 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400444 weston_surface_to_global_float(surface,
445 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200446 if (x < min_x)
447 min_x = x;
448 if (x > max_x)
449 max_x = x;
450 if (y < min_y)
451 min_y = y;
452 if (y > max_y)
453 max_y = y;
454 }
455
Pekka Paalanen219b9822012-02-08 15:38:37 +0200456 int_x = floorf(min_x);
457 int_y = floorf(min_y);
458 pixman_region32_init_rect(bbox, int_x, int_y,
459 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200460}
461
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200462static void
463weston_surface_update_transform_disable(struct weston_surface *surface)
464{
465 surface->transform.enabled = 0;
466
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200467 /* round off fractions when not transformed */
468 surface->geometry.x = roundf(surface->geometry.x);
469 surface->geometry.y = roundf(surface->geometry.y);
470
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200471 pixman_region32_init_rect(&surface->transform.boundingbox,
472 surface->geometry.x,
473 surface->geometry.y,
474 surface->geometry.width,
475 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500476
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400477 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500478 pixman_region32_copy(&surface->transform.opaque,
479 &surface->opaque);
480 pixman_region32_translate(&surface->transform.opaque,
481 surface->geometry.x,
482 surface->geometry.y);
483 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200484}
485
486static int
487weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200488{
489 struct weston_matrix *matrix = &surface->transform.matrix;
490 struct weston_matrix *inverse = &surface->transform.inverse;
491 struct weston_transform *tform;
492
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200493 surface->transform.enabled = 1;
494
495 /* Otherwise identity matrix, but with x and y translation. */
496 surface->transform.position.matrix.d[12] = surface->geometry.x;
497 surface->transform.position.matrix.d[13] = surface->geometry.y;
498
499 weston_matrix_init(matrix);
500 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
501 weston_matrix_multiply(matrix, &tform->matrix);
502
503 if (weston_matrix_invert(inverse, matrix) < 0) {
504 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200505 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200506 " transformation not invertible.\n", surface);
507 return -1;
508 }
509
510 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
511 surface->geometry.height,
512 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500513
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200514 return 0;
515}
516
517WL_EXPORT void
518weston_surface_update_transform(struct weston_surface *surface)
519{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200520 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200521 return;
522
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200523 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200524
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500525 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200526
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200527 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500528 pixman_region32_fini(&surface->transform.opaque);
529 pixman_region32_init(&surface->transform.opaque);
530
531 if (region_is_undefined(&surface->input))
532 pixman_region32_init_rect(&surface->input, 0, 0,
533 surface->geometry.width,
534 surface->geometry.height);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200535
Pekka Paalanencd403622012-01-25 13:37:39 +0200536 /* transform.position is always in transformation_list */
537 if (surface->geometry.transformation_list.next ==
538 &surface->transform.position.link &&
539 surface->geometry.transformation_list.prev ==
540 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200541 weston_surface_update_transform_disable(surface);
542 } else {
543 if (weston_surface_update_transform_enable(surface) < 0)
544 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200545 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200546
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400547 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200548
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300549 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200550}
551
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200552WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100553weston_surface_to_global_fixed(struct weston_surface *surface,
554 wl_fixed_t sx, wl_fixed_t sy,
555 wl_fixed_t *x, wl_fixed_t *y)
556{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200557 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100558
559 weston_surface_to_global_float(surface,
560 wl_fixed_to_double(sx),
561 wl_fixed_to_double(sy),
562 &xf, &yf);
563 *x = wl_fixed_from_double(xf);
564 *y = wl_fixed_from_double(yf);
565}
566
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400567WL_EXPORT void
568weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200569 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200570{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200571 if (surface->transform.enabled) {
572 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
573
574 weston_matrix_transform(&surface->transform.inverse, &v);
575
576 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200577 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200578 "weston_surface_from_global(), divisor = %g\n",
579 v.f[3]);
580 *sx = 0;
581 *sy = 0;
582 return;
583 }
584
Pekka Paalanencd403622012-01-25 13:37:39 +0200585 *sx = v.f[0] / v.f[3];
586 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200587 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200588 *sx = x - surface->geometry.x;
589 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200590 }
591}
592
593WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100594weston_surface_from_global_fixed(struct weston_surface *surface,
595 wl_fixed_t x, wl_fixed_t y,
596 wl_fixed_t *sx, wl_fixed_t *sy)
597{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200598 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100599
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400600 weston_surface_from_global_float(surface,
601 wl_fixed_to_double(x),
602 wl_fixed_to_double(y),
603 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100604 *sx = wl_fixed_from_double(sxf);
605 *sy = wl_fixed_from_double(syf);
606}
607
608WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200609weston_surface_from_global(struct weston_surface *surface,
610 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
611{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200612 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200613
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400614 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200615 *sx = floorf(sxf);
616 *sy = floorf(syf);
617}
618
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400619WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400620weston_surface_schedule_repaint(struct weston_surface *surface)
621{
622 struct weston_output *output;
623
624 wl_list_for_each(output, &surface->compositor->output_list, link)
625 if (surface->output_mask & (1 << output->id))
626 weston_output_schedule_repaint(output);
627}
628
629WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500630weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500631{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400632 pixman_region32_union_rect(&surface->damage, &surface->damage,
633 0, 0, surface->geometry.width,
634 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200635
Kristian Høgsberg98238702012-08-03 16:29:12 -0400636 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500637}
638
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400639WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500640weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200641 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400642{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200643 surface->geometry.x = x;
644 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200645 surface->geometry.width = width;
646 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200647 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400648}
649
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200650WL_EXPORT void
651weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200652 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200653{
654 surface->geometry.x = x;
655 surface->geometry.y = y;
656 surface->geometry.dirty = 1;
657}
658
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300659WL_EXPORT int
660weston_surface_is_mapped(struct weston_surface *surface)
661{
662 if (surface->output)
663 return 1;
664 else
665 return 0;
666}
667
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400668WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500669weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500670{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400671 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500672
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400673 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500674
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400675 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500676}
677
Tiago Vignatti9d393522012-02-10 16:26:19 +0200678static struct weston_surface *
679weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100680 wl_fixed_t x, wl_fixed_t y,
681 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200682{
683 struct weston_surface *surface;
684
685 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100686 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500687 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100688 wl_fixed_to_int(*sx),
689 wl_fixed_to_int(*sy),
690 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200691 return surface;
692 }
693
694 return NULL;
695}
696
697static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400698weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500699{
Scott Moreau447013d2012-02-18 05:05:29 -0700700 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500701 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400702 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500703
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400704 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100705 return;
706
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400707 surface = weston_compositor_pick_surface(seat->compositor,
708 pointer->x,
709 pointer->y,
710 &pointer->current_x,
711 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500712
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400713 if (&surface->surface != pointer->current) {
714 interface = pointer->grab->interface;
715 pointer->current = &surface->surface;
716 interface->focus(pointer->grab, &surface->surface,
717 pointer->current_x,
718 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500719 }
720
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400721 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500722 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100723 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400724 pointer->x,
725 pointer->y,
726 &pointer->grab->x,
727 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500728}
729
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400730static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500731weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400732{
Daniel Stone37816df2012-05-16 18:45:18 +0100733 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400734
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500735 if (!compositor->focus)
736 return;
737
Daniel Stone37816df2012-05-16 18:45:18 +0100738 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400739 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400740}
741
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400742WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500743weston_surface_unmap(struct weston_surface *surface)
744{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100745 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500746
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500747 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500748 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500749 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500750
Daniel Stone4dab5db2012-05-30 16:31:53 +0100751 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100752 if (seat->seat.keyboard &&
753 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100754 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100755 if (seat->seat.pointer &&
756 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100757 wl_pointer_set_focus(seat->seat.pointer,
758 NULL,
759 wl_fixed_from_int(0),
760 wl_fixed_from_int(0));
761 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500762
Kristian Høgsberg98238702012-08-03 16:29:12 -0400763 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500764}
765
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400766struct weston_frame_callback {
767 struct wl_resource resource;
768 struct wl_list link;
769};
770
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500771static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400772destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500773{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500774 struct weston_surface *surface =
775 container_of(resource,
776 struct weston_surface, surface.resource);
777 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400778 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400779
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300780 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500781 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400782
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300783 if (surface->pending.buffer)
784 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
785
Benjamin Franzke06286262011-05-06 19:12:33 +0200786 if (surface->buffer)
787 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400788
Kristian Høgsberg42263852012-09-06 21:59:29 -0400789 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200790
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200791 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200792 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500793 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500794 pixman_region32_fini(&surface->clip);
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500795 if (!region_is_undefined(&surface->input))
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500796 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200797
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400798 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
799 wl_resource_destroy(&cb->resource);
800
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400801 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500802}
803
Alex Wu8811bf92012-02-28 18:07:54 +0800804WL_EXPORT void
805weston_surface_destroy(struct weston_surface *surface)
806{
807 /* Not a valid way to destroy a client surface */
808 assert(surface->surface.resource.client == NULL);
809
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400810 wl_signal_emit(&surface->surface.resource.destroy_signal,
811 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800812 destroy_surface(&surface->surface.resource);
813}
814
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100815static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300816weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100817{
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300818 if (surface->buffer) {
819 weston_buffer_post_release(surface->buffer);
820 wl_list_remove(&surface->buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300821 }
822
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400823 if (buffer) {
824 buffer->busy_count++;
825 wl_signal_add(&buffer->resource.destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300826 &surface->buffer_destroy_listener);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300827
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300828 if (surface->geometry.width != buffer->width ||
829 surface->geometry.height != buffer->height) {
830 undef_region(&surface->input);
831 pixman_region32_fini(&surface->opaque);
832 pixman_region32_init(&surface->opaque);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400833 }
834 } else {
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300835 if (weston_surface_is_mapped(surface))
836 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300837 }
838
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300839 surface->compositor->renderer->attach(surface, buffer);
840 surface->buffer = buffer;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100841}
842
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500843WL_EXPORT void
844weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400845{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500846 wl_list_remove(&surface->layer_link);
847 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500848 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500849 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400850}
851
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400852WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500853weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400854{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500855 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400856
857 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500858 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400859}
860
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500861WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500862weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +0200863{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400864 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200865 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200866
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400867 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -0500868 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200869}
870
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400871WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500872weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400873{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500874 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400875
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400876 pixman_region32_union(&compositor->primary_plane.damage,
877 &compositor->primary_plane.damage,
878 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400879 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400880}
881
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400882static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500883fade_frame(struct weston_animation *animation,
884 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400885{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500886 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400887 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500888 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500889 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400890
Scott Moreau94b0b0c2012-06-14 01:01:56 -0600891 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -0600892 compositor->fade.spring.timestamp = msecs;
893
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500894 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500895 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500896 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
897 compositor->fade.spring.current);
898 weston_surface_damage(surface);
899
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500900 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400901 compositor->fade.spring.current =
902 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400903 wl_list_remove(&animation->link);
904 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200905
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500906 if (compositor->fade.spring.current < 0.001) {
907 destroy_surface(&surface->surface.resource);
908 compositor->fade.surface = NULL;
909 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500910 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400911 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200912 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400913 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400914}
915
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400916static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400917surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400918 pixman_region32_t *opaque)
919{
920 if (surface->buffer && wl_buffer_is_shm(surface->buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400921 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400922
923 if (surface->transform.enabled) {
924 pixman_box32_t *extents;
925
926 extents = pixman_region32_extents(&surface->damage);
927 surface_compute_bbox(surface, extents->x1, extents->y1,
928 extents->x2 - extents->x1,
929 extents->y2 - extents->y1,
930 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400931 pixman_region32_translate(&surface->damage,
932 -surface->plane->x,
933 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400934 } else {
935 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400936 surface->geometry.x - surface->plane->x,
937 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400938 }
939
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400940 pixman_region32_union(&surface->plane->damage,
941 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400942 empty_region(&surface->damage);
943 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400944 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400945}
946
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400947static void
Rob Bradford0fb79752012-08-02 15:36:57 +0100948weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400949{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500950 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500951 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500952 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -0500953 struct weston_animation *animation, *next;
954 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +0200955 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300956 pixman_region32_t opaque, output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500957
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +0200958 weston_compositor_update_drag_surfaces(ec);
959
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500960 /* Rebuild the surface list and update surface transforms up front. */
961 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +0200962 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500963 wl_list_for_each(layer, &ec->layer_list, link) {
964 wl_list_for_each(es, &layer->surface_list, layer_link) {
965 weston_surface_update_transform(es);
966 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +0200967 if (es->output == output) {
968 wl_list_insert_list(&frame_callback_list,
969 &es->frame_callback_list);
970 wl_list_init(&es->frame_callback_list);
971 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500972 }
973 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +0200974
Kristian Høgsberg79af73e2012-08-03 15:45:23 -0400975 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800976 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -0400977 else
978 wl_list_for_each(es, &ec->surface_list, link)
979 weston_surface_move_to_plane(es, &ec->primary_plane);
980
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500981 pixman_region32_init(&opaque);
982
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400983 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400984 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500985
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400986 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -0400987
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400988 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400989 pixman_region32_intersect(&output_damage,
990 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300991 pixman_region32_subtract(&ec->primary_plane.damage,
992 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400993
Scott Moreauccbf29d2012-02-22 14:21:41 -0700994 if (output->dirty)
995 weston_output_update_matrix(output);
996
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400997 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400998
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500999 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001000
Kristian Høgsbergef044142011-06-21 15:02:12 -04001001 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001002
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001003 weston_compositor_repick(ec);
1004 wl_event_loop_dispatch(ec->input_loop, 0);
1005
Jonas Ådahldb773762012-06-13 00:01:21 +02001006 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001007 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001008 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001009 }
Jonas Ådahldb773762012-06-13 00:01:21 +02001010 wl_list_init(&frame_callback_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001011
Scott Moreaud64cf212012-06-08 19:40:54 -06001012 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001013 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001014 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001015 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001016}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001017
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001018static int
1019weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001020{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001021 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001022
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001023 wl_event_loop_dispatch(compositor->input_loop, 0);
1024
1025 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001026}
1027
1028WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001029weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001030{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001031 struct weston_compositor *compositor = output->compositor;
1032 struct wl_event_loop *loop =
1033 wl_display_get_event_loop(compositor->wl_display);
1034 int fd;
1035
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001036 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001037 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001038 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001039 return;
1040 }
1041
1042 output->repaint_scheduled = 0;
1043 if (compositor->input_loop_source)
1044 return;
1045
1046 fd = wl_event_loop_get_fd(compositor->input_loop);
1047 compositor->input_loop_source =
1048 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1049 weston_compositor_read_input, compositor);
1050}
1051
1052static void
1053idle_repaint(void *data)
1054{
1055 struct weston_output *output = data;
1056
1057 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001058}
1059
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001060WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001061weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1062{
1063 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001064 if (below != NULL)
1065 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001066}
1067
1068WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001069weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001070{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001071 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001072 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001073
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001074 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001075 return;
1076
Kristian Høgsbergef044142011-06-21 15:02:12 -04001077 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001078 output->repaint_needed = 1;
1079 if (output->repaint_scheduled)
1080 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001081
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001082 wl_event_loop_add_idle(loop, idle_repaint, output);
1083 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001084
1085 if (compositor->input_loop_source) {
1086 wl_event_source_remove(compositor->input_loop_source);
1087 compositor->input_loop_source = NULL;
1088 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001089}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001090
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001091WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001092weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1093{
1094 struct weston_output *output;
1095
1096 wl_list_for_each(output, &compositor->output_list, link)
1097 weston_output_schedule_repaint(output);
1098}
1099
1100WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001101weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001102{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001103 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001104 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001105
Scott Moreau9d1b1122012-06-08 19:40:53 -06001106 output = container_of(compositor->output_list.next,
1107 struct weston_output, link);
1108
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001109 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001110 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001111 return;
1112
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001113 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001114 surface = weston_surface_create(compositor);
1115 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001116 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001117 wl_list_insert(&compositor->fade_layer.surface_list,
1118 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03001119 weston_surface_update_transform(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001120 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001121 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001122 }
1123
1124 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001125 if (wl_list_empty(&compositor->fade.animation.link)) {
1126 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001127 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001128 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001129 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001130}
1131
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001132static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001133surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001134{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001135 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001136}
1137
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001138static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001139surface_attach(struct wl_client *client,
1140 struct wl_resource *resource,
1141 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1142{
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001143 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001144 struct wl_buffer *buffer = NULL;
1145
1146 if (buffer_resource)
1147 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001148
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001149 if (surface->pending.buffer)
1150 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001151
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001152 surface->pending.sx = sx;
1153 surface->pending.sy = sy;
1154 surface->pending.buffer = buffer;
1155 if (buffer) {
1156 wl_signal_add(&buffer->resource.destroy_signal,
1157 &surface->pending.buffer_destroy_listener);
1158 surface->pending.remove_contents = 0;
1159 } else {
1160 surface->pending.remove_contents = 1;
1161 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001162}
1163
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001164static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001165surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001166 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001167 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001168{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001169 struct weston_surface *es = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001170
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001171 pixman_region32_union_rect(&es->damage, &es->damage,
1172 x, y, width, height);
Kristian Høgsberg98238702012-08-03 16:29:12 -04001173 weston_surface_schedule_repaint(es);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001174}
1175
Kristian Høgsberg33418202011-08-16 23:01:28 -04001176static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001177destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001178{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001179 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001180
1181 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001182 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001183}
1184
1185static void
1186surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001187 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001188{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001189 struct weston_frame_callback *cb;
1190 struct weston_surface *es = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001191
1192 cb = malloc(sizeof *cb);
1193 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001194 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001195 return;
1196 }
1197
1198 cb->resource.object.interface = &wl_callback_interface;
1199 cb->resource.object.id = callback;
1200 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001201 cb->resource.client = client;
1202 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001203
1204 wl_client_add_resource(client, &cb->resource);
Jonas Ådahldb773762012-06-13 00:01:21 +02001205 wl_list_insert(es->frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001206}
1207
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001208static void
1209surface_set_opaque_region(struct wl_client *client,
1210 struct wl_resource *resource,
1211 struct wl_resource *region_resource)
1212{
1213 struct weston_surface *surface = resource->data;
1214 struct weston_region *region;
1215
1216 pixman_region32_fini(&surface->opaque);
1217
1218 if (region_resource) {
1219 region = region_resource->data;
1220 pixman_region32_init_rect(&surface->opaque, 0, 0,
1221 surface->geometry.width,
1222 surface->geometry.height);
1223 pixman_region32_intersect(&surface->opaque,
1224 &surface->opaque, &region->region);
1225 } else {
1226 pixman_region32_init(&surface->opaque);
1227 }
1228
1229 surface->geometry.dirty = 1;
1230}
1231
1232static void
1233surface_set_input_region(struct wl_client *client,
1234 struct wl_resource *resource,
1235 struct wl_resource *region_resource)
1236{
1237 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001238 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001239
1240 if (region_resource) {
1241 region = region_resource->data;
1242 pixman_region32_init_rect(&surface->input, 0, 0,
1243 surface->geometry.width,
1244 surface->geometry.height);
1245 pixman_region32_intersect(&surface->input,
1246 &surface->input, &region->region);
1247 } else {
1248 pixman_region32_init_rect(&surface->input, 0, 0,
1249 surface->geometry.width,
1250 surface->geometry.height);
1251 }
1252
Kristian Høgsberg98238702012-08-03 16:29:12 -04001253 weston_surface_schedule_repaint(surface);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001254}
1255
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001256static void
1257surface_commit(struct wl_client *client, struct wl_resource *resource)
1258{
1259 struct weston_surface *surface = resource->data;
1260
1261 /* wl_surface.attach */
1262 if (surface->pending.buffer || surface->pending.remove_contents)
1263 weston_surface_attach(surface, surface->pending.buffer);
1264
1265 if (surface->buffer && surface->configure)
1266 surface->configure(surface, surface->pending.sx,
1267 surface->pending.sy);
1268}
1269
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001270static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001271 surface_destroy,
1272 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001273 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001274 surface_frame,
1275 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001276 surface_set_input_region,
1277 surface_commit
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001278};
1279
1280static void
1281compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001282 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001283{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001284 struct weston_compositor *ec = resource->data;
1285 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001286
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001287 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001288 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001289 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001290 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001291 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001292
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001293 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001294
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001295 surface->surface.resource.object.id = id;
1296 surface->surface.resource.object.interface = &wl_surface_interface;
1297 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001298 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001299 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001300
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001301 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001302}
1303
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001304static void
1305destroy_region(struct wl_resource *resource)
1306{
1307 struct weston_region *region =
1308 container_of(resource, struct weston_region, resource);
1309
1310 pixman_region32_fini(&region->region);
1311 free(region);
1312}
1313
1314static void
1315region_destroy(struct wl_client *client, struct wl_resource *resource)
1316{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001317 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001318}
1319
1320static void
1321region_add(struct wl_client *client, struct wl_resource *resource,
1322 int32_t x, int32_t y, int32_t width, int32_t height)
1323{
1324 struct weston_region *region = resource->data;
1325
1326 pixman_region32_union_rect(&region->region, &region->region,
1327 x, y, width, height);
1328}
1329
1330static void
1331region_subtract(struct wl_client *client, struct wl_resource *resource,
1332 int32_t x, int32_t y, int32_t width, int32_t height)
1333{
1334 struct weston_region *region = resource->data;
1335 pixman_region32_t rect;
1336
1337 pixman_region32_init_rect(&rect, x, y, width, height);
1338 pixman_region32_subtract(&region->region, &region->region, &rect);
1339 pixman_region32_fini(&rect);
1340}
1341
1342static const struct wl_region_interface region_interface = {
1343 region_destroy,
1344 region_add,
1345 region_subtract
1346};
1347
1348static void
1349compositor_create_region(struct wl_client *client,
1350 struct wl_resource *resource, uint32_t id)
1351{
1352 struct weston_region *region;
1353
1354 region = malloc(sizeof *region);
1355 if (region == NULL) {
1356 wl_resource_post_no_memory(resource);
1357 return;
1358 }
1359
1360 region->resource.destroy = destroy_region;
1361
1362 region->resource.object.id = id;
1363 region->resource.object.interface = &wl_region_interface;
1364 region->resource.object.implementation =
1365 (void (**)(void)) &region_interface;
1366 region->resource.data = region;
1367
1368 pixman_region32_init(&region->region);
1369
1370 wl_client_add_resource(client, &region->resource);
1371}
1372
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001373static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001374 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001375 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001376};
1377
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001378WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001379weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001380{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001381 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1382 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001383
1384 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001385 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001386}
1387
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001388static void
1389weston_compositor_dpms_on(struct weston_compositor *compositor)
1390{
1391 struct weston_output *output;
1392
1393 wl_list_for_each(output, &compositor->output_list, link)
1394 if (output->set_dpms)
1395 output->set_dpms(output, WESTON_DPMS_ON);
1396}
1397
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001398WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001399weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001400{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001401 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1402 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001403 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001404 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001405 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001406 }
1407}
1408
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001409static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001410weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001411{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001412 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001413 compositor->idle_inhibit++;
1414}
1415
1416static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001417weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001418{
1419 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001420 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001421}
1422
1423static int
1424idle_handler(void *data)
1425{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001426 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001427
1428 if (compositor->idle_inhibit)
1429 return 1;
1430
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001431 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001432
1433 return 1;
1434}
1435
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001436WL_EXPORT void
1437weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1438{
1439 pixman_region32_init(&plane->damage);
1440 plane->x = x;
1441 plane->y = y;
1442}
1443
1444WL_EXPORT void
1445weston_plane_release(struct weston_plane *plane)
1446{
1447 pixman_region32_fini(&plane->damage);
1448}
1449
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001450static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001451weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001452
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001453static void
Daniel Stone37816df2012-05-16 18:45:18 +01001454clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001455{
Daniel Stone37816df2012-05-16 18:45:18 +01001456 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001457 struct weston_output *output, *prev = NULL;
1458 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001459
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001460 x = wl_fixed_to_int(*fx);
1461 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001462 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1463 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001464
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001465 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001466 if (pixman_region32_contains_point(&output->region,
1467 x, y, NULL))
1468 valid = 1;
1469 if (pixman_region32_contains_point(&output->region,
1470 old_x, old_y, NULL))
1471 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001472 }
Daniel Stone37816df2012-05-16 18:45:18 +01001473
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001474 if (!valid) {
1475 if (x < prev->x)
1476 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001477 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001478 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001479 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001480 if (y < prev->y)
1481 *fy = wl_fixed_from_int(prev->y);
1482 else if (y >= prev->y + prev->current->height)
1483 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001484 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001485 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001486}
1487
1488WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001489notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001490{
1491 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001492 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001493 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001494 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001495 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001496
1497 weston_compositor_activity(ec);
1498
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001499 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001500
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001501 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001502
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001503 pointer->x = x;
1504 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001505
1506 ix = wl_fixed_to_int(x);
1507 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001508
Scott Moreauccbf29d2012-02-22 14:21:41 -07001509 wl_list_for_each(output, &ec->output_list, link)
1510 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001511 pixman_region32_contains_point(&output->region,
1512 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06001513 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001514
Daniel Stone37816df2012-05-16 18:45:18 +01001515 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001516 interface = pointer->grab->interface;
1517 interface->motion(pointer->grab, time,
1518 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001519
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001520 if (seat->sprite) {
1521 weston_surface_set_position(seat->sprite,
1522 ix - seat->hotspot_x,
1523 iy - seat->hotspot_y);
1524 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001525 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001526}
1527
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001528WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001529weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001530 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001531{
Daniel Stone37816df2012-05-16 18:45:18 +01001532 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001533
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001534 if (seat->seat.keyboard) {
1535 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1536 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04001537 }
1538
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001539 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001540}
1541
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001542WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001543notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001544 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001545{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001546 struct weston_compositor *compositor = seat->compositor;
1547 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001548 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001549 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001550 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001551
Daniel Stone4dbadb12012-05-30 16:31:51 +01001552 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001553 if (compositor->ping_handler && focus)
1554 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001555 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001556 if (pointer->button_count == 0) {
1557 pointer->grab_button = button;
1558 pointer->grab_time = time;
1559 pointer->grab_x = pointer->x;
1560 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001561 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001562 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001563 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001564 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001565 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001566 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001567
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001568 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001569 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001570
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001571 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001572
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001573 if (pointer->button_count == 1)
1574 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001575 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001576}
1577
Scott Moreau210d0792012-03-22 10:47:01 -06001578WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001579notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01001580 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001581{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001582 struct weston_compositor *compositor = seat->compositor;
1583 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001584 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001585 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001586 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1587
1588 if (compositor->ping_handler && focus)
1589 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001590
1591 weston_compositor_activity(compositor);
1592
Scott Moreau6a3633d2012-03-20 08:47:59 -06001593 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001594 weston_compositor_run_axis_binding(compositor, seat,
1595 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06001596 else
1597 return;
1598
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001599 if (pointer->focus_resource)
1600 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01001601 value);
Scott Moreau210d0792012-03-22 10:47:01 -06001602}
1603
Daniel Stone05d58682012-06-22 13:21:38 +01001604WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001605notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001606{
Daniel Stone05d58682012-06-22 13:21:38 +01001607 struct wl_keyboard *keyboard = &seat->keyboard;
1608 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01001609 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001610 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01001611 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01001612 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01001613
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001614 /* Serialize and update our internal state, checking to see if it's
1615 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01001616 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1617 XKB_STATE_DEPRESSED);
1618 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1619 XKB_STATE_LATCHED);
1620 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1621 XKB_STATE_LOCKED);
1622 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01001623 XKB_STATE_EFFECTIVE);
1624
Daniel Stone71c38772012-06-22 13:21:30 +01001625 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
1626 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
1627 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
1628 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01001629 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01001630
Daniel Stone71c38772012-06-22 13:21:30 +01001631 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
1632 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
1633 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
1634 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001635
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001636 /* And update the modifier_state for bindings. */
1637 mods_lookup = mods_depressed | mods_latched;
1638 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01001639 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001640 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01001641 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001642 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01001643 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001644 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04001645 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
1646 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01001647
Daniel Stone8c8164f2012-05-30 16:31:45 +01001648 /* Finally, notify the compositor that LEDs have changed. */
1649 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001650 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001651 leds |= LED_NUM_LOCK;
1652 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001653 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001654 leds |= LED_CAPS_LOCK;
1655 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001656 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001657 leds |= LED_SCROLL_LOCK;
1658 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01001659 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01001660 seat->xkb_state.leds = leds;
1661
Daniel Stone05d58682012-06-22 13:21:38 +01001662 if (changed) {
1663 grab->interface->modifiers(grab,
1664 serial,
1665 keyboard->modifiers.mods_depressed,
1666 keyboard->modifiers.mods_latched,
1667 keyboard->modifiers.mods_locked,
1668 keyboard->modifiers.group);
1669 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001670}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001671
Daniel Stone05d58682012-06-22 13:21:38 +01001672static void
1673update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001674 enum wl_keyboard_key_state state)
1675{
1676 enum xkb_key_direction direction;
1677
1678 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1679 direction = XKB_KEY_DOWN;
1680 else
1681 direction = XKB_KEY_UP;
1682
1683 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1684 * broken keycode system, which starts at 8. */
1685 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1686
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001687 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001688}
1689
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001690WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001691notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001692 enum wl_keyboard_key_state state,
1693 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001694{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001695 struct weston_compositor *compositor = seat->compositor;
1696 struct wl_keyboard *keyboard = seat->seat.keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01001697 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001698 (struct weston_surface *) keyboard->focus;
1699 struct wl_keyboard_grab *grab = keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001700 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001701 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001702
Daniel Stonec9785ea2012-05-30 16:31:52 +01001703 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001704 if (compositor->ping_handler && focus)
1705 compositor->ping_handler(focus, serial);
1706
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001707 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001708 keyboard->grab_key = key;
1709 keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07001710 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001711 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001712 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001713
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001714 end = keyboard->keys.data + keyboard->keys.size;
1715 for (k = keyboard->keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01001716 if (*k == key) {
1717 /* Ignore server-generated repeats. */
1718 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1719 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001720 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01001721 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001722 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001723 keyboard->keys.size = (void *) end - keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001724 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001725 k = wl_array_add(&keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001726 *k = key;
1727 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001728
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001729 if (grab == &keyboard->default_grab) {
1730 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001731 state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001732 grab = keyboard->grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06001733 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001734
Daniel Stone7ace3902012-05-30 16:31:40 +01001735 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01001736
1737 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001738 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01001739 wl_display_get_serial(compositor->wl_display),
1740 key,
1741 state);
1742 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001743}
1744
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001745WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001746notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01001747 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001748{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001749 struct weston_compositor *compositor = seat->compositor;
1750 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001751
1752 if (output) {
Daniel Stone37816df2012-05-16 18:45:18 +01001753 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001754 x - pointer->x,
1755 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001756
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001757 pointer->x = x;
1758 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001759 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001760 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001761 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001762 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03001763 /* FIXME: We should call wl_pointer_set_focus(seat,
1764 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001765 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001766}
1767
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001768static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001769destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001770{
Daniel Stone37816df2012-05-16 18:45:18 +01001771 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001772
Daniel Stone37816df2012-05-16 18:45:18 +01001773 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001774 saved_kbd_focus_listener);
1775
Daniel Stone37816df2012-05-16 18:45:18 +01001776 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001777}
1778
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001779WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001780notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001781 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001782{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001783 struct weston_compositor *compositor = seat->compositor;
1784 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001785 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001786 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001787
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001788 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001789 wl_array_copy(&keyboard->keys, keys);
1790 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001791 weston_compositor_idle_inhibit(compositor);
1792 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001793 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001794 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001795 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001796
Daniel Stoneef172672012-06-22 13:21:32 +01001797 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001798 wl_array_for_each(k, &keyboard->keys) {
1799 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01001800 WL_KEYBOARD_KEY_STATE_PRESSED);
1801 }
1802
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001803 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001804
1805 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001806 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1807 wl_keyboard_set_focus(keyboard, surface);
1808 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001809 }
1810}
1811
1812WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001813notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01001814{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001815 struct weston_compositor *compositor = seat->compositor;
1816 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001817 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001818
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001819 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001820 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001821 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001822 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001823 WL_KEYBOARD_KEY_STATE_RELEASED);
1824 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001825
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001826 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001827
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001828 if (keyboard->focus) {
1829 seat->saved_kbd_focus = keyboard->focus;
1830 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01001831 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001832 wl_signal_add(&keyboard->focus->resource.destroy_signal,
1833 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001834 }
1835
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001836 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001837 /* FIXME: We really need keyboard grab cancel here to
1838 * let the grab shut down properly. As it is we leak
1839 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001840 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001841}
1842
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001843static void
Daniel Stone37816df2012-05-16 18:45:18 +01001844touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001845{
Daniel Stone37816df2012-05-16 18:45:18 +01001846 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001847 struct wl_resource *resource;
1848
Pekka Paalanen56464252012-07-31 13:21:08 +03001849 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001850 return;
1851
Pekka Paalanen56464252012-07-31 13:21:08 +03001852 if (seat->touch->focus_resource)
1853 wl_list_remove(&seat->touch->focus_listener.link);
1854 seat->touch->focus = NULL;
1855 seat->touch->focus_resource = NULL;
1856
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001857 if (surface) {
1858 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01001859 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04001860 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001861 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02001862 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001863 return;
1864 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001865
Daniel Stone37816df2012-05-16 18:45:18 +01001866 seat->touch->focus = surface;
1867 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03001868 wl_signal_add(&resource->destroy_signal,
1869 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001870 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001871}
1872
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001873/**
1874 * notify_touch - emulates button touches and notifies surfaces accordingly.
1875 *
1876 * It assumes always the correct cycle sequence until it gets here: touch_down
1877 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1878 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001879 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001880 */
1881WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001882notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001883 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001884{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001885 struct weston_compositor *ec = seat->compositor;
1886 struct wl_touch *touch = seat->seat.touch;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001887 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001888 wl_fixed_t sx, sy;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001889 uint32_t serial = 0;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001890
1891 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01001892 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001893 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001894
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001895 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001896
1897 /* the first finger down picks the surface, and all further go
1898 * to that surface for the remainder of the touch session i.e.
1899 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001900 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001901 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001902 touch_set_focus(seat, &es->surface);
1903 } else if (touch->focus) {
1904 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001905 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001906 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001907
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001908 if (touch->focus_resource && touch->focus)
1909 wl_touch_send_down(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001910 serial, time,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001911 &touch->focus->resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001912 touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001913 break;
Daniel Stone37816df2012-05-16 18:45:18 +01001914 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001915 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001916 if (!es)
1917 break;
1918
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001919 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001920 if (touch->focus_resource)
1921 wl_touch_send_motion(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001922 time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001923 break;
Daniel Stone37816df2012-05-16 18:45:18 +01001924 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001925 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001926 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001927
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001928 if (touch->focus_resource)
1929 wl_touch_send_up(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001930 serial, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001931 if (seat->num_tp == 0)
1932 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001933 break;
1934 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001935}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001936
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001937static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001938pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1939{
1940 struct weston_seat *seat = container_of(listener, struct weston_seat,
1941 sprite_destroy_listener);
1942
1943 seat->sprite = NULL;
1944}
1945
1946static void
1947pointer_cursor_surface_configure(struct weston_surface *es,
1948 int32_t dx, int32_t dy)
1949{
1950 struct weston_seat *seat = es->private;
1951 int x, y;
1952
1953 assert(es == seat->sprite);
1954
1955 seat->hotspot_x -= dx;
1956 seat->hotspot_y -= dy;
1957
1958 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
1959 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
1960
1961 weston_surface_configure(seat->sprite, x, y,
1962 es->buffer->width, es->buffer->height);
1963
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03001964 empty_region(&es->input);
1965
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001966 if (!weston_surface_is_mapped(es)) {
1967 wl_list_insert(&es->compositor->cursor_layer.surface_list,
1968 &es->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03001969 weston_surface_update_transform(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001970 }
1971}
1972
1973static void
1974pointer_unmap_sprite(struct weston_seat *seat)
1975{
1976 if (weston_surface_is_mapped(seat->sprite))
1977 weston_surface_unmap(seat->sprite);
1978
1979 wl_list_remove(&seat->sprite_destroy_listener.link);
1980 seat->sprite->configure = NULL;
1981 seat->sprite->private = NULL;
1982 seat->sprite = NULL;
1983}
1984
1985static void
1986pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1987 uint32_t serial, struct wl_resource *surface_resource,
1988 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001989{
Daniel Stone37816df2012-05-16 18:45:18 +01001990 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001991 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001992
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001993 if (surface_resource)
1994 surface = container_of(surface_resource->data,
1995 struct weston_surface, surface);
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04001996
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04001997 if (seat->seat.pointer->focus == NULL)
1998 return;
1999 if (seat->seat.pointer->focus->resource.client != client)
2000 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002001 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002002 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002003
2004 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002005 if (surface->configure) {
2006 wl_resource_post_error(&surface->surface.resource,
2007 WL_DISPLAY_ERROR_INVALID_OBJECT,
2008 "surface->configure already "
2009 "set");
2010 return;
2011 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002012 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002013
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002014 if (seat->sprite)
2015 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002016
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002017 if (!surface)
2018 return;
2019
2020 wl_signal_add(&surface->surface.resource.destroy_signal,
2021 &seat->sprite_destroy_listener);
2022
2023 surface->configure = pointer_cursor_surface_configure;
2024 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002025 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002026 seat->hotspot_x = x;
2027 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002028
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04002029 if (surface->buffer)
2030 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002031}
2032
Daniel Stone37816df2012-05-16 18:45:18 +01002033static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002034 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002035};
2036
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002037static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002038handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002039{
Daniel Stone37816df2012-05-16 18:45:18 +01002040 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002041
Daniel Stone37816df2012-05-16 18:45:18 +01002042 seat = container_of(listener, struct weston_seat,
2043 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002044
Daniel Stone37816df2012-05-16 18:45:18 +01002045 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002046}
2047
Casey Dahlin9074db52012-04-19 22:50:09 -04002048static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002049{
2050 wl_list_remove(&resource->link);
2051 free(resource);
2052}
2053
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002054static void
Daniel Stone37816df2012-05-16 18:45:18 +01002055seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2056 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002057{
Daniel Stone37816df2012-05-16 18:45:18 +01002058 struct weston_seat *seat = resource->data;
2059 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002060
Daniel Stone37816df2012-05-16 18:45:18 +01002061 if (!seat->seat.pointer)
2062 return;
2063
2064 cr = wl_client_add_object(client, &wl_pointer_interface,
2065 &pointer_interface, id, seat);
2066 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002067 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002068
2069 if (seat->seat.pointer->focus &&
2070 seat->seat.pointer->focus->resource.client == client) {
2071 struct weston_surface *surface;
2072 wl_fixed_t sx, sy;
2073
2074 surface = (struct weston_surface *) seat->seat.pointer->focus;
2075 weston_surface_from_global_fixed(surface,
2076 seat->seat.pointer->x,
2077 seat->seat.pointer->y,
2078 &sx,
2079 &sy);
2080 wl_pointer_set_focus(seat->seat.pointer,
2081 seat->seat.pointer->focus,
2082 sx,
2083 sy);
2084 }
Daniel Stone37816df2012-05-16 18:45:18 +01002085}
2086
2087static void
2088seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2089 uint32_t id)
2090{
2091 struct weston_seat *seat = resource->data;
2092 struct wl_resource *cr;
2093
2094 if (!seat->seat.keyboard)
2095 return;
2096
2097 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2098 seat);
2099 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002100 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002101
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002102 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2103 seat->xkb_info.keymap_fd,
2104 seat->xkb_info.keymap_size);
2105
Daniel Stone17b13bd2012-05-30 16:31:39 +01002106 if (seat->seat.keyboard->focus &&
2107 seat->seat.keyboard->focus->resource.client == client) {
2108 wl_keyboard_set_focus(seat->seat.keyboard,
2109 seat->seat.keyboard->focus);
2110 }
Daniel Stone37816df2012-05-16 18:45:18 +01002111}
2112
2113static void
2114seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2115 uint32_t id)
2116{
2117 struct weston_seat *seat = resource->data;
2118 struct wl_resource *cr;
2119
2120 if (!seat->seat.touch)
2121 return;
2122
2123 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2124 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002125 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002126}
2127
2128static const struct wl_seat_interface seat_interface = {
2129 seat_get_pointer,
2130 seat_get_keyboard,
2131 seat_get_touch,
2132};
2133
2134static void
2135bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2136{
2137 struct wl_seat *seat = data;
2138 struct wl_resource *resource;
2139 enum wl_seat_capability caps = 0;
2140
2141 resource = wl_client_add_object(client, &wl_seat_interface,
2142 &seat_interface, id, data);
2143 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002144 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002145
2146 if (seat->pointer)
2147 caps |= WL_SEAT_CAPABILITY_POINTER;
2148 if (seat->keyboard)
2149 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2150 if (seat->touch)
2151 caps |= WL_SEAT_CAPABILITY_TOUCH;
2152
2153 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002154}
2155
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002156static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002157device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002158{
Daniel Stone37816df2012-05-16 18:45:18 +01002159 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002160
Daniel Stone37816df2012-05-16 18:45:18 +01002161 seat = container_of(listener, struct weston_seat,
2162 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002163
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002164 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002165}
2166
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002167static void weston_compositor_xkb_init(struct weston_compositor *ec,
2168 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002169{
Daniel Stonee379da92012-05-30 16:32:04 +01002170 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002171 ec->xkb_context = xkb_context_new(0);
2172 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002173 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002174 exit(1);
2175 }
Daniel Stone994679a2012-05-30 16:31:43 +01002176 }
2177
2178 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002179 ec->xkb_names = *names;
2180 if (!ec->xkb_names.rules)
2181 ec->xkb_names.rules = strdup("evdev");
2182 if (!ec->xkb_names.model)
2183 ec->xkb_names.model = strdup("pc105");
2184 if (!ec->xkb_names.layout)
2185 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002186}
2187
Daniel Stonee379da92012-05-30 16:32:04 +01002188static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2189{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002190 if (xkb_info->keymap)
2191 xkb_map_unref(xkb_info->keymap);
2192
2193 if (xkb_info->keymap_area)
2194 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2195 if (xkb_info->keymap_fd >= 0)
2196 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002197}
2198
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002199static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2200{
Daniel Stonee379da92012-05-30 16:32:04 +01002201 free((char *) ec->xkb_names.rules);
2202 free((char *) ec->xkb_names.model);
2203 free((char *) ec->xkb_names.layout);
2204 free((char *) ec->xkb_names.variant);
2205 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002206
Daniel Stonee379da92012-05-30 16:32:04 +01002207 xkb_info_destroy(&ec->xkb_info);
2208 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002209}
2210
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002211static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002212weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002213{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002214 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002215
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002216 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2217 XKB_MOD_NAME_SHIFT);
2218 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2219 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002220 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2221 XKB_MOD_NAME_CTRL);
2222 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2223 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002224 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2225 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002226 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2227 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002228 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002229
2230 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2231 XKB_LED_NAME_NUM);
2232 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2233 XKB_LED_NAME_CAPS);
2234 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2235 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002236
2237 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2238 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002239 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002240 exit(EXIT_FAILURE);
2241 }
2242 xkb_info->keymap_size = strlen(keymap_str) + 1;
2243
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002244 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002245 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002246 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002247 (unsigned long) xkb_info->keymap_size);
2248 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002249 }
2250
2251 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2252 PROT_READ | PROT_WRITE,
2253 MAP_SHARED, xkb_info->keymap_fd, 0);
2254 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002255 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002256 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002257 goto err_dev_zero;
2258 }
2259 strcpy(xkb_info->keymap_area, keymap_str);
2260 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002261
2262 return;
2263
2264err_dev_zero:
2265 close(xkb_info->keymap_fd);
2266 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002267err_keymap_str:
2268 free(keymap_str);
2269 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002270}
2271
2272static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002273weston_compositor_build_global_keymap(struct weston_compositor *ec)
2274{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002275 if (ec->xkb_info.keymap != NULL)
2276 return;
2277
Daniel Stonee379da92012-05-30 16:32:04 +01002278 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002279 &ec->xkb_names,
2280 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002281 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002282 weston_log("failed to compile global XKB keymap\n");
2283 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002284 "options %s",
2285 ec->xkb_names.rules, ec->xkb_names.model,
2286 ec->xkb_names.layout, ec->xkb_names.variant,
2287 ec->xkb_names.options);
2288 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002289 }
2290
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002291 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002292}
2293
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002294WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002295weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002296{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002297 if (seat->has_keyboard)
2298 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002299
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002300 if (keymap != NULL) {
2301 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002302 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002303 }
2304 else {
2305 weston_compositor_build_global_keymap(seat->compositor);
2306 seat->xkb_info = seat->compositor->xkb_info;
2307 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2308 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002309
2310 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2311 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002312 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01002313 exit(1);
2314 }
2315
Daniel Stone71c38772012-06-22 13:21:30 +01002316 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002317
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002318 wl_keyboard_init(&seat->keyboard);
2319 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2320
2321 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01002322}
2323
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002324WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002325weston_seat_init_pointer(struct weston_seat *seat)
2326{
2327 if (seat->has_pointer)
2328 return;
2329
2330 wl_pointer_init(&seat->pointer);
2331 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2332
2333 seat->has_pointer = 1;
2334}
2335
2336WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002337weston_seat_init_touch(struct weston_seat *seat)
2338{
2339 if (seat->has_touch)
2340 return;
2341
2342 wl_touch_init(&seat->touch);
2343 wl_seat_set_touch(&seat->seat, &seat->touch);
2344
2345 seat->has_touch = 1;
2346}
2347
2348WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002349weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002350{
Daniel Stone37816df2012-05-16 18:45:18 +01002351 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002352 seat->has_pointer = 0;
2353 seat->has_keyboard = 0;
2354 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002355
Daniel Stone37816df2012-05-16 18:45:18 +01002356 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2357 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002358
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002359 seat->sprite = NULL;
2360 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002361
Daniel Stone37816df2012-05-16 18:45:18 +01002362 seat->compositor = ec;
2363 seat->hotspot_x = 16;
2364 seat->hotspot_y = 16;
2365 seat->modifier_state = 0;
2366 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002367
Daniel Stone37816df2012-05-16 18:45:18 +01002368 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002369 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002370
Daniel Stone37816df2012-05-16 18:45:18 +01002371 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002372
Daniel Stone37816df2012-05-16 18:45:18 +01002373 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2374 wl_signal_add(&seat->seat.drag_icon_signal,
2375 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002376
2377 clipboard_create(seat);
Jan Arne Petersene829adc2012-08-10 16:47:22 +02002378 input_method_create(ec, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002379}
2380
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002381WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002382weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002383{
Daniel Stone37816df2012-05-16 18:45:18 +01002384 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002385 /* The global object is destroyed at wl_display_destroy() time. */
2386
Daniel Stone37816df2012-05-16 18:45:18 +01002387 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002388 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002389
Daniel Stone74419a22012-05-30 16:32:02 +01002390 if (seat->xkb_state.state != NULL)
2391 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01002392 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002393
Daniel Stone37816df2012-05-16 18:45:18 +01002394 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002395}
2396
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002397static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002398drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2399{
2400 weston_surface_configure(es,
2401 es->geometry.x + sx, es->geometry.y + sy,
2402 es->buffer->width, es->buffer->height);
2403}
2404
2405static int
Daniel Stone37816df2012-05-16 18:45:18 +01002406device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002407 struct weston_surface *surface)
2408{
Daniel Stone37816df2012-05-16 18:45:18 +01002409 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002410
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002411 if (surface->configure) {
2412 wl_resource_post_error(&surface->surface.resource,
2413 WL_DISPLAY_ERROR_INVALID_OBJECT,
2414 "surface->configure already set");
2415 return 0;
2416 }
2417
Daniel Stone37816df2012-05-16 18:45:18 +01002418 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002419
Daniel Stone37816df2012-05-16 18:45:18 +01002420 weston_surface_set_position(ws->drag_surface,
2421 wl_fixed_to_double(seat->pointer->x),
2422 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002423
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002424 surface->configure = drag_surface_configure;
2425
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002426 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002427 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002428
2429 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002430}
2431
2432static void
Daniel Stone37816df2012-05-16 18:45:18 +01002433device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002434{
Daniel Stone37816df2012-05-16 18:45:18 +01002435 seat->drag_surface->configure = NULL;
2436 undef_region(&seat->drag_surface->input);
2437 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2438 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002439}
2440
2441static void
Daniel Stone37816df2012-05-16 18:45:18 +01002442device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002443{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002444 struct wl_list *list;
2445
Daniel Stone37816df2012-05-16 18:45:18 +01002446 if (weston_surface_is_mapped(seat->drag_surface) ||
2447 !seat->drag_surface->buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002448 return;
2449
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002450 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2451 list = &seat->sprite->layer_link;
2452 else
2453 list = &seat->compositor->cursor_layer.surface_list;
2454
2455 wl_list_insert(list, &seat->drag_surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002456 weston_surface_update_transform(seat->drag_surface);
Daniel Stone37816df2012-05-16 18:45:18 +01002457 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002458}
2459
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002460static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002461weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01002462 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002463{
2464 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002465
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002466 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002467 return;
2468
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002469 if (seat->drag_surface && seat->seat.drag_surface &&
2470 (&seat->drag_surface->surface.resource !=
2471 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002472 /* between calls to this funcion we got a new drag_surface */
2473 surface_changed = 1;
2474
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002475 if (!seat->seat.drag_surface || surface_changed) {
2476 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002477 if (!surface_changed)
2478 return;
2479 }
2480
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002481 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002482 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002483 seat->seat.drag_surface;
2484 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002485 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002486 }
2487
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002488 /* the client may not have attached a buffer to the drag surface
2489 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002490 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002491
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02002492 /* the client may have attached a buffer with a different size to
2493 * the drag surface, causing the input region to be reset */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002494 if (region_is_undefined(&seat->drag_surface->input))
2495 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02002496
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002497 if (!dx && !dy)
2498 return;
2499
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002500 weston_surface_set_position(seat->drag_surface,
2501 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
2502 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002503}
2504
2505WL_EXPORT void
2506weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2507{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002508 struct weston_seat *seat;
2509
2510 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002511 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002512}
2513
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002514static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002515bind_output(struct wl_client *client,
2516 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002517{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002518 struct weston_output *output = data;
2519 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002520 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002521
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002522 resource = wl_client_add_object(client,
2523 &wl_output_interface, NULL, id, data);
2524
Casey Dahlin9074db52012-04-19 22:50:09 -04002525 wl_list_insert(&output->resource_list, &resource->link);
2526 resource->destroy = unbind_resource;
2527
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002528 wl_output_send_geometry(resource,
2529 output->x,
2530 output->y,
2531 output->mm_width,
2532 output->mm_height,
2533 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002534 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002535 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002536
2537 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002538 wl_output_send_mode(resource,
2539 mode->flags,
2540 mode->width,
2541 mode->height,
2542 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002543 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002544}
2545
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002546WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002547weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002548{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002549 struct weston_compositor *c = output->compositor;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002550 int i;
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002551
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002552 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002553
2554 for (i = 0; i < 2; i++)
2555 pixman_region32_fini(&output->buffer_damage[i]);
2556
Casey Dahlin9074db52012-04-19 22:50:09 -04002557 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002558
2559 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002560}
2561
Scott Moreau1bad5db2012-08-18 01:04:05 -06002562static void
2563weston_output_compute_transform(struct weston_output *output)
2564{
2565 struct weston_matrix transform;
2566 int flip;
2567
2568 weston_matrix_init(&transform);
2569
2570 switch(output->transform) {
2571 case WL_OUTPUT_TRANSFORM_FLIPPED:
2572 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2573 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2574 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2575 flip = -1;
2576 break;
2577 default:
2578 flip = 1;
2579 break;
2580 }
2581
2582 switch(output->transform) {
2583 case WL_OUTPUT_TRANSFORM_NORMAL:
2584 case WL_OUTPUT_TRANSFORM_FLIPPED:
2585 transform.d[0] = flip;
2586 transform.d[1] = 0;
2587 transform.d[4] = 0;
2588 transform.d[5] = 1;
2589 break;
2590 case WL_OUTPUT_TRANSFORM_90:
2591 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2592 transform.d[0] = 0;
2593 transform.d[1] = -flip;
2594 transform.d[4] = 1;
2595 transform.d[5] = 0;
2596 break;
2597 case WL_OUTPUT_TRANSFORM_180:
2598 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2599 transform.d[0] = -flip;
2600 transform.d[1] = 0;
2601 transform.d[4] = 0;
2602 transform.d[5] = -1;
2603 break;
2604 case WL_OUTPUT_TRANSFORM_270:
2605 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2606 transform.d[0] = 0;
2607 transform.d[1] = flip;
2608 transform.d[4] = -1;
2609 transform.d[5] = 0;
2610 break;
2611 default:
2612 break;
2613 }
2614
2615 weston_matrix_multiply(&output->matrix, &transform);
2616}
2617
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002618WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002619weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002620{
Scott Moreau850ca422012-05-21 15:21:25 -06002621 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002622 struct weston_matrix camera;
2623 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002624
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002625 weston_matrix_init(&output->matrix);
2626 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002627 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2628 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002629
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002630 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002631 2.0 / (output->width + output->border.left + output->border.right),
2632 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2633
2634 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002635
Scott Moreauccbf29d2012-02-22 14:21:41 -07002636 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002637 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002638 weston_matrix_init(&camera);
2639 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002640 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002641 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002642 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002643 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002644 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002645 weston_matrix_multiply(&output->matrix, &modelview);
2646 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002647
Scott Moreauccbf29d2012-02-22 14:21:41 -07002648 output->dirty = 0;
2649}
2650
Scott Moreau1bad5db2012-08-18 01:04:05 -06002651static void
2652weston_output_transform_init(struct weston_output *output, uint32_t transform)
2653{
2654 output->transform = transform;
2655
2656 switch (transform) {
2657 case WL_OUTPUT_TRANSFORM_90:
2658 case WL_OUTPUT_TRANSFORM_270:
2659 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2660 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2661 /* Swap width and height */
2662 output->width = output->current->height;
2663 output->height = output->current->width;
2664 break;
2665 case WL_OUTPUT_TRANSFORM_NORMAL:
2666 case WL_OUTPUT_TRANSFORM_180:
2667 case WL_OUTPUT_TRANSFORM_FLIPPED:
2668 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2669 output->width = output->current->width;
2670 output->height = output->current->height;
2671 break;
2672 default:
2673 break;
2674 }
2675}
2676
Scott Moreauccbf29d2012-02-22 14:21:41 -07002677WL_EXPORT void
2678weston_output_move(struct weston_output *output, int x, int y)
2679{
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002680 int i;
2681
Scott Moreauccbf29d2012-02-22 14:21:41 -07002682 output->x = x;
2683 output->y = y;
2684
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002685 output->current_buffer = 0;
2686 for (i = 0; i < 2; i++)
2687 pixman_region32_init(&output->buffer_damage[i]);
2688
Scott Moreauccbf29d2012-02-22 14:21:41 -07002689 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002690 output->width,
2691 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002692}
2693
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002694WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002695weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002696 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002697{
2698 output->compositor = c;
2699 output->x = x;
2700 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002701 output->border.top = 0;
2702 output->border.bottom = 0;
2703 output->border.left = 0;
2704 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002705 output->mm_width = width;
2706 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002707 output->dirty = 1;
2708
Scott Moreau1bad5db2012-08-18 01:04:05 -06002709 if (transform != WL_OUTPUT_TRANSFORM_NORMAL)
2710 output->disable_planes++;
2711
2712 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06002713 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002714
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002715 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002716 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002717
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002718 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002719 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002720 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002721
Casey Dahlin58ba1372012-04-19 22:50:08 -04002722 output->id = ffs(~output->compositor->output_id_pool) - 1;
2723 output->compositor->output_id_pool |= 1 << output->id;
2724
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002725 output->global =
2726 wl_display_add_global(c->wl_display, &wl_output_interface,
2727 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002728}
2729
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002730static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002731compositor_bind(struct wl_client *client,
2732 void *data, uint32_t version, uint32_t id)
2733{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002734 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002735
2736 wl_client_add_object(client, &wl_compositor_interface,
2737 &compositor_interface, id, compositor);
2738}
2739
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002740static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002741log_uname(void)
2742{
2743 struct utsname usys;
2744
2745 uname(&usys);
2746
2747 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2748 usys.version, usys.machine);
2749}
2750
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002751WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002752weston_compositor_init(struct weston_compositor *ec,
2753 struct wl_display *display,
2754 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04002755 char *argv[],
2756 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002757{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002758 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002759 struct xkb_rule_names xkb_names;
2760 const struct config_key keyboard_config_keys[] = {
2761 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2762 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2763 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2764 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2765 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2766 };
2767 const struct config_section cs[] = {
2768 { "keyboard",
2769 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
2770 };
2771
2772 memset(&xkb_names, 0, sizeof(xkb_names));
2773 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002774
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002775 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002776 wl_signal_init(&ec->destroy_signal);
2777 wl_signal_init(&ec->activate_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002778 wl_signal_init(&ec->kill_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002779 wl_signal_init(&ec->lock_signal);
2780 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002781 wl_signal_init(&ec->show_input_panel_signal);
2782 wl_signal_init(&ec->hide_input_panel_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002783 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002784
Casey Dahlin58ba1372012-04-19 22:50:08 -04002785 ec->output_id_pool = 0;
2786
Kristian Høgsberga8873122011-11-23 10:39:34 -05002787 if (!wl_display_add_global(display, &wl_compositor_interface,
2788 ec, compositor_bind))
2789 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002790
Daniel Stone725c2c32012-06-22 14:04:36 +01002791 wl_list_init(&ec->surface_list);
2792 wl_list_init(&ec->layer_list);
2793 wl_list_init(&ec->seat_list);
2794 wl_list_init(&ec->output_list);
2795 wl_list_init(&ec->key_binding_list);
2796 wl_list_init(&ec->button_binding_list);
2797 wl_list_init(&ec->axis_binding_list);
2798 wl_list_init(&ec->fade.animation.link);
2799
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002800 weston_plane_init(&ec->primary_plane, 0, 0);
2801
Daniel Stone725c2c32012-06-22 14:04:36 +01002802 weston_compositor_xkb_init(ec, &xkb_names);
2803
2804 ec->ping_handler = NULL;
2805
2806 screenshooter_create(ec);
2807 text_cursor_position_notifier_create(ec);
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +02002808 text_model_factory_create(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002809
2810 wl_data_device_manager_init(ec->wl_display);
2811
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002812 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002813
Daniel Stone725c2c32012-06-22 14:04:36 +01002814 loop = wl_display_get_event_loop(ec->wl_display);
2815 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2816 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2817
2818 ec->input_loop = wl_event_loop_create();
2819
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002820 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
2821 ec->fade.animation.frame = fade_frame;
2822
2823 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2824 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2825
2826 weston_compositor_schedule_repaint(ec);
2827
Daniel Stone725c2c32012-06-22 14:04:36 +01002828 return 0;
2829}
2830
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002831WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002832weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002833{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002834 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002835
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002836 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002837 if (ec->input_loop_source)
2838 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002839
Matt Roper361d2ad2011-08-29 13:52:23 -07002840 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002841 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002842 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002843
Daniel Stone325fc2d2012-05-30 16:31:58 +01002844 weston_binding_list_destroy_all(&ec->key_binding_list);
2845 weston_binding_list_destroy_all(&ec->button_binding_list);
2846 weston_binding_list_destroy_all(&ec->axis_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002847
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002848 weston_plane_release(&ec->primary_plane);
2849
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002850 wl_array_release(&ec->vertices);
2851 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05002852 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002853
2854 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07002855}
2856
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002857static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002858{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002859 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002860
Martin Minarik6d118362012-06-07 18:01:59 +02002861 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002862 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002863
2864 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002865}
2866
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002867static void
2868on_segv_signal(int s, siginfo_t *siginfo, void *context)
2869{
2870 void *buffer[32];
2871 int i, count;
2872 Dl_info info;
2873
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002874 /* This SIGSEGV handler will do a best-effort backtrace, and
2875 * then call the backend restore function, which will switch
2876 * back to the vt we launched from or ungrab X etc and then
2877 * raise SIGTRAP. If we run weston under gdb from X or a
2878 * different vt, and tell gdb "handle SIGSEGV nostop", this
2879 * will allow weston to switch back to gdb on crash and then
2880 * gdb will catch the crash with SIGTRAP. */
2881
Martin Minarik6d118362012-06-07 18:01:59 +02002882 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002883
2884 count = backtrace(buffer, ARRAY_LENGTH(buffer));
2885 for (i = 0; i < count; i++) {
2886 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02002887 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002888 (long) buffer[i],
2889 info.dli_sname ? info.dli_sname : "--",
2890 info.dli_fname);
2891 }
2892
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002893 segv_compositor->restore(segv_compositor);
2894
2895 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002896}
2897
2898
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002899static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04002900load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002901{
2902 char path[PATH_MAX];
2903 void *module, *init;
2904
2905 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07002906 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002907 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02002908 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002909
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002910 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
2911 if (module) {
2912 weston_log("Module '%s' already loaded\n", path);
2913 dlclose(module);
2914 return NULL;
2915 }
2916
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002917 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04002918 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002919 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002920 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002921 return NULL;
2922 }
2923
2924 init = dlsym(module, entrypoint);
2925 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002926 weston_log("Failed to lookup init function: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002927 return NULL;
2928 }
2929
2930 return init;
2931}
2932
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002933static int
2934load_modules(struct weston_compositor *ec, const char *modules)
2935{
2936 const char *p, *end;
2937 char buffer[256];
2938 int (*module_init)(struct weston_compositor *ec);
2939
2940 if (modules == NULL)
2941 return 0;
2942
2943 p = modules;
2944 while (*p) {
2945 end = strchrnul(p, ',');
2946 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
2947 module_init = load_module(buffer, "module_init");
2948 if (module_init)
2949 module_init(ec);
2950 p = end;
2951 while (*p == ',')
2952 p++;
2953
2954 }
2955
2956 return 0;
2957}
2958
Pekka Paalanen78a0b572012-06-06 16:59:44 +03002959static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02002960 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
2961
2962static const char xdg_wrong_message[] =
2963 "fatal: environment variable XDG_RUNTIME_DIR\n"
2964 "is set to \"%s\", which is not a directory.\n";
2965
2966static const char xdg_wrong_mode_message[] =
2967 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
2968 "correctly. Unix access mode must be 0700 but is %o,\n"
2969 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04002970 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02002971
2972static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03002973 "Refer to your distribution on how to get it, or\n"
2974 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
2975 "on how to implement it.\n";
2976
Martin Minarik37032f82012-06-18 20:15:18 +02002977static void
2978verify_xdg_runtime_dir(void)
2979{
2980 char *dir = getenv("XDG_RUNTIME_DIR");
2981 struct stat s;
2982
2983 if (!dir) {
2984 weston_log(xdg_error_message);
2985 weston_log_continue(xdg_detail_message);
2986 exit(EXIT_FAILURE);
2987 }
2988
2989 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
2990 weston_log(xdg_wrong_message, dir);
2991 weston_log_continue(xdg_detail_message);
2992 exit(EXIT_FAILURE);
2993 }
2994
2995 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
2996 weston_log(xdg_wrong_mode_message,
2997 dir, s.st_mode & 0777, s.st_uid);
2998 weston_log_continue(xdg_detail_message);
2999 }
3000}
3001
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003002static int
3003usage(int error_code)
3004{
3005 fprintf(stderr,
3006 "Usage: weston [OPTIONS]\n\n"
3007 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3008 "Weston supports multiple backends, and depending on which backend is in use\n"
3009 "different options will be accepted.\n\n"
3010
3011
3012 "Core options:\n\n"
3013 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3014 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3015 " -S, --socket=NAME\tName of socket to listen on\n"
3016 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003017 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003018 " --log==FILE\t\tLog to the given file\n"
3019 " -h, --help\t\tThis help message\n\n");
3020
3021 fprintf(stderr,
3022 "Options for drm-backend.so:\n\n"
3023 " --connector=ID\tBring up only this connector\n"
3024 " --seat=SEAT\t\tThe seat that weston should run on\n"
3025 " --tty=TTY\t\tThe tty to use\n"
3026 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3027
3028 fprintf(stderr,
3029 "Options for x11-backend.so:\n\n"
3030 " --width=WIDTH\t\tWidth of X window\n"
3031 " --height=HEIGHT\tHeight of X window\n"
3032 " --fullscreen\t\tRun in fullscreen mode\n"
3033 " --output-count=COUNT\tCreate multiple outputs\n"
3034 " --no-input\t\tDont create input devices\n\n");
3035
3036 fprintf(stderr,
3037 "Options for wayland-backend.so:\n\n"
3038 " --width=WIDTH\t\tWidth of Wayland surface\n"
3039 " --height=HEIGHT\tHeight of Wayland surface\n"
3040 " --display=DISPLAY\tWayland display to connect to\n\n");
3041
3042 exit(error_code);
3043}
3044
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003045int main(int argc, char *argv[])
3046{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003047 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003048 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003049 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003050 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003051 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003052 struct sigaction segv_action;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003053 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003054 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003055 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003056 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003057 char *backend = NULL;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003058 const char *modules = "desktop-shell.so", *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003059 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003060 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003061 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003062 char *socket_name = "wayland-0";
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003063 char *config_file;
3064
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003065 const struct config_key core_config_keys[] = {
3066 { "modules", CONFIG_KEY_STRING, &modules },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003067 };
3068
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003069 const struct config_section cs[] = {
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003070 { "core",
3071 core_config_keys, ARRAY_LENGTH(core_config_keys) },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003072 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003073
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003074 const struct weston_option core_options[] = {
3075 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3076 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3077 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003078 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003079 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003080 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003081 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003082
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003083 argc = parse_options(core_options,
3084 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003085
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003086 if (help)
3087 usage(EXIT_SUCCESS);
3088
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003089 weston_log_file_open(log);
3090
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003091 weston_log("%s\n"
3092 STAMP_SPACE "%s\n"
3093 STAMP_SPACE "Bug reports to: %s\n"
3094 STAMP_SPACE "Build: %s\n",
3095 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003096 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003097 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003098
Martin Minarik37032f82012-06-18 20:15:18 +02003099 verify_xdg_runtime_dir();
3100
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003101 display = wl_display_create();
3102
Tiago Vignatti2116b892011-08-08 05:52:59 -07003103 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003104 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3105 display);
3106 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3107 display);
3108 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3109 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003110
3111 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003112 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3113 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003114
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003115 if (!backend) {
3116 if (getenv("WAYLAND_DISPLAY"))
3117 backend = "wayland-backend.so";
3118 else if (getenv("DISPLAY"))
3119 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003120 else
3121 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003122 }
3123
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003124 config_file = config_file_path("weston.ini");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003125 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003126
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003127 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003128 if (!backend_init)
3129 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003130
Daniel Stonec1be8e52012-06-01 11:14:02 -04003131 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003132 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003133 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003134 exit(EXIT_FAILURE);
3135 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003136
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003137 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3138 segv_action.sa_sigaction = on_segv_signal;
3139 sigemptyset(&segv_action.sa_mask);
3140 sigaction(SIGSEGV, &segv_action, NULL);
3141 segv_compositor = ec;
3142
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003143 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03003144 weston_log("fatal: unhandled option: %s\n", argv[i]);
3145 if (argv[1]) {
3146 ret = EXIT_FAILURE;
3147 goto out;
3148 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003149
Daniel Stonec1be8e52012-06-01 11:14:02 -04003150 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01003151
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003152 ec->option_idle_time = idle_time;
3153 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003154
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003155 setenv("WAYLAND_DISPLAY", socket_name, 1);
3156
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003157 if (load_modules(ec, modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003158 goto out;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003159 if (load_modules(ec, option_modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003160 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003161
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003162 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003163 weston_log("fatal: failed to add socket: %m\n");
3164 ret = EXIT_FAILURE;
3165 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003166 }
3167
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003168 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003169 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003170
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003171 wl_display_run(display);
3172
3173 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003174 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05003175 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003176
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003177 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003178
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003179 for (i = ARRAY_LENGTH(signals); i;)
3180 wl_event_source_remove(signals[--i]);
3181
Daniel Stone855028f2012-05-01 20:37:10 +01003182 weston_compositor_xkb_destroy(ec);
3183
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003184 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003185 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003186
Martin Minarik19e6f262012-06-07 13:08:46 +02003187 weston_log_file_close();
3188
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003189 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003190}