blob: 417c5083453bb2ea592fe43189aebe6fd3fec4de [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
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500196static const pixman_region32_data_t undef_region_data;
197
198static void
199undef_region(pixman_region32_t *region)
200{
Kristian Høgsberg66a099b2012-05-29 16:49:45 -0400201 if (region->data != &undef_region_data)
202 pixman_region32_fini(region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500203 region->data = (pixman_region32_data_t *) &undef_region_data;
204}
205
206static int
207region_is_undefined(pixman_region32_t *region)
208{
209 return region->data == &undef_region_data;
210}
211
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200212static void
213empty_region(pixman_region32_t *region)
214{
215 if (!region_is_undefined(region))
216 pixman_region32_fini(region);
217
218 pixman_region32_init(region);
219}
220
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500221WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500222weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500223{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500224 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400225
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200226 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400227 if (surface == NULL)
228 return NULL;
229
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400230 wl_signal_init(&surface->surface.resource.destroy_signal);
Kristian Høgsberg48891542012-02-23 17:38:33 -0500231
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500232 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500233 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500234
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400235 surface->surface.resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400236
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500237 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400238 surface->alpha = 1.0;
Juan Zhao46436612012-03-20 01:48:46 +0800239 surface->pitch = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400240
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200241 surface->num_textures = 0;
242 surface->num_images = 0;
243
Benjamin Franzke06286262011-05-06 19:12:33 +0200244 surface->buffer = NULL;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400245 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400246 surface->plane = &compositor->primary_plane;
Benjamin Franzke06286262011-05-06 19:12:33 +0200247
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400248 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500249 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500250 pixman_region32_init(&surface->clip);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500251 undef_region(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200252 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500253 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400254
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400255 surface->buffer_destroy_listener.notify =
256 surface_handle_buffer_destroy;
Benjamin Franzke06286262011-05-06 19:12:33 +0200257
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200258 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200259 wl_list_insert(&surface->geometry.transformation_list,
260 &surface->transform.position.link);
261 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200262 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200263 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500264
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400265 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500266}
267
Alex Wu8811bf92012-02-28 18:07:54 +0800268WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500269weston_surface_set_color(struct weston_surface *surface,
270 GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
271{
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500272 surface->color[0] = red;
273 surface->color[1] = green;
274 surface->color[2] = blue;
275 surface->color[3] = alpha;
276 surface->shader = &surface->compositor->solid_shader;
277}
278
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400279WL_EXPORT void
280weston_surface_to_global_float(struct weston_surface *surface,
281 GLfloat sx, GLfloat sy, GLfloat *x, GLfloat *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200282{
283 if (surface->transform.enabled) {
284 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
285
286 weston_matrix_transform(&surface->transform.matrix, &v);
287
288 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200289 weston_log("warning: numerical instability in "
Pekka Paalanenece8a012012-02-08 15:23:15 +0200290 "weston_surface_to_global(), divisor = %g\n",
291 v.f[3]);
292 *x = 0;
293 *y = 0;
294 return;
295 }
296
297 *x = v.f[0] / v.f[3];
298 *y = v.f[1] / v.f[3];
299 } else {
300 *x = sx + surface->geometry.x;
301 *y = sy + surface->geometry.y;
302 }
303}
304
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500305WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400306weston_surface_move_to_plane(struct weston_surface *surface,
307 struct weston_plane *plane)
308{
309 if (surface->plane == plane)
310 return;
311
312 weston_surface_damage_below(surface);
313 surface->plane = plane;
314 weston_surface_damage(surface);
315}
316
317WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500318weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200319{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500320 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200321
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500322 pixman_region32_init(&damage);
323 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
324 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400325 pixman_region32_union(&surface->plane->damage,
326 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500327 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200328}
329
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400330static struct wl_resource *
331find_resource_for_client(struct wl_list *list, struct wl_client *client)
332{
333 struct wl_resource *r;
334
335 wl_list_for_each(r, list, link) {
336 if (r->client == client)
337 return r;
338 }
339
340 return NULL;
341}
342
343static void
344weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
345{
346 uint32_t different = es->output_mask ^ mask;
347 uint32_t entered = mask & different;
348 uint32_t left = es->output_mask & different;
349 struct weston_output *output;
350 struct wl_resource *resource = NULL;
351 struct wl_client *client = es->surface.resource.client;
352
353 es->output_mask = mask;
354 if (es->surface.resource.client == NULL)
355 return;
356 if (different == 0)
357 return;
358
359 wl_list_for_each(output, &es->compositor->output_list, link) {
360 if (1 << output->id & different)
361 resource =
362 find_resource_for_client(&output->resource_list,
363 client);
364 if (resource == NULL)
365 continue;
366 if (1 << output->id & entered)
367 wl_surface_send_enter(&es->surface.resource, resource);
368 if (1 << output->id & left)
369 wl_surface_send_leave(&es->surface.resource, resource);
370 }
371}
372
373static void
374weston_surface_assign_output(struct weston_surface *es)
375{
376 struct weston_compositor *ec = es->compositor;
377 struct weston_output *output, *new_output;
378 pixman_region32_t region;
379 uint32_t max, area, mask;
380 pixman_box32_t *e;
381
382 new_output = NULL;
383 max = 0;
384 mask = 0;
385 pixman_region32_init(&region);
386 wl_list_for_each(output, &ec->output_list, link) {
387 pixman_region32_intersect(&region, &es->transform.boundingbox,
388 &output->region);
389
390 e = pixman_region32_extents(&region);
391 area = (e->x2 - e->x1) * (e->y2 - e->y1);
392
393 if (area > 0)
394 mask |= 1 << output->id;
395
396 if (area >= max) {
397 new_output = output;
398 max = area;
399 }
400 }
401 pixman_region32_fini(&region);
402
403 es->output = new_output;
404 weston_surface_update_output_mask(es, mask);
405}
406
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200407static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200408surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
409 int32_t width, int32_t height,
410 pixman_region32_t *bbox)
411{
Pekka Paalanen219b9822012-02-08 15:38:37 +0200412 GLfloat min_x = HUGE_VALF, min_y = HUGE_VALF;
413 GLfloat max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200414 int32_t s[4][2] = {
415 { sx, sy },
416 { sx, sy + height },
417 { sx + width, sy },
418 { sx + width, sy + height }
419 };
Pekka Paalanen219b9822012-02-08 15:38:37 +0200420 GLfloat int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200421 int i;
422
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300423 if (width == 0 || height == 0) {
424 /* avoid rounding empty bbox to 1x1 */
425 pixman_region32_init(bbox);
426 return;
427 }
428
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200429 for (i = 0; i < 4; ++i) {
Pekka Paalanen219b9822012-02-08 15:38:37 +0200430 GLfloat x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400431 weston_surface_to_global_float(surface,
432 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200433 if (x < min_x)
434 min_x = x;
435 if (x > max_x)
436 max_x = x;
437 if (y < min_y)
438 min_y = y;
439 if (y > max_y)
440 max_y = y;
441 }
442
Pekka Paalanen219b9822012-02-08 15:38:37 +0200443 int_x = floorf(min_x);
444 int_y = floorf(min_y);
445 pixman_region32_init_rect(bbox, int_x, int_y,
446 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200447}
448
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200449static void
450weston_surface_update_transform_disable(struct weston_surface *surface)
451{
452 surface->transform.enabled = 0;
453
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200454 /* round off fractions when not transformed */
455 surface->geometry.x = roundf(surface->geometry.x);
456 surface->geometry.y = roundf(surface->geometry.y);
457
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200458 pixman_region32_init_rect(&surface->transform.boundingbox,
459 surface->geometry.x,
460 surface->geometry.y,
461 surface->geometry.width,
462 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500463
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400464 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500465 pixman_region32_copy(&surface->transform.opaque,
466 &surface->opaque);
467 pixman_region32_translate(&surface->transform.opaque,
468 surface->geometry.x,
469 surface->geometry.y);
470 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200471}
472
473static int
474weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200475{
476 struct weston_matrix *matrix = &surface->transform.matrix;
477 struct weston_matrix *inverse = &surface->transform.inverse;
478 struct weston_transform *tform;
479
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200480 surface->transform.enabled = 1;
481
482 /* Otherwise identity matrix, but with x and y translation. */
483 surface->transform.position.matrix.d[12] = surface->geometry.x;
484 surface->transform.position.matrix.d[13] = surface->geometry.y;
485
486 weston_matrix_init(matrix);
487 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
488 weston_matrix_multiply(matrix, &tform->matrix);
489
490 if (weston_matrix_invert(inverse, matrix) < 0) {
491 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200492 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200493 " transformation not invertible.\n", surface);
494 return -1;
495 }
496
497 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
498 surface->geometry.height,
499 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500500
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200501 return 0;
502}
503
504WL_EXPORT void
505weston_surface_update_transform(struct weston_surface *surface)
506{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200507 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200508 return;
509
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200510 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200511
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500512 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200513
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200514 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500515 pixman_region32_fini(&surface->transform.opaque);
516 pixman_region32_init(&surface->transform.opaque);
517
518 if (region_is_undefined(&surface->input))
519 pixman_region32_init_rect(&surface->input, 0, 0,
520 surface->geometry.width,
521 surface->geometry.height);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200522
Pekka Paalanencd403622012-01-25 13:37:39 +0200523 /* transform.position is always in transformation_list */
524 if (surface->geometry.transformation_list.next ==
525 &surface->transform.position.link &&
526 surface->geometry.transformation_list.prev ==
527 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200528 weston_surface_update_transform_disable(surface);
529 } else {
530 if (weston_surface_update_transform_enable(surface) < 0)
531 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200532 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200533
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400534 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200535
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300536 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200537}
538
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200539WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100540weston_surface_to_global_fixed(struct weston_surface *surface,
541 wl_fixed_t sx, wl_fixed_t sy,
542 wl_fixed_t *x, wl_fixed_t *y)
543{
544 GLfloat xf, yf;
545
546 weston_surface_to_global_float(surface,
547 wl_fixed_to_double(sx),
548 wl_fixed_to_double(sy),
549 &xf, &yf);
550 *x = wl_fixed_from_double(xf);
551 *y = wl_fixed_from_double(yf);
552}
553
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400554WL_EXPORT void
555weston_surface_from_global_float(struct weston_surface *surface,
556 GLfloat x, GLfloat y, GLfloat *sx, GLfloat *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200557{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200558 if (surface->transform.enabled) {
559 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
560
561 weston_matrix_transform(&surface->transform.inverse, &v);
562
563 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200564 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200565 "weston_surface_from_global(), divisor = %g\n",
566 v.f[3]);
567 *sx = 0;
568 *sy = 0;
569 return;
570 }
571
Pekka Paalanencd403622012-01-25 13:37:39 +0200572 *sx = v.f[0] / v.f[3];
573 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200574 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200575 *sx = x - surface->geometry.x;
576 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200577 }
578}
579
580WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100581weston_surface_from_global_fixed(struct weston_surface *surface,
582 wl_fixed_t x, wl_fixed_t y,
583 wl_fixed_t *sx, wl_fixed_t *sy)
584{
585 GLfloat sxf, syf;
586
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400587 weston_surface_from_global_float(surface,
588 wl_fixed_to_double(x),
589 wl_fixed_to_double(y),
590 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100591 *sx = wl_fixed_from_double(sxf);
592 *sy = wl_fixed_from_double(syf);
593}
594
595WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200596weston_surface_from_global(struct weston_surface *surface,
597 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
598{
599 GLfloat sxf, syf;
600
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400601 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200602 *sx = floorf(sxf);
603 *sy = floorf(syf);
604}
605
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400606WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400607weston_surface_schedule_repaint(struct weston_surface *surface)
608{
609 struct weston_output *output;
610
611 wl_list_for_each(output, &surface->compositor->output_list, link)
612 if (surface->output_mask & (1 << output->id))
613 weston_output_schedule_repaint(output);
614}
615
616WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500617weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500618{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400619 pixman_region32_union_rect(&surface->damage, &surface->damage,
620 0, 0, surface->geometry.width,
621 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200622
Kristian Høgsberg98238702012-08-03 16:29:12 -0400623 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500624}
625
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400626WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500627weston_surface_configure(struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200628 GLfloat x, GLfloat y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400629{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200630 surface->geometry.x = x;
631 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200632 surface->geometry.width = width;
633 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200634 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400635}
636
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200637WL_EXPORT void
638weston_surface_set_position(struct weston_surface *surface,
639 GLfloat x, GLfloat y)
640{
641 surface->geometry.x = x;
642 surface->geometry.y = y;
643 surface->geometry.dirty = 1;
644}
645
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300646WL_EXPORT int
647weston_surface_is_mapped(struct weston_surface *surface)
648{
649 if (surface->output)
650 return 1;
651 else
652 return 0;
653}
654
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400655WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500656weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500657{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400658 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500659
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400660 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500661
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400662 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500663}
664
Tiago Vignatti9d393522012-02-10 16:26:19 +0200665static struct weston_surface *
666weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100667 wl_fixed_t x, wl_fixed_t y,
668 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200669{
670 struct weston_surface *surface;
671
672 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100673 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500674 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100675 wl_fixed_to_int(*sx),
676 wl_fixed_to_int(*sy),
677 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200678 return surface;
679 }
680
681 return NULL;
682}
683
684static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400685weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500686{
Scott Moreau447013d2012-02-18 05:05:29 -0700687 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500688 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400689 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500690
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400691 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100692 return;
693
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400694 surface = weston_compositor_pick_surface(seat->compositor,
695 pointer->x,
696 pointer->y,
697 &pointer->current_x,
698 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500699
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400700 if (&surface->surface != pointer->current) {
701 interface = pointer->grab->interface;
702 pointer->current = &surface->surface;
703 interface->focus(pointer->grab, &surface->surface,
704 pointer->current_x,
705 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500706 }
707
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400708 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500709 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100710 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400711 pointer->x,
712 pointer->y,
713 &pointer->grab->x,
714 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500715}
716
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400717static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500718weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400719{
Daniel Stone37816df2012-05-16 18:45:18 +0100720 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400721
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500722 if (!compositor->focus)
723 return;
724
Daniel Stone37816df2012-05-16 18:45:18 +0100725 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400726 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400727}
728
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400729WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500730weston_surface_unmap(struct weston_surface *surface)
731{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100732 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500733
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500734 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500735 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500736 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500737
Daniel Stone4dab5db2012-05-30 16:31:53 +0100738 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100739 if (seat->seat.keyboard &&
740 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100741 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100742 if (seat->seat.pointer &&
743 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100744 wl_pointer_set_focus(seat->seat.pointer,
745 NULL,
746 wl_fixed_from_int(0),
747 wl_fixed_from_int(0));
748 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500749
Kristian Høgsberg98238702012-08-03 16:29:12 -0400750 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500751}
752
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400753struct weston_frame_callback {
754 struct wl_resource resource;
755 struct wl_list link;
756};
757
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500758static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400759destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500760{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500761 struct weston_surface *surface =
762 container_of(resource,
763 struct weston_surface, surface.resource);
764 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400765 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400766
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300767 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500768 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400769
Benjamin Franzke06286262011-05-06 19:12:33 +0200770 if (surface->buffer)
771 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400772
Kristian Høgsberg42263852012-09-06 21:59:29 -0400773 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200774
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200775 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200776 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500777 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500778 pixman_region32_fini(&surface->clip);
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500779 if (!region_is_undefined(&surface->input))
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500780 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200781
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400782 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
783 wl_resource_destroy(&cb->resource);
784
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400785 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500786}
787
Alex Wu8811bf92012-02-28 18:07:54 +0800788WL_EXPORT void
789weston_surface_destroy(struct weston_surface *surface)
790{
791 /* Not a valid way to destroy a client surface */
792 assert(surface->surface.resource.client == NULL);
793
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400794 wl_signal_emit(&surface->surface.resource.destroy_signal,
795 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800796 destroy_surface(&surface->surface.resource);
797}
798
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100799static void
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -0400800weston_surface_attach(struct wl_surface *surface, struct wl_buffer *buffer)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100801{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500802 struct weston_surface *es = (struct weston_surface *) surface;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100803
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300804 if (es->buffer) {
805 weston_buffer_post_release(es->buffer);
806 wl_list_remove(&es->buffer_destroy_listener.link);
807 }
808
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400809 if (buffer) {
810 buffer->busy_count++;
811 wl_signal_add(&buffer->resource.destroy_signal,
812 &es->buffer_destroy_listener);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300813
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400814 if (es->geometry.width != buffer->width ||
815 es->geometry.height != buffer->height) {
816 undef_region(&es->input);
817 pixman_region32_fini(&es->opaque);
818 pixman_region32_init(&es->opaque);
819 }
820 } else {
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300821 if (weston_surface_is_mapped(es))
822 weston_surface_unmap(es);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300823 }
824
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400825 es->compositor->renderer->attach(es, buffer);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400826 es->buffer = buffer;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100827}
828
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500829WL_EXPORT void
830weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400831{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500832 wl_list_remove(&surface->layer_link);
833 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500834 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500835 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400836}
837
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400838WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500839weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400840{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500841 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400842
843 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500844 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400845}
846
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500847WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500848weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +0200849{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400850 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200851 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200852
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400853 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -0500854 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200855}
856
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400857WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500858weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400859{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500860 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400861
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400862 pixman_region32_union(&compositor->primary_plane.damage,
863 &compositor->primary_plane.damage,
864 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400865 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400866}
867
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400868static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500869fade_frame(struct weston_animation *animation,
870 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400871{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500872 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400873 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500874 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500875 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400876
Scott Moreau94b0b0c2012-06-14 01:01:56 -0600877 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -0600878 compositor->fade.spring.timestamp = msecs;
879
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500880 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500881 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500882 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
883 compositor->fade.spring.current);
884 weston_surface_damage(surface);
885
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500886 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400887 compositor->fade.spring.current =
888 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400889 wl_list_remove(&animation->link);
890 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200891
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500892 if (compositor->fade.spring.current < 0.001) {
893 destroy_surface(&surface->surface.resource);
894 compositor->fade.surface = NULL;
895 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500896 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400897 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200898 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400899 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400900}
901
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400902static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400903surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400904 pixman_region32_t *opaque)
905{
906 if (surface->buffer && wl_buffer_is_shm(surface->buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400907 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400908
909 if (surface->transform.enabled) {
910 pixman_box32_t *extents;
911
912 extents = pixman_region32_extents(&surface->damage);
913 surface_compute_bbox(surface, extents->x1, extents->y1,
914 extents->x2 - extents->x1,
915 extents->y2 - extents->y1,
916 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400917 pixman_region32_translate(&surface->damage,
918 -surface->plane->x,
919 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400920 } else {
921 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400922 surface->geometry.x - surface->plane->x,
923 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400924 }
925
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400926 pixman_region32_union(&surface->plane->damage,
927 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400928 empty_region(&surface->damage);
929 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400930 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400931}
932
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400933static void
Rob Bradford0fb79752012-08-02 15:36:57 +0100934weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400935{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500936 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500937 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500938 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -0500939 struct weston_animation *animation, *next;
940 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +0200941 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300942 pixman_region32_t opaque, output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500943
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +0200944 weston_compositor_update_drag_surfaces(ec);
945
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500946 /* Rebuild the surface list and update surface transforms up front. */
947 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +0200948 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500949 wl_list_for_each(layer, &ec->layer_list, link) {
950 wl_list_for_each(es, &layer->surface_list, layer_link) {
951 weston_surface_update_transform(es);
952 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +0200953 if (es->output == output) {
954 wl_list_insert_list(&frame_callback_list,
955 &es->frame_callback_list);
956 wl_list_init(&es->frame_callback_list);
957 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500958 }
959 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +0200960
Kristian Høgsberg79af73e2012-08-03 15:45:23 -0400961 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800962 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -0400963 else
964 wl_list_for_each(es, &ec->surface_list, link)
965 weston_surface_move_to_plane(es, &ec->primary_plane);
966
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500967 pixman_region32_init(&opaque);
968
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400969 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400970 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500971
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400972 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -0400973
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400974 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400975 pixman_region32_intersect(&output_damage,
976 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300977 pixman_region32_subtract(&ec->primary_plane.damage,
978 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400979
Scott Moreauccbf29d2012-02-22 14:21:41 -0700980 if (output->dirty)
981 weston_output_update_matrix(output);
982
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400983 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400984
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500985 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500986
Kristian Høgsbergef044142011-06-21 15:02:12 -0400987 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100988
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -0400989 weston_compositor_repick(ec);
990 wl_event_loop_dispatch(ec->input_loop, 0);
991
Jonas Ådahldb773762012-06-13 00:01:21 +0200992 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500993 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400994 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500995 }
Jonas Ådahldb773762012-06-13 00:01:21 +0200996 wl_list_init(&frame_callback_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500997
Scott Moreaud64cf212012-06-08 19:40:54 -0600998 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -0600999 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001000 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001001 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001002}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001003
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001004static int
1005weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001006{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001007 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001008
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001009 wl_event_loop_dispatch(compositor->input_loop, 0);
1010
1011 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001012}
1013
1014WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001015weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001016{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001017 struct weston_compositor *compositor = output->compositor;
1018 struct wl_event_loop *loop =
1019 wl_display_get_event_loop(compositor->wl_display);
1020 int fd;
1021
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001022 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001023 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001024 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001025 return;
1026 }
1027
1028 output->repaint_scheduled = 0;
1029 if (compositor->input_loop_source)
1030 return;
1031
1032 fd = wl_event_loop_get_fd(compositor->input_loop);
1033 compositor->input_loop_source =
1034 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1035 weston_compositor_read_input, compositor);
1036}
1037
1038static void
1039idle_repaint(void *data)
1040{
1041 struct weston_output *output = data;
1042
1043 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001044}
1045
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001046WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001047weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1048{
1049 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001050 if (below != NULL)
1051 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001052}
1053
1054WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001055weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001056{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001057 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001058 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001059
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001060 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001061 return;
1062
Kristian Høgsbergef044142011-06-21 15:02:12 -04001063 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001064 output->repaint_needed = 1;
1065 if (output->repaint_scheduled)
1066 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001067
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001068 wl_event_loop_add_idle(loop, idle_repaint, output);
1069 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001070
1071 if (compositor->input_loop_source) {
1072 wl_event_source_remove(compositor->input_loop_source);
1073 compositor->input_loop_source = NULL;
1074 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001075}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001076
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001077WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001078weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1079{
1080 struct weston_output *output;
1081
1082 wl_list_for_each(output, &compositor->output_list, link)
1083 weston_output_schedule_repaint(output);
1084}
1085
1086WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001087weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001088{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001089 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001090 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001091
Scott Moreau9d1b1122012-06-08 19:40:53 -06001092 output = container_of(compositor->output_list.next,
1093 struct weston_output, link);
1094
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001095 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001096 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001097 return;
1098
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001099 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001100 surface = weston_surface_create(compositor);
1101 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001102 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001103 wl_list_insert(&compositor->fade_layer.surface_list,
1104 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03001105 weston_surface_update_transform(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001106 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001107 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001108 }
1109
1110 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001111 if (wl_list_empty(&compositor->fade.animation.link)) {
1112 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001113 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001114 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001115 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001116}
1117
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001118static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001119surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001120{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001121 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001122}
1123
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001124static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001125surface_attach(struct wl_client *client,
1126 struct wl_resource *resource,
1127 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1128{
1129 struct weston_surface *es = resource->data;
1130 struct wl_buffer *buffer = NULL;
1131
1132 if (buffer_resource)
1133 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001134
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -04001135 weston_surface_attach(&es->surface, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001136
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001137 if (buffer && es->configure)
1138 es->configure(es, sx, sy);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001139}
1140
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001141static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001142surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001143 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001144 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001145{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001146 struct weston_surface *es = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001147
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001148 pixman_region32_union_rect(&es->damage, &es->damage,
1149 x, y, width, height);
Kristian Høgsberg98238702012-08-03 16:29:12 -04001150 weston_surface_schedule_repaint(es);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001151}
1152
Kristian Høgsberg33418202011-08-16 23:01:28 -04001153static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001154destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001155{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001156 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001157
1158 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001159 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001160}
1161
1162static void
1163surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001164 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001165{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001166 struct weston_frame_callback *cb;
1167 struct weston_surface *es = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001168
1169 cb = malloc(sizeof *cb);
1170 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001171 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001172 return;
1173 }
1174
1175 cb->resource.object.interface = &wl_callback_interface;
1176 cb->resource.object.id = callback;
1177 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001178 cb->resource.client = client;
1179 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001180
1181 wl_client_add_resource(client, &cb->resource);
Jonas Ådahldb773762012-06-13 00:01:21 +02001182 wl_list_insert(es->frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001183}
1184
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001185static void
1186surface_set_opaque_region(struct wl_client *client,
1187 struct wl_resource *resource,
1188 struct wl_resource *region_resource)
1189{
1190 struct weston_surface *surface = resource->data;
1191 struct weston_region *region;
1192
1193 pixman_region32_fini(&surface->opaque);
1194
1195 if (region_resource) {
1196 region = region_resource->data;
1197 pixman_region32_init_rect(&surface->opaque, 0, 0,
1198 surface->geometry.width,
1199 surface->geometry.height);
1200 pixman_region32_intersect(&surface->opaque,
1201 &surface->opaque, &region->region);
1202 } else {
1203 pixman_region32_init(&surface->opaque);
1204 }
1205
1206 surface->geometry.dirty = 1;
1207}
1208
1209static void
1210surface_set_input_region(struct wl_client *client,
1211 struct wl_resource *resource,
1212 struct wl_resource *region_resource)
1213{
1214 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001215 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001216
1217 if (region_resource) {
1218 region = region_resource->data;
1219 pixman_region32_init_rect(&surface->input, 0, 0,
1220 surface->geometry.width,
1221 surface->geometry.height);
1222 pixman_region32_intersect(&surface->input,
1223 &surface->input, &region->region);
1224 } else {
1225 pixman_region32_init_rect(&surface->input, 0, 0,
1226 surface->geometry.width,
1227 surface->geometry.height);
1228 }
1229
Kristian Høgsberg98238702012-08-03 16:29:12 -04001230 weston_surface_schedule_repaint(surface);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001231}
1232
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001233static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001234 surface_destroy,
1235 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001236 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001237 surface_frame,
1238 surface_set_opaque_region,
1239 surface_set_input_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001240};
1241
1242static void
1243compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001244 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001245{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001246 struct weston_compositor *ec = resource->data;
1247 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001248
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001249 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001250 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001251 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001252 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001253 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001254
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001255 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001256
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001257 surface->surface.resource.object.id = id;
1258 surface->surface.resource.object.interface = &wl_surface_interface;
1259 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001260 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001261 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001262
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001263 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001264}
1265
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001266static void
1267destroy_region(struct wl_resource *resource)
1268{
1269 struct weston_region *region =
1270 container_of(resource, struct weston_region, resource);
1271
1272 pixman_region32_fini(&region->region);
1273 free(region);
1274}
1275
1276static void
1277region_destroy(struct wl_client *client, struct wl_resource *resource)
1278{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001279 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001280}
1281
1282static void
1283region_add(struct wl_client *client, struct wl_resource *resource,
1284 int32_t x, int32_t y, int32_t width, int32_t height)
1285{
1286 struct weston_region *region = resource->data;
1287
1288 pixman_region32_union_rect(&region->region, &region->region,
1289 x, y, width, height);
1290}
1291
1292static void
1293region_subtract(struct wl_client *client, struct wl_resource *resource,
1294 int32_t x, int32_t y, int32_t width, int32_t height)
1295{
1296 struct weston_region *region = resource->data;
1297 pixman_region32_t rect;
1298
1299 pixman_region32_init_rect(&rect, x, y, width, height);
1300 pixman_region32_subtract(&region->region, &region->region, &rect);
1301 pixman_region32_fini(&rect);
1302}
1303
1304static const struct wl_region_interface region_interface = {
1305 region_destroy,
1306 region_add,
1307 region_subtract
1308};
1309
1310static void
1311compositor_create_region(struct wl_client *client,
1312 struct wl_resource *resource, uint32_t id)
1313{
1314 struct weston_region *region;
1315
1316 region = malloc(sizeof *region);
1317 if (region == NULL) {
1318 wl_resource_post_no_memory(resource);
1319 return;
1320 }
1321
1322 region->resource.destroy = destroy_region;
1323
1324 region->resource.object.id = id;
1325 region->resource.object.interface = &wl_region_interface;
1326 region->resource.object.implementation =
1327 (void (**)(void)) &region_interface;
1328 region->resource.data = region;
1329
1330 pixman_region32_init(&region->region);
1331
1332 wl_client_add_resource(client, &region->resource);
1333}
1334
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001335static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001336 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001337 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001338};
1339
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001340WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001341weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001342{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001343 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1344 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001345
1346 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001347 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001348}
1349
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001350static void
1351weston_compositor_dpms_on(struct weston_compositor *compositor)
1352{
1353 struct weston_output *output;
1354
1355 wl_list_for_each(output, &compositor->output_list, link)
1356 if (output->set_dpms)
1357 output->set_dpms(output, WESTON_DPMS_ON);
1358}
1359
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001360WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001361weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001362{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001363 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1364 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001365 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001366 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001367 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001368 }
1369}
1370
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001371static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001372weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001373{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001374 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001375 compositor->idle_inhibit++;
1376}
1377
1378static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001379weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001380{
1381 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001382 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001383}
1384
1385static int
1386idle_handler(void *data)
1387{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001388 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001389
1390 if (compositor->idle_inhibit)
1391 return 1;
1392
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001393 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001394
1395 return 1;
1396}
1397
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001398WL_EXPORT void
1399weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1400{
1401 pixman_region32_init(&plane->damage);
1402 plane->x = x;
1403 plane->y = y;
1404}
1405
1406WL_EXPORT void
1407weston_plane_release(struct weston_plane *plane)
1408{
1409 pixman_region32_fini(&plane->damage);
1410}
1411
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001412static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001413weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001414
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001415static void
Daniel Stone37816df2012-05-16 18:45:18 +01001416clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001417{
Daniel Stone37816df2012-05-16 18:45:18 +01001418 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001419 struct weston_output *output, *prev = NULL;
1420 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001421
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001422 x = wl_fixed_to_int(*fx);
1423 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001424 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1425 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001426
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001427 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001428 if (pixman_region32_contains_point(&output->region,
1429 x, y, NULL))
1430 valid = 1;
1431 if (pixman_region32_contains_point(&output->region,
1432 old_x, old_y, NULL))
1433 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001434 }
Daniel Stone37816df2012-05-16 18:45:18 +01001435
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001436 if (!valid) {
1437 if (x < prev->x)
1438 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001439 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001440 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001441 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001442 if (y < prev->y)
1443 *fy = wl_fixed_from_int(prev->y);
1444 else if (y >= prev->y + prev->current->height)
1445 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001446 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001447 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001448}
1449
1450WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001451notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001452{
1453 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001454 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001455 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001456 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001457 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001458
1459 weston_compositor_activity(ec);
1460
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001461 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001462
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001463 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001464
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001465 pointer->x = x;
1466 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001467
1468 ix = wl_fixed_to_int(x);
1469 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001470
Scott Moreauccbf29d2012-02-22 14:21:41 -07001471 wl_list_for_each(output, &ec->output_list, link)
1472 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001473 pixman_region32_contains_point(&output->region,
1474 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06001475 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001476
Daniel Stone37816df2012-05-16 18:45:18 +01001477 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001478 interface = pointer->grab->interface;
1479 interface->motion(pointer->grab, time,
1480 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001481
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001482 if (seat->sprite) {
1483 weston_surface_set_position(seat->sprite,
1484 ix - seat->hotspot_x,
1485 iy - seat->hotspot_y);
1486 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001487 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001488}
1489
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001490WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001491weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001492 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001493{
Daniel Stone37816df2012-05-16 18:45:18 +01001494 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001495
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001496 if (seat->seat.keyboard) {
1497 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1498 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04001499 }
1500
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001501 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001502}
1503
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001504WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001505notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001506 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001507{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001508 struct weston_compositor *compositor = seat->compositor;
1509 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001510 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001511 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001512 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001513
Daniel Stone4dbadb12012-05-30 16:31:51 +01001514 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001515 if (compositor->ping_handler && focus)
1516 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001517 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001518 if (pointer->button_count == 0) {
1519 pointer->grab_button = button;
1520 pointer->grab_time = time;
1521 pointer->grab_x = pointer->x;
1522 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001523 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001524 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001525 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001526 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001527 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001528 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001529
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001530 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001531 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001532
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001533 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001534
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001535 if (pointer->button_count == 1)
1536 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001537 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001538}
1539
Scott Moreau210d0792012-03-22 10:47:01 -06001540WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001541notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01001542 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001543{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001544 struct weston_compositor *compositor = seat->compositor;
1545 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001546 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001547 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001548 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1549
1550 if (compositor->ping_handler && focus)
1551 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001552
1553 weston_compositor_activity(compositor);
1554
Scott Moreau6a3633d2012-03-20 08:47:59 -06001555 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001556 weston_compositor_run_axis_binding(compositor, seat,
1557 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06001558 else
1559 return;
1560
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001561 if (pointer->focus_resource)
1562 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01001563 value);
Scott Moreau210d0792012-03-22 10:47:01 -06001564}
1565
Daniel Stone05d58682012-06-22 13:21:38 +01001566WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001567notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001568{
Daniel Stone05d58682012-06-22 13:21:38 +01001569 struct wl_keyboard *keyboard = &seat->keyboard;
1570 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01001571 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001572 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01001573 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01001574 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01001575
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001576 /* Serialize and update our internal state, checking to see if it's
1577 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01001578 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1579 XKB_STATE_DEPRESSED);
1580 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1581 XKB_STATE_LATCHED);
1582 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1583 XKB_STATE_LOCKED);
1584 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01001585 XKB_STATE_EFFECTIVE);
1586
Daniel Stone71c38772012-06-22 13:21:30 +01001587 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
1588 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
1589 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
1590 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01001591 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01001592
Daniel Stone71c38772012-06-22 13:21:30 +01001593 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
1594 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
1595 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
1596 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001597
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001598 /* And update the modifier_state for bindings. */
1599 mods_lookup = mods_depressed | mods_latched;
1600 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01001601 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001602 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01001603 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001604 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01001605 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001606 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04001607 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
1608 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01001609
Daniel Stone8c8164f2012-05-30 16:31:45 +01001610 /* Finally, notify the compositor that LEDs have changed. */
1611 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001612 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001613 leds |= LED_NUM_LOCK;
1614 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001615 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001616 leds |= LED_CAPS_LOCK;
1617 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001618 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001619 leds |= LED_SCROLL_LOCK;
1620 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01001621 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01001622 seat->xkb_state.leds = leds;
1623
Daniel Stone05d58682012-06-22 13:21:38 +01001624 if (changed) {
1625 grab->interface->modifiers(grab,
1626 serial,
1627 keyboard->modifiers.mods_depressed,
1628 keyboard->modifiers.mods_latched,
1629 keyboard->modifiers.mods_locked,
1630 keyboard->modifiers.group);
1631 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001632}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001633
Daniel Stone05d58682012-06-22 13:21:38 +01001634static void
1635update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001636 enum wl_keyboard_key_state state)
1637{
1638 enum xkb_key_direction direction;
1639
1640 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1641 direction = XKB_KEY_DOWN;
1642 else
1643 direction = XKB_KEY_UP;
1644
1645 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1646 * broken keycode system, which starts at 8. */
1647 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1648
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001649 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001650}
1651
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001652WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001653notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001654 enum wl_keyboard_key_state state,
1655 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001656{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001657 struct weston_compositor *compositor = seat->compositor;
1658 struct wl_keyboard *keyboard = seat->seat.keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01001659 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001660 (struct weston_surface *) keyboard->focus;
1661 struct wl_keyboard_grab *grab = keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001662 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001663 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001664
Daniel Stonec9785ea2012-05-30 16:31:52 +01001665 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001666 if (compositor->ping_handler && focus)
1667 compositor->ping_handler(focus, serial);
1668
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001669 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001670 keyboard->grab_key = key;
1671 keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07001672 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001673 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001674 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001675
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001676 end = keyboard->keys.data + keyboard->keys.size;
1677 for (k = keyboard->keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01001678 if (*k == key) {
1679 /* Ignore server-generated repeats. */
1680 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1681 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001682 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01001683 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001684 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001685 keyboard->keys.size = (void *) end - keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001686 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001687 k = wl_array_add(&keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001688 *k = key;
1689 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001690
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001691 if (grab == &keyboard->default_grab) {
1692 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001693 state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001694 grab = keyboard->grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06001695 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001696
Daniel Stone7ace3902012-05-30 16:31:40 +01001697 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01001698
1699 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001700 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01001701 wl_display_get_serial(compositor->wl_display),
1702 key,
1703 state);
1704 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001705}
1706
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001707WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001708notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01001709 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001710{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001711 struct weston_compositor *compositor = seat->compositor;
1712 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001713
1714 if (output) {
Daniel Stone37816df2012-05-16 18:45:18 +01001715 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001716 x - pointer->x,
1717 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001718
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001719 pointer->x = x;
1720 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001721 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001722 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001723 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001724 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03001725 /* FIXME: We should call wl_pointer_set_focus(seat,
1726 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001727 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001728}
1729
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001730static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001731destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001732{
Daniel Stone37816df2012-05-16 18:45:18 +01001733 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001734
Daniel Stone37816df2012-05-16 18:45:18 +01001735 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001736 saved_kbd_focus_listener);
1737
Daniel Stone37816df2012-05-16 18:45:18 +01001738 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001739}
1740
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001741WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001742notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001743 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001744{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001745 struct weston_compositor *compositor = seat->compositor;
1746 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001747 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001748 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001749
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001750 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001751 wl_array_copy(&keyboard->keys, keys);
1752 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001753 weston_compositor_idle_inhibit(compositor);
1754 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001755 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001756 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001757 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001758
Daniel Stoneef172672012-06-22 13:21:32 +01001759 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001760 wl_array_for_each(k, &keyboard->keys) {
1761 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01001762 WL_KEYBOARD_KEY_STATE_PRESSED);
1763 }
1764
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001765 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001766
1767 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001768 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1769 wl_keyboard_set_focus(keyboard, surface);
1770 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001771 }
1772}
1773
1774WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001775notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01001776{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001777 struct weston_compositor *compositor = seat->compositor;
1778 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001779 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001780
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001781 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001782 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001783 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001784 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001785 WL_KEYBOARD_KEY_STATE_RELEASED);
1786 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001787
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001788 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001789
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001790 if (keyboard->focus) {
1791 seat->saved_kbd_focus = keyboard->focus;
1792 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01001793 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001794 wl_signal_add(&keyboard->focus->resource.destroy_signal,
1795 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001796 }
1797
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001798 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001799 /* FIXME: We really need keyboard grab cancel here to
1800 * let the grab shut down properly. As it is we leak
1801 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001802 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001803}
1804
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001805static void
Daniel Stone37816df2012-05-16 18:45:18 +01001806touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001807{
Daniel Stone37816df2012-05-16 18:45:18 +01001808 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001809 struct wl_resource *resource;
1810
Pekka Paalanen56464252012-07-31 13:21:08 +03001811 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001812 return;
1813
Pekka Paalanen56464252012-07-31 13:21:08 +03001814 if (seat->touch->focus_resource)
1815 wl_list_remove(&seat->touch->focus_listener.link);
1816 seat->touch->focus = NULL;
1817 seat->touch->focus_resource = NULL;
1818
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001819 if (surface) {
1820 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01001821 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04001822 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001823 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02001824 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001825 return;
1826 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001827
Daniel Stone37816df2012-05-16 18:45:18 +01001828 seat->touch->focus = surface;
1829 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03001830 wl_signal_add(&resource->destroy_signal,
1831 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001832 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001833}
1834
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001835/**
1836 * notify_touch - emulates button touches and notifies surfaces accordingly.
1837 *
1838 * It assumes always the correct cycle sequence until it gets here: touch_down
1839 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1840 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001841 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001842 */
1843WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001844notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001845 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001846{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001847 struct weston_compositor *ec = seat->compositor;
1848 struct wl_touch *touch = seat->seat.touch;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001849 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001850 wl_fixed_t sx, sy;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001851 uint32_t serial = 0;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001852
1853 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01001854 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001855 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001856
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001857 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001858
1859 /* the first finger down picks the surface, and all further go
1860 * to that surface for the remainder of the touch session i.e.
1861 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001862 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001863 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001864 touch_set_focus(seat, &es->surface);
1865 } else if (touch->focus) {
1866 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001867 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001868 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001869
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001870 if (touch->focus_resource && touch->focus)
1871 wl_touch_send_down(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001872 serial, time,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001873 &touch->focus->resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001874 touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001875 break;
Daniel Stone37816df2012-05-16 18:45:18 +01001876 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001877 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001878 if (!es)
1879 break;
1880
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001881 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001882 if (touch->focus_resource)
1883 wl_touch_send_motion(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001884 time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001885 break;
Daniel Stone37816df2012-05-16 18:45:18 +01001886 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001887 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001888 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001889
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001890 if (touch->focus_resource)
1891 wl_touch_send_up(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001892 serial, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001893 if (seat->num_tp == 0)
1894 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001895 break;
1896 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001897}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001898
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001899static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001900pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1901{
1902 struct weston_seat *seat = container_of(listener, struct weston_seat,
1903 sprite_destroy_listener);
1904
1905 seat->sprite = NULL;
1906}
1907
1908static void
1909pointer_cursor_surface_configure(struct weston_surface *es,
1910 int32_t dx, int32_t dy)
1911{
1912 struct weston_seat *seat = es->private;
1913 int x, y;
1914
1915 assert(es == seat->sprite);
1916
1917 seat->hotspot_x -= dx;
1918 seat->hotspot_y -= dy;
1919
1920 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
1921 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
1922
1923 weston_surface_configure(seat->sprite, x, y,
1924 es->buffer->width, es->buffer->height);
1925
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03001926 empty_region(&es->input);
1927
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001928 if (!weston_surface_is_mapped(es)) {
1929 wl_list_insert(&es->compositor->cursor_layer.surface_list,
1930 &es->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03001931 weston_surface_update_transform(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001932 }
1933}
1934
1935static void
1936pointer_unmap_sprite(struct weston_seat *seat)
1937{
1938 if (weston_surface_is_mapped(seat->sprite))
1939 weston_surface_unmap(seat->sprite);
1940
1941 wl_list_remove(&seat->sprite_destroy_listener.link);
1942 seat->sprite->configure = NULL;
1943 seat->sprite->private = NULL;
1944 seat->sprite = NULL;
1945}
1946
1947static void
1948pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1949 uint32_t serial, struct wl_resource *surface_resource,
1950 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001951{
Daniel Stone37816df2012-05-16 18:45:18 +01001952 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001953 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001954
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001955 if (surface_resource)
1956 surface = container_of(surface_resource->data,
1957 struct weston_surface, surface);
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04001958
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04001959 if (seat->seat.pointer->focus == NULL)
1960 return;
1961 if (seat->seat.pointer->focus->resource.client != client)
1962 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04001963 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001964 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03001965
1966 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03001967 if (surface->configure) {
1968 wl_resource_post_error(&surface->surface.resource,
1969 WL_DISPLAY_ERROR_INVALID_OBJECT,
1970 "surface->configure already "
1971 "set");
1972 return;
1973 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001974 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001975
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001976 if (seat->sprite)
1977 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001978
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001979 if (!surface)
1980 return;
1981
1982 wl_signal_add(&surface->surface.resource.destroy_signal,
1983 &seat->sprite_destroy_listener);
1984
1985 surface->configure = pointer_cursor_surface_configure;
1986 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001987 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01001988 seat->hotspot_x = x;
1989 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001990
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04001991 if (surface->buffer)
1992 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001993}
1994
Daniel Stone37816df2012-05-16 18:45:18 +01001995static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03001996 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001997};
1998
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02001999static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002000handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002001{
Daniel Stone37816df2012-05-16 18:45:18 +01002002 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002003
Daniel Stone37816df2012-05-16 18:45:18 +01002004 seat = container_of(listener, struct weston_seat,
2005 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002006
Daniel Stone37816df2012-05-16 18:45:18 +01002007 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002008}
2009
Casey Dahlin9074db52012-04-19 22:50:09 -04002010static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002011{
2012 wl_list_remove(&resource->link);
2013 free(resource);
2014}
2015
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002016static void
Daniel Stone37816df2012-05-16 18:45:18 +01002017seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2018 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002019{
Daniel Stone37816df2012-05-16 18:45:18 +01002020 struct weston_seat *seat = resource->data;
2021 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002022
Daniel Stone37816df2012-05-16 18:45:18 +01002023 if (!seat->seat.pointer)
2024 return;
2025
2026 cr = wl_client_add_object(client, &wl_pointer_interface,
2027 &pointer_interface, id, seat);
2028 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002029 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002030
2031 if (seat->seat.pointer->focus &&
2032 seat->seat.pointer->focus->resource.client == client) {
2033 struct weston_surface *surface;
2034 wl_fixed_t sx, sy;
2035
2036 surface = (struct weston_surface *) seat->seat.pointer->focus;
2037 weston_surface_from_global_fixed(surface,
2038 seat->seat.pointer->x,
2039 seat->seat.pointer->y,
2040 &sx,
2041 &sy);
2042 wl_pointer_set_focus(seat->seat.pointer,
2043 seat->seat.pointer->focus,
2044 sx,
2045 sy);
2046 }
Daniel Stone37816df2012-05-16 18:45:18 +01002047}
2048
2049static void
2050seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2051 uint32_t id)
2052{
2053 struct weston_seat *seat = resource->data;
2054 struct wl_resource *cr;
2055
2056 if (!seat->seat.keyboard)
2057 return;
2058
2059 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2060 seat);
2061 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002062 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002063
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002064 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2065 seat->xkb_info.keymap_fd,
2066 seat->xkb_info.keymap_size);
2067
Daniel Stone17b13bd2012-05-30 16:31:39 +01002068 if (seat->seat.keyboard->focus &&
2069 seat->seat.keyboard->focus->resource.client == client) {
2070 wl_keyboard_set_focus(seat->seat.keyboard,
2071 seat->seat.keyboard->focus);
2072 }
Daniel Stone37816df2012-05-16 18:45:18 +01002073}
2074
2075static void
2076seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2077 uint32_t id)
2078{
2079 struct weston_seat *seat = resource->data;
2080 struct wl_resource *cr;
2081
2082 if (!seat->seat.touch)
2083 return;
2084
2085 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2086 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002087 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002088}
2089
2090static const struct wl_seat_interface seat_interface = {
2091 seat_get_pointer,
2092 seat_get_keyboard,
2093 seat_get_touch,
2094};
2095
2096static void
2097bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2098{
2099 struct wl_seat *seat = data;
2100 struct wl_resource *resource;
2101 enum wl_seat_capability caps = 0;
2102
2103 resource = wl_client_add_object(client, &wl_seat_interface,
2104 &seat_interface, id, data);
2105 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002106 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002107
2108 if (seat->pointer)
2109 caps |= WL_SEAT_CAPABILITY_POINTER;
2110 if (seat->keyboard)
2111 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2112 if (seat->touch)
2113 caps |= WL_SEAT_CAPABILITY_TOUCH;
2114
2115 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002116}
2117
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002118static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002119device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002120{
Daniel Stone37816df2012-05-16 18:45:18 +01002121 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002122
Daniel Stone37816df2012-05-16 18:45:18 +01002123 seat = container_of(listener, struct weston_seat,
2124 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002125
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002126 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002127}
2128
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002129static void weston_compositor_xkb_init(struct weston_compositor *ec,
2130 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002131{
Daniel Stonee379da92012-05-30 16:32:04 +01002132 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002133 ec->xkb_context = xkb_context_new(0);
2134 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002135 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002136 exit(1);
2137 }
Daniel Stone994679a2012-05-30 16:31:43 +01002138 }
2139
2140 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002141 ec->xkb_names = *names;
2142 if (!ec->xkb_names.rules)
2143 ec->xkb_names.rules = strdup("evdev");
2144 if (!ec->xkb_names.model)
2145 ec->xkb_names.model = strdup("pc105");
2146 if (!ec->xkb_names.layout)
2147 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002148}
2149
Daniel Stonee379da92012-05-30 16:32:04 +01002150static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2151{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002152 if (xkb_info->keymap)
2153 xkb_map_unref(xkb_info->keymap);
2154
2155 if (xkb_info->keymap_area)
2156 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2157 if (xkb_info->keymap_fd >= 0)
2158 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002159}
2160
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002161static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2162{
Daniel Stonee379da92012-05-30 16:32:04 +01002163 free((char *) ec->xkb_names.rules);
2164 free((char *) ec->xkb_names.model);
2165 free((char *) ec->xkb_names.layout);
2166 free((char *) ec->xkb_names.variant);
2167 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002168
Daniel Stonee379da92012-05-30 16:32:04 +01002169 xkb_info_destroy(&ec->xkb_info);
2170 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002171}
2172
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002173static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002174weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002175{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002176 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002177
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002178 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2179 XKB_MOD_NAME_SHIFT);
2180 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2181 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002182 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2183 XKB_MOD_NAME_CTRL);
2184 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2185 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002186 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2187 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002188 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2189 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002190 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002191
2192 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2193 XKB_LED_NAME_NUM);
2194 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2195 XKB_LED_NAME_CAPS);
2196 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2197 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002198
2199 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2200 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002201 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002202 exit(EXIT_FAILURE);
2203 }
2204 xkb_info->keymap_size = strlen(keymap_str) + 1;
2205
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002206 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002207 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002208 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002209 (unsigned long) xkb_info->keymap_size);
2210 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002211 }
2212
2213 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2214 PROT_READ | PROT_WRITE,
2215 MAP_SHARED, xkb_info->keymap_fd, 0);
2216 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002217 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002218 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002219 goto err_dev_zero;
2220 }
2221 strcpy(xkb_info->keymap_area, keymap_str);
2222 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002223
2224 return;
2225
2226err_dev_zero:
2227 close(xkb_info->keymap_fd);
2228 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002229err_keymap_str:
2230 free(keymap_str);
2231 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002232}
2233
2234static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002235weston_compositor_build_global_keymap(struct weston_compositor *ec)
2236{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002237 if (ec->xkb_info.keymap != NULL)
2238 return;
2239
Daniel Stonee379da92012-05-30 16:32:04 +01002240 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002241 &ec->xkb_names,
2242 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002243 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002244 weston_log("failed to compile global XKB keymap\n");
2245 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002246 "options %s",
2247 ec->xkb_names.rules, ec->xkb_names.model,
2248 ec->xkb_names.layout, ec->xkb_names.variant,
2249 ec->xkb_names.options);
2250 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002251 }
2252
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002253 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002254}
2255
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002256WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002257weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002258{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002259 if (seat->has_keyboard)
2260 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002261
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002262 if (keymap != NULL) {
2263 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002264 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002265 }
2266 else {
2267 weston_compositor_build_global_keymap(seat->compositor);
2268 seat->xkb_info = seat->compositor->xkb_info;
2269 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2270 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002271
2272 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2273 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002274 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01002275 exit(1);
2276 }
2277
Daniel Stone71c38772012-06-22 13:21:30 +01002278 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002279
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002280 wl_keyboard_init(&seat->keyboard);
2281 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2282
2283 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01002284}
2285
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002286WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002287weston_seat_init_pointer(struct weston_seat *seat)
2288{
2289 if (seat->has_pointer)
2290 return;
2291
2292 wl_pointer_init(&seat->pointer);
2293 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2294
2295 seat->has_pointer = 1;
2296}
2297
2298WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002299weston_seat_init_touch(struct weston_seat *seat)
2300{
2301 if (seat->has_touch)
2302 return;
2303
2304 wl_touch_init(&seat->touch);
2305 wl_seat_set_touch(&seat->seat, &seat->touch);
2306
2307 seat->has_touch = 1;
2308}
2309
2310WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002311weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002312{
Daniel Stone37816df2012-05-16 18:45:18 +01002313 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002314 seat->has_pointer = 0;
2315 seat->has_keyboard = 0;
2316 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002317
Daniel Stone37816df2012-05-16 18:45:18 +01002318 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2319 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002320
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002321 seat->sprite = NULL;
2322 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002323
Daniel Stone37816df2012-05-16 18:45:18 +01002324 seat->compositor = ec;
2325 seat->hotspot_x = 16;
2326 seat->hotspot_y = 16;
2327 seat->modifier_state = 0;
2328 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002329
Daniel Stone37816df2012-05-16 18:45:18 +01002330 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002331 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002332
Daniel Stone37816df2012-05-16 18:45:18 +01002333 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002334
Daniel Stone37816df2012-05-16 18:45:18 +01002335 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2336 wl_signal_add(&seat->seat.drag_icon_signal,
2337 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002338
2339 clipboard_create(seat);
Jan Arne Petersene829adc2012-08-10 16:47:22 +02002340 input_method_create(ec, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002341}
2342
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002343WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002344weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002345{
Daniel Stone37816df2012-05-16 18:45:18 +01002346 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002347 /* The global object is destroyed at wl_display_destroy() time. */
2348
Daniel Stone37816df2012-05-16 18:45:18 +01002349 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002350 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002351
Daniel Stone74419a22012-05-30 16:32:02 +01002352 if (seat->xkb_state.state != NULL)
2353 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01002354 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002355
Daniel Stone37816df2012-05-16 18:45:18 +01002356 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002357}
2358
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002359static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002360drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2361{
2362 weston_surface_configure(es,
2363 es->geometry.x + sx, es->geometry.y + sy,
2364 es->buffer->width, es->buffer->height);
2365}
2366
2367static int
Daniel Stone37816df2012-05-16 18:45:18 +01002368device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002369 struct weston_surface *surface)
2370{
Daniel Stone37816df2012-05-16 18:45:18 +01002371 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002372
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002373 if (surface->configure) {
2374 wl_resource_post_error(&surface->surface.resource,
2375 WL_DISPLAY_ERROR_INVALID_OBJECT,
2376 "surface->configure already set");
2377 return 0;
2378 }
2379
Daniel Stone37816df2012-05-16 18:45:18 +01002380 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002381
Daniel Stone37816df2012-05-16 18:45:18 +01002382 weston_surface_set_position(ws->drag_surface,
2383 wl_fixed_to_double(seat->pointer->x),
2384 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002385
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002386 surface->configure = drag_surface_configure;
2387
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002388 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002389 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002390
2391 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002392}
2393
2394static void
Daniel Stone37816df2012-05-16 18:45:18 +01002395device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002396{
Daniel Stone37816df2012-05-16 18:45:18 +01002397 seat->drag_surface->configure = NULL;
2398 undef_region(&seat->drag_surface->input);
2399 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2400 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002401}
2402
2403static void
Daniel Stone37816df2012-05-16 18:45:18 +01002404device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002405{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002406 struct wl_list *list;
2407
Daniel Stone37816df2012-05-16 18:45:18 +01002408 if (weston_surface_is_mapped(seat->drag_surface) ||
2409 !seat->drag_surface->buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002410 return;
2411
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002412 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2413 list = &seat->sprite->layer_link;
2414 else
2415 list = &seat->compositor->cursor_layer.surface_list;
2416
2417 wl_list_insert(list, &seat->drag_surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002418 weston_surface_update_transform(seat->drag_surface);
Daniel Stone37816df2012-05-16 18:45:18 +01002419 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002420}
2421
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002422static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002423weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01002424 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002425{
2426 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002427
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002428 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002429 return;
2430
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002431 if (seat->drag_surface && seat->seat.drag_surface &&
2432 (&seat->drag_surface->surface.resource !=
2433 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002434 /* between calls to this funcion we got a new drag_surface */
2435 surface_changed = 1;
2436
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002437 if (!seat->seat.drag_surface || surface_changed) {
2438 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002439 if (!surface_changed)
2440 return;
2441 }
2442
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002443 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002444 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002445 seat->seat.drag_surface;
2446 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002447 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002448 }
2449
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002450 /* the client may not have attached a buffer to the drag surface
2451 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002452 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002453
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02002454 /* the client may have attached a buffer with a different size to
2455 * the drag surface, causing the input region to be reset */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002456 if (region_is_undefined(&seat->drag_surface->input))
2457 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02002458
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002459 if (!dx && !dy)
2460 return;
2461
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002462 weston_surface_set_position(seat->drag_surface,
2463 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
2464 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002465}
2466
2467WL_EXPORT void
2468weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2469{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002470 struct weston_seat *seat;
2471
2472 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002473 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002474}
2475
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002476static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002477bind_output(struct wl_client *client,
2478 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002479{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002480 struct weston_output *output = data;
2481 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002482 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002483
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002484 resource = wl_client_add_object(client,
2485 &wl_output_interface, NULL, id, data);
2486
Casey Dahlin9074db52012-04-19 22:50:09 -04002487 wl_list_insert(&output->resource_list, &resource->link);
2488 resource->destroy = unbind_resource;
2489
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002490 wl_output_send_geometry(resource,
2491 output->x,
2492 output->y,
2493 output->mm_width,
2494 output->mm_height,
2495 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002496 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002497 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002498
2499 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002500 wl_output_send_mode(resource,
2501 mode->flags,
2502 mode->width,
2503 mode->height,
2504 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002505 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002506}
2507
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002508WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002509weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002510{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002511 struct weston_compositor *c = output->compositor;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002512 int i;
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002513
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002514 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002515
2516 for (i = 0; i < 2; i++)
2517 pixman_region32_fini(&output->buffer_damage[i]);
2518
Casey Dahlin9074db52012-04-19 22:50:09 -04002519 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002520
2521 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002522}
2523
Scott Moreau1bad5db2012-08-18 01:04:05 -06002524static void
2525weston_output_compute_transform(struct weston_output *output)
2526{
2527 struct weston_matrix transform;
2528 int flip;
2529
2530 weston_matrix_init(&transform);
2531
2532 switch(output->transform) {
2533 case WL_OUTPUT_TRANSFORM_FLIPPED:
2534 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2535 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2536 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2537 flip = -1;
2538 break;
2539 default:
2540 flip = 1;
2541 break;
2542 }
2543
2544 switch(output->transform) {
2545 case WL_OUTPUT_TRANSFORM_NORMAL:
2546 case WL_OUTPUT_TRANSFORM_FLIPPED:
2547 transform.d[0] = flip;
2548 transform.d[1] = 0;
2549 transform.d[4] = 0;
2550 transform.d[5] = 1;
2551 break;
2552 case WL_OUTPUT_TRANSFORM_90:
2553 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2554 transform.d[0] = 0;
2555 transform.d[1] = -flip;
2556 transform.d[4] = 1;
2557 transform.d[5] = 0;
2558 break;
2559 case WL_OUTPUT_TRANSFORM_180:
2560 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2561 transform.d[0] = -flip;
2562 transform.d[1] = 0;
2563 transform.d[4] = 0;
2564 transform.d[5] = -1;
2565 break;
2566 case WL_OUTPUT_TRANSFORM_270:
2567 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2568 transform.d[0] = 0;
2569 transform.d[1] = flip;
2570 transform.d[4] = -1;
2571 transform.d[5] = 0;
2572 break;
2573 default:
2574 break;
2575 }
2576
2577 weston_matrix_multiply(&output->matrix, &transform);
2578}
2579
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002580WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002581weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002582{
Scott Moreau850ca422012-05-21 15:21:25 -06002583 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002584 struct weston_matrix camera;
2585 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002586
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002587 weston_matrix_init(&output->matrix);
2588 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002589 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2590 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002591
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002592 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002593 2.0 / (output->width + output->border.left + output->border.right),
2594 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2595
2596 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002597
Scott Moreauccbf29d2012-02-22 14:21:41 -07002598 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002599 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002600 weston_matrix_init(&camera);
2601 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002602 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002603 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002604 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002605 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002606 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002607 weston_matrix_multiply(&output->matrix, &modelview);
2608 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002609
Scott Moreauccbf29d2012-02-22 14:21:41 -07002610 output->dirty = 0;
2611}
2612
Scott Moreau1bad5db2012-08-18 01:04:05 -06002613static void
2614weston_output_transform_init(struct weston_output *output, uint32_t transform)
2615{
2616 output->transform = transform;
2617
2618 switch (transform) {
2619 case WL_OUTPUT_TRANSFORM_90:
2620 case WL_OUTPUT_TRANSFORM_270:
2621 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2622 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2623 /* Swap width and height */
2624 output->width = output->current->height;
2625 output->height = output->current->width;
2626 break;
2627 case WL_OUTPUT_TRANSFORM_NORMAL:
2628 case WL_OUTPUT_TRANSFORM_180:
2629 case WL_OUTPUT_TRANSFORM_FLIPPED:
2630 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2631 output->width = output->current->width;
2632 output->height = output->current->height;
2633 break;
2634 default:
2635 break;
2636 }
2637}
2638
Scott Moreauccbf29d2012-02-22 14:21:41 -07002639WL_EXPORT void
2640weston_output_move(struct weston_output *output, int x, int y)
2641{
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002642 int i;
2643
Scott Moreauccbf29d2012-02-22 14:21:41 -07002644 output->x = x;
2645 output->y = y;
2646
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03002647 output->current_buffer = 0;
2648 for (i = 0; i < 2; i++)
2649 pixman_region32_init(&output->buffer_damage[i]);
2650
Scott Moreauccbf29d2012-02-22 14:21:41 -07002651 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002652 output->width,
2653 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002654}
2655
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002656WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002657weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002658 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002659{
2660 output->compositor = c;
2661 output->x = x;
2662 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002663 output->border.top = 0;
2664 output->border.bottom = 0;
2665 output->border.left = 0;
2666 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002667 output->mm_width = width;
2668 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002669 output->dirty = 1;
2670
Scott Moreau1bad5db2012-08-18 01:04:05 -06002671 if (transform != WL_OUTPUT_TRANSFORM_NORMAL)
2672 output->disable_planes++;
2673
2674 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06002675 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002676
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002677 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002678 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002679
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002680 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002681 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002682 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002683
Casey Dahlin58ba1372012-04-19 22:50:08 -04002684 output->id = ffs(~output->compositor->output_id_pool) - 1;
2685 output->compositor->output_id_pool |= 1 << output->id;
2686
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002687 output->global =
2688 wl_display_add_global(c->wl_display, &wl_output_interface,
2689 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002690}
2691
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002692static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002693compositor_bind(struct wl_client *client,
2694 void *data, uint32_t version, uint32_t id)
2695{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002696 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002697
2698 wl_client_add_object(client, &wl_compositor_interface,
2699 &compositor_interface, id, compositor);
2700}
2701
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002702static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002703log_uname(void)
2704{
2705 struct utsname usys;
2706
2707 uname(&usys);
2708
2709 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2710 usys.version, usys.machine);
2711}
2712
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002713WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002714weston_compositor_init(struct weston_compositor *ec,
2715 struct wl_display *display,
2716 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04002717 char *argv[],
2718 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002719{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002720 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002721 struct xkb_rule_names xkb_names;
2722 const struct config_key keyboard_config_keys[] = {
2723 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2724 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2725 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2726 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2727 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2728 };
2729 const struct config_section cs[] = {
2730 { "keyboard",
2731 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
2732 };
2733
2734 memset(&xkb_names, 0, sizeof(xkb_names));
2735 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002736
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002737 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002738 wl_signal_init(&ec->destroy_signal);
2739 wl_signal_init(&ec->activate_signal);
2740 wl_signal_init(&ec->lock_signal);
2741 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002742 wl_signal_init(&ec->show_input_panel_signal);
2743 wl_signal_init(&ec->hide_input_panel_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002744 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002745
Casey Dahlin58ba1372012-04-19 22:50:08 -04002746 ec->output_id_pool = 0;
2747
Kristian Høgsberga8873122011-11-23 10:39:34 -05002748 if (!wl_display_add_global(display, &wl_compositor_interface,
2749 ec, compositor_bind))
2750 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002751
Daniel Stone725c2c32012-06-22 14:04:36 +01002752 wl_list_init(&ec->surface_list);
2753 wl_list_init(&ec->layer_list);
2754 wl_list_init(&ec->seat_list);
2755 wl_list_init(&ec->output_list);
2756 wl_list_init(&ec->key_binding_list);
2757 wl_list_init(&ec->button_binding_list);
2758 wl_list_init(&ec->axis_binding_list);
2759 wl_list_init(&ec->fade.animation.link);
2760
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002761 weston_plane_init(&ec->primary_plane, 0, 0);
2762
Daniel Stone725c2c32012-06-22 14:04:36 +01002763 weston_compositor_xkb_init(ec, &xkb_names);
2764
2765 ec->ping_handler = NULL;
2766
2767 screenshooter_create(ec);
2768 text_cursor_position_notifier_create(ec);
Philipp Brüschweilerb13b9ff2012-09-09 23:08:31 +02002769 text_model_factory_create(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002770
2771 wl_data_device_manager_init(ec->wl_display);
2772
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002773 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002774
Daniel Stone725c2c32012-06-22 14:04:36 +01002775 loop = wl_display_get_event_loop(ec->wl_display);
2776 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2777 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2778
2779 ec->input_loop = wl_event_loop_create();
2780
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002781 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
2782 ec->fade.animation.frame = fade_frame;
2783
2784 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2785 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2786
2787 weston_compositor_schedule_repaint(ec);
2788
Daniel Stone725c2c32012-06-22 14:04:36 +01002789 return 0;
2790}
2791
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002792WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002793weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002794{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002795 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002796
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002797 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002798 if (ec->input_loop_source)
2799 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002800
Matt Roper361d2ad2011-08-29 13:52:23 -07002801 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002802 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002803 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002804
Daniel Stone325fc2d2012-05-30 16:31:58 +01002805 weston_binding_list_destroy_all(&ec->key_binding_list);
2806 weston_binding_list_destroy_all(&ec->button_binding_list);
2807 weston_binding_list_destroy_all(&ec->axis_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002808
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002809 weston_plane_release(&ec->primary_plane);
2810
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002811 wl_array_release(&ec->vertices);
2812 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05002813 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002814
2815 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07002816}
2817
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002818static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002819{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002820 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002821
Martin Minarik6d118362012-06-07 18:01:59 +02002822 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002823 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002824
2825 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002826}
2827
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002828static void
2829on_segv_signal(int s, siginfo_t *siginfo, void *context)
2830{
2831 void *buffer[32];
2832 int i, count;
2833 Dl_info info;
2834
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002835 /* This SIGSEGV handler will do a best-effort backtrace, and
2836 * then call the backend restore function, which will switch
2837 * back to the vt we launched from or ungrab X etc and then
2838 * raise SIGTRAP. If we run weston under gdb from X or a
2839 * different vt, and tell gdb "handle SIGSEGV nostop", this
2840 * will allow weston to switch back to gdb on crash and then
2841 * gdb will catch the crash with SIGTRAP. */
2842
Martin Minarik6d118362012-06-07 18:01:59 +02002843 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002844
2845 count = backtrace(buffer, ARRAY_LENGTH(buffer));
2846 for (i = 0; i < count; i++) {
2847 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02002848 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002849 (long) buffer[i],
2850 info.dli_sname ? info.dli_sname : "--",
2851 info.dli_fname);
2852 }
2853
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002854 segv_compositor->restore(segv_compositor);
2855
2856 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002857}
2858
2859
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002860static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04002861load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002862{
2863 char path[PATH_MAX];
2864 void *module, *init;
2865
2866 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07002867 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002868 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02002869 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002870
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002871 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
2872 if (module) {
2873 weston_log("Module '%s' already loaded\n", path);
2874 dlclose(module);
2875 return NULL;
2876 }
2877
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002878 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04002879 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002880 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002881 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002882 return NULL;
2883 }
2884
2885 init = dlsym(module, entrypoint);
2886 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002887 weston_log("Failed to lookup init function: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002888 return NULL;
2889 }
2890
2891 return init;
2892}
2893
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002894static int
2895load_modules(struct weston_compositor *ec, const char *modules)
2896{
2897 const char *p, *end;
2898 char buffer[256];
2899 int (*module_init)(struct weston_compositor *ec);
2900
2901 if (modules == NULL)
2902 return 0;
2903
2904 p = modules;
2905 while (*p) {
2906 end = strchrnul(p, ',');
2907 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
2908 module_init = load_module(buffer, "module_init");
2909 if (module_init)
2910 module_init(ec);
2911 p = end;
2912 while (*p == ',')
2913 p++;
2914
2915 }
2916
2917 return 0;
2918}
2919
Pekka Paalanen78a0b572012-06-06 16:59:44 +03002920static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02002921 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
2922
2923static const char xdg_wrong_message[] =
2924 "fatal: environment variable XDG_RUNTIME_DIR\n"
2925 "is set to \"%s\", which is not a directory.\n";
2926
2927static const char xdg_wrong_mode_message[] =
2928 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
2929 "correctly. Unix access mode must be 0700 but is %o,\n"
2930 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04002931 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02002932
2933static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03002934 "Refer to your distribution on how to get it, or\n"
2935 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
2936 "on how to implement it.\n";
2937
Martin Minarik37032f82012-06-18 20:15:18 +02002938static void
2939verify_xdg_runtime_dir(void)
2940{
2941 char *dir = getenv("XDG_RUNTIME_DIR");
2942 struct stat s;
2943
2944 if (!dir) {
2945 weston_log(xdg_error_message);
2946 weston_log_continue(xdg_detail_message);
2947 exit(EXIT_FAILURE);
2948 }
2949
2950 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
2951 weston_log(xdg_wrong_message, dir);
2952 weston_log_continue(xdg_detail_message);
2953 exit(EXIT_FAILURE);
2954 }
2955
2956 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
2957 weston_log(xdg_wrong_mode_message,
2958 dir, s.st_mode & 0777, s.st_uid);
2959 weston_log_continue(xdg_detail_message);
2960 }
2961}
2962
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04002963static int
2964usage(int error_code)
2965{
2966 fprintf(stderr,
2967 "Usage: weston [OPTIONS]\n\n"
2968 "This is weston version " VERSION ", the Wayland reference compositor.\n"
2969 "Weston supports multiple backends, and depending on which backend is in use\n"
2970 "different options will be accepted.\n\n"
2971
2972
2973 "Core options:\n\n"
2974 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
2975 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
2976 " -S, --socket=NAME\tName of socket to listen on\n"
2977 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002978 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04002979 " --log==FILE\t\tLog to the given file\n"
2980 " -h, --help\t\tThis help message\n\n");
2981
2982 fprintf(stderr,
2983 "Options for drm-backend.so:\n\n"
2984 " --connector=ID\tBring up only this connector\n"
2985 " --seat=SEAT\t\tThe seat that weston should run on\n"
2986 " --tty=TTY\t\tThe tty to use\n"
2987 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
2988
2989 fprintf(stderr,
2990 "Options for x11-backend.so:\n\n"
2991 " --width=WIDTH\t\tWidth of X window\n"
2992 " --height=HEIGHT\tHeight of X window\n"
2993 " --fullscreen\t\tRun in fullscreen mode\n"
2994 " --output-count=COUNT\tCreate multiple outputs\n"
2995 " --no-input\t\tDont create input devices\n\n");
2996
2997 fprintf(stderr,
2998 "Options for wayland-backend.so:\n\n"
2999 " --width=WIDTH\t\tWidth of Wayland surface\n"
3000 " --height=HEIGHT\tHeight of Wayland surface\n"
3001 " --display=DISPLAY\tWayland display to connect to\n\n");
3002
3003 exit(error_code);
3004}
3005
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003006int main(int argc, char *argv[])
3007{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003008 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003009 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003010 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003011 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003012 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003013 struct sigaction segv_action;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003014 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003015 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003016 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003017 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003018 char *backend = NULL;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003019 const char *modules = "desktop-shell.so", *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003020 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003021 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003022 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003023 char *socket_name = "wayland-0";
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003024 char *config_file;
3025
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003026 const struct config_key core_config_keys[] = {
3027 { "modules", CONFIG_KEY_STRING, &modules },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003028 };
3029
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003030 const struct config_section cs[] = {
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003031 { "core",
3032 core_config_keys, ARRAY_LENGTH(core_config_keys) },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003033 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003034
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003035 const struct weston_option core_options[] = {
3036 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3037 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3038 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003039 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003040 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003041 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003042 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003043
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003044 argc = parse_options(core_options,
3045 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003046
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003047 if (help)
3048 usage(EXIT_SUCCESS);
3049
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003050 weston_log_file_open(log);
3051
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003052 weston_log("%s\n"
3053 STAMP_SPACE "%s\n"
3054 STAMP_SPACE "Bug reports to: %s\n"
3055 STAMP_SPACE "Build: %s\n",
3056 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003057 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003058 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003059
Martin Minarik37032f82012-06-18 20:15:18 +02003060 verify_xdg_runtime_dir();
3061
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003062 display = wl_display_create();
3063
Tiago Vignatti2116b892011-08-08 05:52:59 -07003064 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003065 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3066 display);
3067 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3068 display);
3069 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3070 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003071
3072 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003073 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3074 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003075
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003076 if (!backend) {
3077 if (getenv("WAYLAND_DISPLAY"))
3078 backend = "wayland-backend.so";
3079 else if (getenv("DISPLAY"))
3080 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003081 else
3082 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003083 }
3084
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003085 config_file = config_file_path("weston.ini");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003086 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003087
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003088 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003089 if (!backend_init)
3090 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003091
Daniel Stonec1be8e52012-06-01 11:14:02 -04003092 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003093 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003094 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003095 exit(EXIT_FAILURE);
3096 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003097
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003098 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3099 segv_action.sa_sigaction = on_segv_signal;
3100 sigemptyset(&segv_action.sa_mask);
3101 sigaction(SIGSEGV, &segv_action, NULL);
3102 segv_compositor = ec;
3103
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003104 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03003105 weston_log("fatal: unhandled option: %s\n", argv[i]);
3106 if (argv[1]) {
3107 ret = EXIT_FAILURE;
3108 goto out;
3109 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003110
Daniel Stonec1be8e52012-06-01 11:14:02 -04003111 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01003112
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003113 ec->option_idle_time = idle_time;
3114 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003115
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003116 setenv("WAYLAND_DISPLAY", socket_name, 1);
3117
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003118 if (load_modules(ec, modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003119 goto out;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003120 if (load_modules(ec, option_modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003121 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003122
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003123 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003124 weston_log("fatal: failed to add socket: %m\n");
3125 ret = EXIT_FAILURE;
3126 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003127 }
3128
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003129 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003130 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003131
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003132 wl_display_run(display);
3133
3134 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003135 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05003136 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003137
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003138 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003139
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003140 for (i = ARRAY_LENGTH(signals); i;)
3141 wl_event_source_remove(signals[--i]);
3142
Daniel Stone855028f2012-05-01 20:37:10 +01003143 weston_compositor_xkb_destroy(ec);
3144
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003145 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003146 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003147
Martin Minarik19e6f262012-06-07 13:08:46 +02003148 weston_log_file_close();
3149
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003150 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003151}