blob: 869fffa28de56fd4ef933c5a4bb2e8951c3ebacb [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
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300184surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
185{
186 struct weston_surface *surface =
187 container_of(listener, struct weston_surface,
188 pending.buffer_destroy_listener);
189
190 surface->pending.buffer = NULL;
191}
192
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200193static void
194empty_region(pixman_region32_t *region)
195{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300196 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200197 pixman_region32_init(region);
198}
199
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300200static void
201region_init_infinite(pixman_region32_t *region)
202{
203 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
204 UINT32_MAX, UINT32_MAX);
205}
206
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500207WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500208weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500209{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500210 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400211
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200212 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400213 if (surface == NULL)
214 return NULL;
215
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400216 wl_signal_init(&surface->surface.resource.destroy_signal);
Kristian Høgsberg48891542012-02-23 17:38:33 -0500217
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500218 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500219 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500220
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400221 surface->surface.resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400222
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500223 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400224 surface->alpha = 1.0;
Juan Zhao46436612012-03-20 01:48:46 +0800225 surface->pitch = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400226
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100227 if (compositor->renderer->create_surface(surface) < 0) {
228 free(surface);
229 return NULL;
230 }
231
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200232 pixman_region32_init(&surface->texture_damage);
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200233
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200234 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
235 surface->pending.buffer_transform = surface->buffer_transform;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400236 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400237 surface->plane = &compositor->primary_plane;
Benjamin Franzke06286262011-05-06 19:12:33 +0200238
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400239 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500240 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500241 pixman_region32_init(&surface->clip);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300242 region_init_infinite(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200243 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500244 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400245
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200246 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200247 wl_list_insert(&surface->geometry.transformation_list,
248 &surface->transform.position.link);
249 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200250 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200251 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500252
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300253 surface->pending.buffer_destroy_listener.notify =
254 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300255 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300256 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300257 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300258 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300259
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400260 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500261}
262
Alex Wu8811bf92012-02-28 18:07:54 +0800263WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500264weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200265 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500266{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100267 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500268}
269
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400270WL_EXPORT void
271weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200272 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200273{
274 if (surface->transform.enabled) {
275 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
276
277 weston_matrix_transform(&surface->transform.matrix, &v);
278
279 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200280 weston_log("warning: numerical instability in "
Pekka Paalanenece8a012012-02-08 15:23:15 +0200281 "weston_surface_to_global(), divisor = %g\n",
282 v.f[3]);
283 *x = 0;
284 *y = 0;
285 return;
286 }
287
288 *x = v.f[0] / v.f[3];
289 *y = v.f[1] / v.f[3];
290 } else {
291 *x = sx + surface->geometry.x;
292 *y = sy + surface->geometry.y;
293 }
294}
295
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500296WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200297weston_surface_to_buffer_float(struct weston_surface *surface,
298 float sx, float sy, float *bx, float *by)
299{
300 switch (surface->buffer_transform) {
301 case WL_OUTPUT_TRANSFORM_NORMAL:
302 default:
303 *bx = sx;
304 *by = sy;
305 break;
306 case WL_OUTPUT_TRANSFORM_FLIPPED:
307 *bx = surface->geometry.width - sx;
308 *by = sy;
309 break;
310 case WL_OUTPUT_TRANSFORM_90:
311 *bx = surface->geometry.height - sy;
312 *by = sx;
313 break;
314 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
315 *bx = surface->geometry.height - sy;
316 *by = surface->geometry.width - sx;
317 break;
318 case WL_OUTPUT_TRANSFORM_180:
319 *bx = surface->geometry.width - sx;
320 *by = surface->geometry.height - sy;
321 break;
322 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
323 *bx = sx;
324 *by = surface->geometry.height - sy;
325 break;
326 case WL_OUTPUT_TRANSFORM_270:
327 *bx = sy;
328 *by = surface->geometry.width - sx;
329 break;
330 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
331 *bx = sy;
332 *by = sx;
333 break;
334 }
335}
336
337WL_EXPORT pixman_box32_t
338weston_surface_to_buffer_rect(struct weston_surface *surface,
339 pixman_box32_t rect)
340{
341 float x1, x2, y1, y2;
342
343 pixman_box32_t ret;
344
345 weston_surface_to_buffer_float(surface, rect.x1, rect.y1, &x1, &y1);
346 weston_surface_to_buffer_float(surface, rect.x2, rect.y2, &x2, &y2);
347
348 if (x1 <= x2) {
349 ret.x1 = x1;
350 ret.x2 = x2;
351 } else {
352 ret.x1 = x2;
353 ret.x2 = x1;
354 }
355
356 if (y1 <= y2) {
357 ret.y1 = y1;
358 ret.y2 = y2;
359 } else {
360 ret.y1 = y2;
361 ret.y2 = y1;
362 }
363
364 return ret;
365}
366
367WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400368weston_surface_move_to_plane(struct weston_surface *surface,
369 struct weston_plane *plane)
370{
371 if (surface->plane == plane)
372 return;
373
374 weston_surface_damage_below(surface);
375 surface->plane = plane;
376 weston_surface_damage(surface);
377}
378
379WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500380weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200381{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500382 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200383
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500384 pixman_region32_init(&damage);
385 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
386 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400387 pixman_region32_union(&surface->plane->damage,
388 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500389 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200390}
391
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400392static struct wl_resource *
393find_resource_for_client(struct wl_list *list, struct wl_client *client)
394{
395 struct wl_resource *r;
396
397 wl_list_for_each(r, list, link) {
398 if (r->client == client)
399 return r;
400 }
401
402 return NULL;
403}
404
405static void
406weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
407{
408 uint32_t different = es->output_mask ^ mask;
409 uint32_t entered = mask & different;
410 uint32_t left = es->output_mask & different;
411 struct weston_output *output;
412 struct wl_resource *resource = NULL;
413 struct wl_client *client = es->surface.resource.client;
414
415 es->output_mask = mask;
416 if (es->surface.resource.client == NULL)
417 return;
418 if (different == 0)
419 return;
420
421 wl_list_for_each(output, &es->compositor->output_list, link) {
422 if (1 << output->id & different)
423 resource =
424 find_resource_for_client(&output->resource_list,
425 client);
426 if (resource == NULL)
427 continue;
428 if (1 << output->id & entered)
429 wl_surface_send_enter(&es->surface.resource, resource);
430 if (1 << output->id & left)
431 wl_surface_send_leave(&es->surface.resource, resource);
432 }
433}
434
435static void
436weston_surface_assign_output(struct weston_surface *es)
437{
438 struct weston_compositor *ec = es->compositor;
439 struct weston_output *output, *new_output;
440 pixman_region32_t region;
441 uint32_t max, area, mask;
442 pixman_box32_t *e;
443
444 new_output = NULL;
445 max = 0;
446 mask = 0;
447 pixman_region32_init(&region);
448 wl_list_for_each(output, &ec->output_list, link) {
449 pixman_region32_intersect(&region, &es->transform.boundingbox,
450 &output->region);
451
452 e = pixman_region32_extents(&region);
453 area = (e->x2 - e->x1) * (e->y2 - e->y1);
454
455 if (area > 0)
456 mask |= 1 << output->id;
457
458 if (area >= max) {
459 new_output = output;
460 max = area;
461 }
462 }
463 pixman_region32_fini(&region);
464
465 es->output = new_output;
466 weston_surface_update_output_mask(es, mask);
467}
468
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200469static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200470surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
471 int32_t width, int32_t height,
472 pixman_region32_t *bbox)
473{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200474 float min_x = HUGE_VALF, min_y = HUGE_VALF;
475 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200476 int32_t s[4][2] = {
477 { sx, sy },
478 { sx, sy + height },
479 { sx + width, sy },
480 { sx + width, sy + height }
481 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200482 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200483 int i;
484
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300485 if (width == 0 || height == 0) {
486 /* avoid rounding empty bbox to 1x1 */
487 pixman_region32_init(bbox);
488 return;
489 }
490
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200491 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200492 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400493 weston_surface_to_global_float(surface,
494 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200495 if (x < min_x)
496 min_x = x;
497 if (x > max_x)
498 max_x = x;
499 if (y < min_y)
500 min_y = y;
501 if (y > max_y)
502 max_y = y;
503 }
504
Pekka Paalanen219b9822012-02-08 15:38:37 +0200505 int_x = floorf(min_x);
506 int_y = floorf(min_y);
507 pixman_region32_init_rect(bbox, int_x, int_y,
508 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200509}
510
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200511static void
512weston_surface_update_transform_disable(struct weston_surface *surface)
513{
514 surface->transform.enabled = 0;
515
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200516 /* round off fractions when not transformed */
517 surface->geometry.x = roundf(surface->geometry.x);
518 surface->geometry.y = roundf(surface->geometry.y);
519
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200520 pixman_region32_init_rect(&surface->transform.boundingbox,
521 surface->geometry.x,
522 surface->geometry.y,
523 surface->geometry.width,
524 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500525
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400526 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500527 pixman_region32_copy(&surface->transform.opaque,
528 &surface->opaque);
529 pixman_region32_translate(&surface->transform.opaque,
530 surface->geometry.x,
531 surface->geometry.y);
532 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200533}
534
535static int
536weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200537{
538 struct weston_matrix *matrix = &surface->transform.matrix;
539 struct weston_matrix *inverse = &surface->transform.inverse;
540 struct weston_transform *tform;
541
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200542 surface->transform.enabled = 1;
543
544 /* Otherwise identity matrix, but with x and y translation. */
545 surface->transform.position.matrix.d[12] = surface->geometry.x;
546 surface->transform.position.matrix.d[13] = surface->geometry.y;
547
548 weston_matrix_init(matrix);
549 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
550 weston_matrix_multiply(matrix, &tform->matrix);
551
552 if (weston_matrix_invert(inverse, matrix) < 0) {
553 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200554 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200555 " transformation not invertible.\n", surface);
556 return -1;
557 }
558
559 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
560 surface->geometry.height,
561 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500562
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200563 return 0;
564}
565
566WL_EXPORT void
567weston_surface_update_transform(struct weston_surface *surface)
568{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200569 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200570 return;
571
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200572 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200573
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500574 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200575
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200576 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500577 pixman_region32_fini(&surface->transform.opaque);
578 pixman_region32_init(&surface->transform.opaque);
579
Pekka Paalanencd403622012-01-25 13:37:39 +0200580 /* transform.position is always in transformation_list */
581 if (surface->geometry.transformation_list.next ==
582 &surface->transform.position.link &&
583 surface->geometry.transformation_list.prev ==
584 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200585 weston_surface_update_transform_disable(surface);
586 } else {
587 if (weston_surface_update_transform_enable(surface) < 0)
588 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200589 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200590
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400591 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200592
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300593 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200594}
595
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200596WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100597weston_surface_to_global_fixed(struct weston_surface *surface,
598 wl_fixed_t sx, wl_fixed_t sy,
599 wl_fixed_t *x, wl_fixed_t *y)
600{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200601 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100602
603 weston_surface_to_global_float(surface,
604 wl_fixed_to_double(sx),
605 wl_fixed_to_double(sy),
606 &xf, &yf);
607 *x = wl_fixed_from_double(xf);
608 *y = wl_fixed_from_double(yf);
609}
610
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400611WL_EXPORT void
612weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200613 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200614{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200615 if (surface->transform.enabled) {
616 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
617
618 weston_matrix_transform(&surface->transform.inverse, &v);
619
620 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200621 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200622 "weston_surface_from_global(), divisor = %g\n",
623 v.f[3]);
624 *sx = 0;
625 *sy = 0;
626 return;
627 }
628
Pekka Paalanencd403622012-01-25 13:37:39 +0200629 *sx = v.f[0] / v.f[3];
630 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200631 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200632 *sx = x - surface->geometry.x;
633 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200634 }
635}
636
637WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100638weston_surface_from_global_fixed(struct weston_surface *surface,
639 wl_fixed_t x, wl_fixed_t y,
640 wl_fixed_t *sx, wl_fixed_t *sy)
641{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200642 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100643
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400644 weston_surface_from_global_float(surface,
645 wl_fixed_to_double(x),
646 wl_fixed_to_double(y),
647 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100648 *sx = wl_fixed_from_double(sxf);
649 *sy = wl_fixed_from_double(syf);
650}
651
652WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200653weston_surface_from_global(struct weston_surface *surface,
654 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
655{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200656 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200657
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400658 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200659 *sx = floorf(sxf);
660 *sy = floorf(syf);
661}
662
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400663WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400664weston_surface_schedule_repaint(struct weston_surface *surface)
665{
666 struct weston_output *output;
667
668 wl_list_for_each(output, &surface->compositor->output_list, link)
669 if (surface->output_mask & (1 << output->id))
670 weston_output_schedule_repaint(output);
671}
672
673WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500674weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500675{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400676 pixman_region32_union_rect(&surface->damage, &surface->damage,
677 0, 0, surface->geometry.width,
678 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200679
Kristian Høgsberg98238702012-08-03 16:29:12 -0400680 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500681}
682
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400683WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500684weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200685 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400686{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200687 surface->geometry.x = x;
688 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200689 surface->geometry.width = width;
690 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200691 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400692}
693
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200694WL_EXPORT void
695weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200696 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200697{
698 surface->geometry.x = x;
699 surface->geometry.y = y;
700 surface->geometry.dirty = 1;
701}
702
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300703WL_EXPORT int
704weston_surface_is_mapped(struct weston_surface *surface)
705{
706 if (surface->output)
707 return 1;
708 else
709 return 0;
710}
711
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200712WL_EXPORT int32_t
713weston_surface_buffer_width(struct weston_surface *surface)
714{
715 switch (surface->buffer_transform) {
716 case WL_OUTPUT_TRANSFORM_90:
717 case WL_OUTPUT_TRANSFORM_270:
718 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
719 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200720 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200721 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200722 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200723 }
724}
725
726WL_EXPORT int32_t
727weston_surface_buffer_height(struct weston_surface *surface)
728{
729 switch (surface->buffer_transform) {
730 case WL_OUTPUT_TRANSFORM_90:
731 case WL_OUTPUT_TRANSFORM_270:
732 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
733 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200734 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200735 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200736 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200737 }
738}
739
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400740WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500741weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500742{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400743 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500744
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400745 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500746
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400747 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500748}
749
Tiago Vignatti9d393522012-02-10 16:26:19 +0200750static struct weston_surface *
751weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100752 wl_fixed_t x, wl_fixed_t y,
753 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200754{
755 struct weston_surface *surface;
756
757 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100758 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500759 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100760 wl_fixed_to_int(*sx),
761 wl_fixed_to_int(*sy),
762 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200763 return surface;
764 }
765
766 return NULL;
767}
768
769static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400770weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500771{
Scott Moreau447013d2012-02-18 05:05:29 -0700772 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500773 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400774 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500775
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400776 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100777 return;
778
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400779 surface = weston_compositor_pick_surface(seat->compositor,
780 pointer->x,
781 pointer->y,
782 &pointer->current_x,
783 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500784
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400785 if (&surface->surface != pointer->current) {
786 interface = pointer->grab->interface;
787 pointer->current = &surface->surface;
788 interface->focus(pointer->grab, &surface->surface,
789 pointer->current_x,
790 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500791 }
792
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400793 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500794 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100795 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400796 pointer->x,
797 pointer->y,
798 &pointer->grab->x,
799 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500800}
801
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400802static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500803weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400804{
Daniel Stone37816df2012-05-16 18:45:18 +0100805 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400806
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500807 if (!compositor->focus)
808 return;
809
Daniel Stone37816df2012-05-16 18:45:18 +0100810 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400811 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400812}
813
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400814WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500815weston_surface_unmap(struct weston_surface *surface)
816{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100817 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500818
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500819 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500820 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500821 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500822
Daniel Stone4dab5db2012-05-30 16:31:53 +0100823 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100824 if (seat->seat.keyboard &&
825 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100826 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100827 if (seat->seat.pointer &&
828 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100829 wl_pointer_set_focus(seat->seat.pointer,
830 NULL,
831 wl_fixed_from_int(0),
832 wl_fixed_from_int(0));
833 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500834
Kristian Høgsberg98238702012-08-03 16:29:12 -0400835 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500836}
837
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400838struct weston_frame_callback {
839 struct wl_resource resource;
840 struct wl_list link;
841};
842
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500843static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400844destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500845{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500846 struct weston_surface *surface =
847 container_of(resource,
848 struct weston_surface, surface.resource);
849 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400850 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400851
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300852 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500853 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400854
Pekka Paalanenbc106382012-10-10 12:49:31 +0300855 wl_list_for_each_safe(cb, next,
856 &surface->pending.frame_callback_list, link)
857 wl_resource_destroy(&cb->resource);
858
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300859 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300860 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +0300861 pixman_region32_fini(&surface->pending.damage);
862
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300863 if (surface->pending.buffer)
864 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
865
Pekka Paalanende685b82012-12-04 15:58:12 +0200866 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400867
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200868 pixman_region32_fini(&surface->texture_damage);
Kristian Høgsberg42263852012-09-06 21:59:29 -0400869 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200870
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200871 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200872 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500873 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500874 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300875 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200876
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400877 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
878 wl_resource_destroy(&cb->resource);
879
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400880 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500881}
882
Alex Wu8811bf92012-02-28 18:07:54 +0800883WL_EXPORT void
884weston_surface_destroy(struct weston_surface *surface)
885{
886 /* Not a valid way to destroy a client surface */
887 assert(surface->surface.resource.client == NULL);
888
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400889 wl_signal_emit(&surface->surface.resource.destroy_signal,
890 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800891 destroy_surface(&surface->surface.resource);
892}
893
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100894static void
Pekka Paalanende685b82012-12-04 15:58:12 +0200895weston_buffer_reference_handle_destroy(struct wl_listener *listener,
896 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100897{
Pekka Paalanende685b82012-12-04 15:58:12 +0200898 struct weston_buffer_reference *ref =
899 container_of(listener, struct weston_buffer_reference,
900 destroy_listener);
901
902 assert((struct wl_buffer *)data == ref->buffer);
903 ref->buffer = NULL;
904}
905
906WL_EXPORT void
907weston_buffer_reference(struct weston_buffer_reference *ref,
908 struct wl_buffer *buffer)
909{
910 if (ref->buffer && buffer != ref->buffer) {
911 weston_buffer_post_release(ref->buffer);
912 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300913 }
914
Pekka Paalanende685b82012-12-04 15:58:12 +0200915 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400916 buffer->busy_count++;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300917 wl_signal_add(&buffer->resource.destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +0200918 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +0200919 }
920
Pekka Paalanende685b82012-12-04 15:58:12 +0200921 ref->buffer = buffer;
922 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
923}
924
925static void
926weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
927{
928 weston_buffer_reference(&surface->buffer_ref, buffer);
929
Pekka Paalanena6421c42012-12-04 15:58:10 +0200930 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300931 if (weston_surface_is_mapped(surface))
932 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300933 }
934
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300935 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100936}
937
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500938WL_EXPORT void
939weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400940{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500941 wl_list_remove(&surface->layer_link);
942 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500943 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500944 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400945}
946
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400947WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500948weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400949{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500950 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400951
952 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500953 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400954}
955
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500956WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500957weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +0200958{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400959 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200960 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200961
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400962 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -0500963 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200964}
965
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400966WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500967weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400968{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500969 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400970
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400971 pixman_region32_union(&compositor->primary_plane.damage,
972 &compositor->primary_plane.damage,
973 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400974 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400975}
976
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400977static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500978fade_frame(struct weston_animation *animation,
979 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400980{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500981 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400982 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500983 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500984 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400985
Scott Moreau94b0b0c2012-06-14 01:01:56 -0600986 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -0600987 compositor->fade.spring.timestamp = msecs;
988
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500989 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500990 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500991 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
992 compositor->fade.spring.current);
993 weston_surface_damage(surface);
994
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500995 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400996 compositor->fade.spring.current =
997 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400998 wl_list_remove(&animation->link);
999 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001000
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001001 if (compositor->fade.spring.current < 0.001) {
1002 destroy_surface(&surface->surface.resource);
1003 compositor->fade.surface = NULL;
1004 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001005 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001006 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001007 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001008 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001009}
1010
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001011static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001012surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001013 pixman_region32_t *opaque)
1014{
Pekka Paalanende685b82012-12-04 15:58:12 +02001015 if (surface->buffer_ref.buffer &&
1016 wl_buffer_is_shm(surface->buffer_ref.buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001017 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001018
1019 if (surface->transform.enabled) {
1020 pixman_box32_t *extents;
1021
1022 extents = pixman_region32_extents(&surface->damage);
1023 surface_compute_bbox(surface, extents->x1, extents->y1,
1024 extents->x2 - extents->x1,
1025 extents->y2 - extents->y1,
1026 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001027 pixman_region32_translate(&surface->damage,
1028 -surface->plane->x,
1029 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001030 } else {
1031 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001032 surface->geometry.x - surface->plane->x,
1033 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001034 }
1035
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001036 pixman_region32_subtract(&surface->damage,
1037 &surface->damage, &surface->plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001038 pixman_region32_union(&surface->plane->damage,
1039 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001040 empty_region(&surface->damage);
1041 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001042 pixman_region32_union(&surface->plane->opaque,
1043 &surface->plane->opaque,
1044 &surface->transform.opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001045 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001046}
1047
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001048static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001049weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001050{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001051 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001052 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001053 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001054 struct weston_animation *animation, *next;
1055 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001056 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001057 pixman_region32_t opaque, output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001058
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001059 weston_compositor_update_drag_surfaces(ec);
1060
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001061 /* Rebuild the surface list and update surface transforms up front. */
1062 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +02001063 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001064 wl_list_for_each(layer, &ec->layer_list, link) {
1065 wl_list_for_each(es, &layer->surface_list, layer_link) {
1066 weston_surface_update_transform(es);
1067 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +02001068 if (es->output == output) {
1069 wl_list_insert_list(&frame_callback_list,
1070 &es->frame_callback_list);
1071 wl_list_init(&es->frame_callback_list);
1072 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001073 }
1074 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001075
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001076 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001077 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001078 else
1079 wl_list_for_each(es, &ec->surface_list, link)
1080 weston_surface_move_to_plane(es, &ec->primary_plane);
1081
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001082 pixman_region32_init(&opaque);
1083
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001084 pixman_region32_fini(&ec->primary_plane.opaque);
1085 pixman_region32_init(&ec->primary_plane.opaque);
1086
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001087 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001088 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001089
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -04001090 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001091
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001092 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001093 pixman_region32_intersect(&output_damage,
1094 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001095
Scott Moreauccbf29d2012-02-22 14:21:41 -07001096 if (output->dirty)
1097 weston_output_update_matrix(output);
1098
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001099 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001100
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001101 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001102
Kristian Høgsbergef044142011-06-21 15:02:12 -04001103 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001104
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001105 weston_compositor_repick(ec);
1106 wl_event_loop_dispatch(ec->input_loop, 0);
1107
Jonas Ådahldb773762012-06-13 00:01:21 +02001108 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001109 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001110 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001111 }
1112
Scott Moreaud64cf212012-06-08 19:40:54 -06001113 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001114 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001115 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001116 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001117}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001118
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001119static int
1120weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001121{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001122 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001123
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001124 wl_event_loop_dispatch(compositor->input_loop, 0);
1125
1126 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001127}
1128
1129WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001130weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001131{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001132 struct weston_compositor *compositor = output->compositor;
1133 struct wl_event_loop *loop =
1134 wl_display_get_event_loop(compositor->wl_display);
1135 int fd;
1136
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001137 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001138 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001139 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001140 return;
1141 }
1142
1143 output->repaint_scheduled = 0;
1144 if (compositor->input_loop_source)
1145 return;
1146
1147 fd = wl_event_loop_get_fd(compositor->input_loop);
1148 compositor->input_loop_source =
1149 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1150 weston_compositor_read_input, compositor);
1151}
1152
1153static void
1154idle_repaint(void *data)
1155{
1156 struct weston_output *output = data;
1157
1158 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001159}
1160
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001161WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001162weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1163{
1164 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001165 if (below != NULL)
1166 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001167}
1168
1169WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001170weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001171{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001172 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001173 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001174
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001175 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001176 return;
1177
Kristian Høgsbergef044142011-06-21 15:02:12 -04001178 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001179 output->repaint_needed = 1;
1180 if (output->repaint_scheduled)
1181 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001182
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001183 wl_event_loop_add_idle(loop, idle_repaint, output);
1184 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001185
1186 if (compositor->input_loop_source) {
1187 wl_event_source_remove(compositor->input_loop_source);
1188 compositor->input_loop_source = NULL;
1189 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001190}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001191
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001192WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001193weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1194{
1195 struct weston_output *output;
1196
1197 wl_list_for_each(output, &compositor->output_list, link)
1198 weston_output_schedule_repaint(output);
1199}
1200
1201WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001202weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001203{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001204 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001205 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001206
Scott Moreau9d1b1122012-06-08 19:40:53 -06001207 output = container_of(compositor->output_list.next,
1208 struct weston_output, link);
1209
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001210 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001211 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001212 return;
1213
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001214 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001215 surface = weston_surface_create(compositor);
John Kåre Alsakere2e3d072012-10-12 12:25:09 +02001216 if (!surface)
1217 return;
1218
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001219 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001220 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001221 wl_list_insert(&compositor->fade_layer.surface_list,
1222 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03001223 weston_surface_update_transform(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001224 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001225 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001226 }
1227
1228 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001229 if (wl_list_empty(&compositor->fade.animation.link)) {
1230 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001231 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001232 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001233 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001234}
1235
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001236static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001237surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001238{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001239 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001240}
1241
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001242static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001243surface_attach(struct wl_client *client,
1244 struct wl_resource *resource,
1245 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1246{
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001247 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001248 struct wl_buffer *buffer = NULL;
1249
1250 if (buffer_resource)
1251 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001252
Pekka Paalanende685b82012-12-04 15:58:12 +02001253 /* Attach, attach, without commit in between does not send
1254 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001255 if (surface->pending.buffer)
1256 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001257
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001258 surface->pending.sx = sx;
1259 surface->pending.sy = sy;
1260 surface->pending.buffer = buffer;
1261 if (buffer) {
1262 wl_signal_add(&buffer->resource.destroy_signal,
1263 &surface->pending.buffer_destroy_listener);
1264 surface->pending.remove_contents = 0;
1265 } else {
1266 surface->pending.remove_contents = 1;
1267 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001268}
1269
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001270static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001271surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001272 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001273 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001274{
Pekka Paalanen8e159182012-10-10 12:49:25 +03001275 struct weston_surface *surface = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001276
Pekka Paalanen8e159182012-10-10 12:49:25 +03001277 pixman_region32_union_rect(&surface->pending.damage,
1278 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001279 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001280}
1281
Kristian Høgsberg33418202011-08-16 23:01:28 -04001282static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001283destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001284{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001285 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001286
1287 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001288 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001289}
1290
1291static void
1292surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001293 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001294{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001295 struct weston_frame_callback *cb;
Pekka Paalanenbc106382012-10-10 12:49:31 +03001296 struct weston_surface *surface = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001297
1298 cb = malloc(sizeof *cb);
1299 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001300 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001301 return;
1302 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001303
Kristian Høgsberg33418202011-08-16 23:01:28 -04001304 cb->resource.object.interface = &wl_callback_interface;
1305 cb->resource.object.id = callback;
1306 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001307 cb->resource.client = client;
1308 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001309
1310 wl_client_add_resource(client, &cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001311 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001312}
1313
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001314static void
1315surface_set_opaque_region(struct wl_client *client,
1316 struct wl_resource *resource,
1317 struct wl_resource *region_resource)
1318{
1319 struct weston_surface *surface = resource->data;
1320 struct weston_region *region;
1321
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001322 if (region_resource) {
1323 region = region_resource->data;
Pekka Paalanen512dde82012-10-10 12:49:27 +03001324 pixman_region32_copy(&surface->pending.opaque,
1325 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001326 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001327 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001328 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001329}
1330
1331static void
1332surface_set_input_region(struct wl_client *client,
1333 struct wl_resource *resource,
1334 struct wl_resource *region_resource)
1335{
1336 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001337 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001338
1339 if (region_resource) {
1340 region = region_resource->data;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001341 pixman_region32_copy(&surface->pending.input,
1342 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001343 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001344 pixman_region32_fini(&surface->pending.input);
1345 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001346 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001347}
1348
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001349static int
1350surface_pending_buffer_has_different_size(struct weston_surface *surface)
1351{
1352 int width, height;
1353
1354 switch (surface->pending.buffer_transform) {
1355 case WL_OUTPUT_TRANSFORM_90:
1356 case WL_OUTPUT_TRANSFORM_270:
1357 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1358 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1359 height = surface->pending.buffer->width;
1360 width = surface->pending.buffer->height;
1361 break;
1362 default:
1363 width = surface->pending.buffer->width;
1364 height = surface->pending.buffer->height;
1365 }
1366
1367 if (width == surface->geometry.width &&
1368 height == surface->geometry.height)
1369 return 0;
1370 else
1371 return 1;
1372}
1373
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001374static void
1375surface_commit(struct wl_client *client, struct wl_resource *resource)
1376{
1377 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001378 pixman_region32_t opaque;
1379
1380 if (surface->pending.sx || surface->pending.sy ||
1381 (surface->pending.buffer &&
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001382 surface_pending_buffer_has_different_size(surface)))
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001383 surface->geometry.dirty = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001384
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001385 /* wl_surface.set_buffer_rotation */
1386 surface->buffer_transform = surface->pending.buffer_transform;
1387
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001388 /* wl_surface.attach */
1389 if (surface->pending.buffer || surface->pending.remove_contents)
1390 weston_surface_attach(surface, surface->pending.buffer);
1391
Pekka Paalanende685b82012-12-04 15:58:12 +02001392 if (surface->buffer_ref.buffer && surface->configure)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001393 surface->configure(surface, surface->pending.sx,
1394 surface->pending.sy);
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001395 surface->pending.sx = 0;
1396 surface->pending.sy = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001397
1398 /* wl_surface.damage */
1399 pixman_region32_union(&surface->damage, &surface->damage,
1400 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001401 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1402 0, 0,
1403 surface->geometry.width,
1404 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001405 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001406
1407 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001408 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001409 surface->geometry.width,
1410 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001411 pixman_region32_intersect(&opaque,
1412 &opaque, &surface->pending.opaque);
1413
1414 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1415 pixman_region32_copy(&surface->opaque, &opaque);
1416 surface->geometry.dirty = 1;
1417 }
1418
1419 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001420
1421 /* wl_surface.set_input_region */
1422 pixman_region32_fini(&surface->input);
1423 pixman_region32_init_rect(&surface->input, 0, 0,
1424 surface->geometry.width,
1425 surface->geometry.height);
1426 pixman_region32_intersect(&surface->input,
1427 &surface->input, &surface->pending.input);
1428
Pekka Paalanenbc106382012-10-10 12:49:31 +03001429 /* wl_surface.frame */
1430 wl_list_insert_list(&surface->frame_callback_list,
1431 &surface->pending.frame_callback_list);
1432 wl_list_init(&surface->pending.frame_callback_list);
1433
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001434 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001435}
1436
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001437static void
1438surface_set_buffer_transform(struct wl_client *client,
1439 struct wl_resource *resource, int transform)
1440{
1441 struct weston_surface *surface = resource->data;
1442
1443 surface->pending.buffer_transform = transform;
1444}
1445
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001446static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001447 surface_destroy,
1448 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001449 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001450 surface_frame,
1451 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001452 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001453 surface_commit,
1454 surface_set_buffer_transform
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001455};
1456
1457static void
1458compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001459 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001460{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001461 struct weston_compositor *ec = resource->data;
1462 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001463
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001464 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001465 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001466 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001467 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001468 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001469
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001470 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001471
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001472 surface->surface.resource.object.id = id;
1473 surface->surface.resource.object.interface = &wl_surface_interface;
1474 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001475 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001476 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001477
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001478 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001479}
1480
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001481static void
1482destroy_region(struct wl_resource *resource)
1483{
1484 struct weston_region *region =
1485 container_of(resource, struct weston_region, resource);
1486
1487 pixman_region32_fini(&region->region);
1488 free(region);
1489}
1490
1491static void
1492region_destroy(struct wl_client *client, struct wl_resource *resource)
1493{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001494 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001495}
1496
1497static void
1498region_add(struct wl_client *client, struct wl_resource *resource,
1499 int32_t x, int32_t y, int32_t width, int32_t height)
1500{
1501 struct weston_region *region = resource->data;
1502
1503 pixman_region32_union_rect(&region->region, &region->region,
1504 x, y, width, height);
1505}
1506
1507static void
1508region_subtract(struct wl_client *client, struct wl_resource *resource,
1509 int32_t x, int32_t y, int32_t width, int32_t height)
1510{
1511 struct weston_region *region = resource->data;
1512 pixman_region32_t rect;
1513
1514 pixman_region32_init_rect(&rect, x, y, width, height);
1515 pixman_region32_subtract(&region->region, &region->region, &rect);
1516 pixman_region32_fini(&rect);
1517}
1518
1519static const struct wl_region_interface region_interface = {
1520 region_destroy,
1521 region_add,
1522 region_subtract
1523};
1524
1525static void
1526compositor_create_region(struct wl_client *client,
1527 struct wl_resource *resource, uint32_t id)
1528{
1529 struct weston_region *region;
1530
1531 region = malloc(sizeof *region);
1532 if (region == NULL) {
1533 wl_resource_post_no_memory(resource);
1534 return;
1535 }
1536
1537 region->resource.destroy = destroy_region;
1538
1539 region->resource.object.id = id;
1540 region->resource.object.interface = &wl_region_interface;
1541 region->resource.object.implementation =
1542 (void (**)(void)) &region_interface;
1543 region->resource.data = region;
1544
1545 pixman_region32_init(&region->region);
1546
1547 wl_client_add_resource(client, &region->resource);
1548}
1549
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001550static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001551 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001552 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001553};
1554
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001555WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001556weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001557{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001558 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1559 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001560
1561 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001562 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001563}
1564
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001565static void
1566weston_compositor_dpms_on(struct weston_compositor *compositor)
1567{
1568 struct weston_output *output;
1569
1570 wl_list_for_each(output, &compositor->output_list, link)
1571 if (output->set_dpms)
1572 output->set_dpms(output, WESTON_DPMS_ON);
1573}
1574
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001575WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001576weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001577{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001578 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1579 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001580 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001581 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001582 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001583 }
1584}
1585
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001586static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001587weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001588{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001589 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001590 compositor->idle_inhibit++;
1591}
1592
1593static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001594weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001595{
1596 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001597 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001598}
1599
1600static int
1601idle_handler(void *data)
1602{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001603 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001604
1605 if (compositor->idle_inhibit)
1606 return 1;
1607
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001608 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001609
1610 return 1;
1611}
1612
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001613WL_EXPORT void
1614weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1615{
1616 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001617 pixman_region32_init(&plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001618 plane->x = x;
1619 plane->y = y;
1620}
1621
1622WL_EXPORT void
1623weston_plane_release(struct weston_plane *plane)
1624{
1625 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001626 pixman_region32_fini(&plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001627}
1628
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001629static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001630weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001631
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001632static void
Daniel Stone37816df2012-05-16 18:45:18 +01001633clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001634{
Daniel Stone37816df2012-05-16 18:45:18 +01001635 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001636 struct weston_output *output, *prev = NULL;
1637 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001638
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001639 x = wl_fixed_to_int(*fx);
1640 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001641 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1642 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001643
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001644 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001645 if (pixman_region32_contains_point(&output->region,
1646 x, y, NULL))
1647 valid = 1;
1648 if (pixman_region32_contains_point(&output->region,
1649 old_x, old_y, NULL))
1650 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001651 }
Daniel Stone37816df2012-05-16 18:45:18 +01001652
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001653 if (!valid) {
1654 if (x < prev->x)
1655 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001656 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001657 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001658 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001659 if (y < prev->y)
1660 *fy = wl_fixed_from_int(prev->y);
1661 else if (y >= prev->y + prev->current->height)
1662 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001663 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001664 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001665}
1666
1667WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001668notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001669{
1670 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001671 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001672 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001673 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001674 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001675
1676 weston_compositor_activity(ec);
1677
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001678 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001679
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001680 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001681
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001682 pointer->x = x;
1683 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001684
1685 ix = wl_fixed_to_int(x);
1686 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001687
Scott Moreauccbf29d2012-02-22 14:21:41 -07001688 wl_list_for_each(output, &ec->output_list, link)
1689 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001690 pixman_region32_contains_point(&output->region,
1691 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06001692 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001693
Daniel Stone37816df2012-05-16 18:45:18 +01001694 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001695 interface = pointer->grab->interface;
1696 interface->motion(pointer->grab, time,
1697 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001698
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001699 if (seat->sprite) {
1700 weston_surface_set_position(seat->sprite,
1701 ix - seat->hotspot_x,
1702 iy - seat->hotspot_y);
1703 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001704 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001705}
1706
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001707WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001708weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001709 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001710{
Daniel Stone37816df2012-05-16 18:45:18 +01001711 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001712
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001713 if (seat->seat.keyboard) {
1714 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1715 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04001716 }
1717
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001718 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001719}
1720
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001721WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001722notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001723 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001724{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001725 struct weston_compositor *compositor = seat->compositor;
1726 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001727 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001728 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001729 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001730
Daniel Stone4dbadb12012-05-30 16:31:51 +01001731 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001732 if (compositor->ping_handler && focus)
1733 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001734 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001735 if (pointer->button_count == 0) {
1736 pointer->grab_button = button;
1737 pointer->grab_time = time;
1738 pointer->grab_x = pointer->x;
1739 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001740 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001741 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001742 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001743 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001744 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001745 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001746
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001747 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001748 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001749
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001750 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001751
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001752 if (pointer->button_count == 1)
1753 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001754 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001755}
1756
Scott Moreau210d0792012-03-22 10:47:01 -06001757WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001758notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01001759 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001760{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001761 struct weston_compositor *compositor = seat->compositor;
1762 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001763 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001764 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001765 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1766
1767 if (compositor->ping_handler && focus)
1768 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001769
1770 weston_compositor_activity(compositor);
1771
Scott Moreau6a3633d2012-03-20 08:47:59 -06001772 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001773 weston_compositor_run_axis_binding(compositor, seat,
1774 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06001775 else
1776 return;
1777
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001778 if (pointer->focus_resource)
1779 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01001780 value);
Scott Moreau210d0792012-03-22 10:47:01 -06001781}
1782
Daniel Stone05d58682012-06-22 13:21:38 +01001783WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001784notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001785{
Daniel Stone05d58682012-06-22 13:21:38 +01001786 struct wl_keyboard *keyboard = &seat->keyboard;
1787 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01001788 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001789 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01001790 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01001791 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01001792
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001793 /* Serialize and update our internal state, checking to see if it's
1794 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01001795 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1796 XKB_STATE_DEPRESSED);
1797 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1798 XKB_STATE_LATCHED);
1799 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1800 XKB_STATE_LOCKED);
1801 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01001802 XKB_STATE_EFFECTIVE);
1803
Daniel Stone71c38772012-06-22 13:21:30 +01001804 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
1805 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
1806 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
1807 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01001808 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01001809
Daniel Stone71c38772012-06-22 13:21:30 +01001810 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
1811 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
1812 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
1813 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001814
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001815 /* And update the modifier_state for bindings. */
1816 mods_lookup = mods_depressed | mods_latched;
1817 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01001818 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001819 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01001820 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001821 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01001822 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001823 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04001824 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
1825 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01001826
Daniel Stone8c8164f2012-05-30 16:31:45 +01001827 /* Finally, notify the compositor that LEDs have changed. */
1828 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001829 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001830 leds |= LED_NUM_LOCK;
1831 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001832 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001833 leds |= LED_CAPS_LOCK;
1834 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001835 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001836 leds |= LED_SCROLL_LOCK;
1837 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01001838 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01001839 seat->xkb_state.leds = leds;
1840
Daniel Stone05d58682012-06-22 13:21:38 +01001841 if (changed) {
1842 grab->interface->modifiers(grab,
1843 serial,
1844 keyboard->modifiers.mods_depressed,
1845 keyboard->modifiers.mods_latched,
1846 keyboard->modifiers.mods_locked,
1847 keyboard->modifiers.group);
1848 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001849}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001850
Daniel Stone05d58682012-06-22 13:21:38 +01001851static void
1852update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001853 enum wl_keyboard_key_state state)
1854{
1855 enum xkb_key_direction direction;
1856
1857 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1858 direction = XKB_KEY_DOWN;
1859 else
1860 direction = XKB_KEY_UP;
1861
1862 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1863 * broken keycode system, which starts at 8. */
1864 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1865
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001866 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001867}
1868
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001869WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001870notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001871 enum wl_keyboard_key_state state,
1872 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001873{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001874 struct weston_compositor *compositor = seat->compositor;
1875 struct wl_keyboard *keyboard = seat->seat.keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01001876 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001877 (struct weston_surface *) keyboard->focus;
1878 struct wl_keyboard_grab *grab = keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001879 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001880 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001881
Daniel Stonec9785ea2012-05-30 16:31:52 +01001882 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001883 if (compositor->ping_handler && focus)
1884 compositor->ping_handler(focus, serial);
1885
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001886 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001887 keyboard->grab_key = key;
1888 keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07001889 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001890 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001891 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001892
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001893 end = keyboard->keys.data + keyboard->keys.size;
1894 for (k = keyboard->keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01001895 if (*k == key) {
1896 /* Ignore server-generated repeats. */
1897 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1898 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001899 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01001900 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001901 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001902 keyboard->keys.size = (void *) end - keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001903 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001904 k = wl_array_add(&keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001905 *k = key;
1906 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001907
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001908 if (grab == &keyboard->default_grab) {
1909 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001910 state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001911 grab = keyboard->grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06001912 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001913
Daniel Stone7ace3902012-05-30 16:31:40 +01001914 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01001915
1916 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001917 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01001918 wl_display_get_serial(compositor->wl_display),
1919 key,
1920 state);
1921 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001922}
1923
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001924WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001925notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01001926 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001927{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001928 struct weston_compositor *compositor = seat->compositor;
1929 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001930
1931 if (output) {
Kristian Høgsberg00fbbe62012-10-23 13:04:09 -04001932 clip_pointer_motion(seat, &x, &y);
Daniel Stone37816df2012-05-16 18:45:18 +01001933 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001934 x - pointer->x,
1935 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001936
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001937 pointer->x = x;
1938 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001939 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001940 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001941 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001942 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03001943 /* FIXME: We should call wl_pointer_set_focus(seat,
1944 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001945 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001946}
1947
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001948static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001949destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001950{
Daniel Stone37816df2012-05-16 18:45:18 +01001951 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001952
Daniel Stone37816df2012-05-16 18:45:18 +01001953 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001954 saved_kbd_focus_listener);
1955
Daniel Stone37816df2012-05-16 18:45:18 +01001956 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001957}
1958
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001959WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001960notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001961 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001962{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001963 struct weston_compositor *compositor = seat->compositor;
1964 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001965 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001966 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001967
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001968 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001969 wl_array_copy(&keyboard->keys, keys);
1970 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001971 weston_compositor_idle_inhibit(compositor);
1972 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001973 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001974 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001975 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001976
Daniel Stoneef172672012-06-22 13:21:32 +01001977 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001978 wl_array_for_each(k, &keyboard->keys) {
1979 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01001980 WL_KEYBOARD_KEY_STATE_PRESSED);
1981 }
1982
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001983 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001984
1985 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001986 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1987 wl_keyboard_set_focus(keyboard, surface);
1988 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001989 }
1990}
1991
1992WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001993notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01001994{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001995 struct weston_compositor *compositor = seat->compositor;
1996 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001997 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001998
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001999 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002000 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002001 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002002 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002003 WL_KEYBOARD_KEY_STATE_RELEASED);
2004 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002005
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002006 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002007
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002008 if (keyboard->focus) {
2009 seat->saved_kbd_focus = keyboard->focus;
2010 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01002011 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002012 wl_signal_add(&keyboard->focus->resource.destroy_signal,
2013 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002014 }
2015
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002016 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002017 /* FIXME: We really need keyboard grab cancel here to
2018 * let the grab shut down properly. As it is we leak
2019 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002020 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002021}
2022
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002023static void
Daniel Stone37816df2012-05-16 18:45:18 +01002024touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002025{
Daniel Stone37816df2012-05-16 18:45:18 +01002026 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002027 struct wl_resource *resource;
2028
Pekka Paalanen56464252012-07-31 13:21:08 +03002029 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002030 return;
2031
Pekka Paalanen56464252012-07-31 13:21:08 +03002032 if (seat->touch->focus_resource)
2033 wl_list_remove(&seat->touch->focus_listener.link);
2034 seat->touch->focus = NULL;
2035 seat->touch->focus_resource = NULL;
2036
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002037 if (surface) {
2038 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01002039 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04002040 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002041 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02002042 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002043 return;
2044 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002045
Daniel Stone37816df2012-05-16 18:45:18 +01002046 seat->touch->focus = surface;
2047 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03002048 wl_signal_add(&resource->destroy_signal,
2049 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002050 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002051}
2052
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002053/**
2054 * notify_touch - emulates button touches and notifies surfaces accordingly.
2055 *
2056 * It assumes always the correct cycle sequence until it gets here: touch_down
2057 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2058 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002059 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002060 */
2061WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002062notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002063 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002064{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002065 struct weston_compositor *ec = seat->compositor;
2066 struct wl_touch *touch = seat->seat.touch;
Matt Roper47c1b982012-10-10 16:56:53 -07002067 struct wl_touch_grab *grab = touch->grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002068 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002069 wl_fixed_t sx, sy;
Matt Roper47c1b982012-10-10 16:56:53 -07002070
2071 /* Update grab's global coordinates. */
2072 touch->grab_x = x;
2073 touch->grab_y = y;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002074
2075 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01002076 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002077 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002078
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002079 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002080
2081 /* the first finger down picks the surface, and all further go
2082 * to that surface for the remainder of the touch session i.e.
2083 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002084 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002085 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002086 touch_set_focus(seat, &es->surface);
2087 } else if (touch->focus) {
2088 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002089 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002090 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002091
Matt Roper47c1b982012-10-10 16:56:53 -07002092 grab->interface->down(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002093 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002094 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002095 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002096 if (!es)
2097 break;
2098
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002099 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Matt Roper47c1b982012-10-10 16:56:53 -07002100 grab->interface->motion(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002101 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002102 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002103 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002104 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002105
Matt Roper47c1b982012-10-10 16:56:53 -07002106 grab->interface->up(grab, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002107 if (seat->num_tp == 0)
2108 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002109 break;
2110 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002111}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002112
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04002113static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002114pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
2115{
2116 struct weston_seat *seat = container_of(listener, struct weston_seat,
2117 sprite_destroy_listener);
2118
2119 seat->sprite = NULL;
2120}
2121
2122static void
2123pointer_cursor_surface_configure(struct weston_surface *es,
2124 int32_t dx, int32_t dy)
2125{
2126 struct weston_seat *seat = es->private;
2127 int x, y;
2128
2129 assert(es == seat->sprite);
2130
2131 seat->hotspot_x -= dx;
2132 seat->hotspot_y -= dy;
2133
2134 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
2135 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
2136
2137 weston_surface_configure(seat->sprite, x, y,
Pekka Paalanende685b82012-12-04 15:58:12 +02002138 es->buffer_ref.buffer->width,
2139 es->buffer_ref.buffer->height);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002140
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002141 empty_region(&es->pending.input);
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03002142
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002143 if (!weston_surface_is_mapped(es)) {
2144 wl_list_insert(&es->compositor->cursor_layer.surface_list,
2145 &es->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002146 weston_surface_update_transform(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002147 }
2148}
2149
2150static void
2151pointer_unmap_sprite(struct weston_seat *seat)
2152{
2153 if (weston_surface_is_mapped(seat->sprite))
2154 weston_surface_unmap(seat->sprite);
2155
2156 wl_list_remove(&seat->sprite_destroy_listener.link);
2157 seat->sprite->configure = NULL;
2158 seat->sprite->private = NULL;
2159 seat->sprite = NULL;
2160}
2161
2162static void
2163pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2164 uint32_t serial, struct wl_resource *surface_resource,
2165 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002166{
Daniel Stone37816df2012-05-16 18:45:18 +01002167 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002168 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002169
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002170 if (surface_resource)
Pekka Paalanen6c71ee12012-10-10 12:49:30 +03002171 surface = surface_resource->data;
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002172
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04002173 if (seat->seat.pointer->focus == NULL)
2174 return;
2175 if (seat->seat.pointer->focus->resource.client != client)
2176 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002177 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002178 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002179
2180 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002181 if (surface->configure) {
2182 wl_resource_post_error(&surface->surface.resource,
2183 WL_DISPLAY_ERROR_INVALID_OBJECT,
2184 "surface->configure already "
2185 "set");
2186 return;
2187 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002188 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002189
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002190 if (seat->sprite)
2191 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002192
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002193 if (!surface)
2194 return;
2195
2196 wl_signal_add(&surface->surface.resource.destroy_signal,
2197 &seat->sprite_destroy_listener);
2198
2199 surface->configure = pointer_cursor_surface_configure;
2200 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002201 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002202 seat->hotspot_x = x;
2203 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002204
Pekka Paalanende685b82012-12-04 15:58:12 +02002205 if (surface->buffer_ref.buffer)
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04002206 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002207}
2208
Daniel Stone37816df2012-05-16 18:45:18 +01002209static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002210 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002211};
2212
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002213static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002214handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002215{
Daniel Stone37816df2012-05-16 18:45:18 +01002216 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002217
Daniel Stone37816df2012-05-16 18:45:18 +01002218 seat = container_of(listener, struct weston_seat,
2219 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002220
Daniel Stone37816df2012-05-16 18:45:18 +01002221 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002222}
2223
Casey Dahlin9074db52012-04-19 22:50:09 -04002224static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002225{
2226 wl_list_remove(&resource->link);
2227 free(resource);
2228}
2229
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002230static void
Daniel Stone37816df2012-05-16 18:45:18 +01002231seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2232 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002233{
Daniel Stone37816df2012-05-16 18:45:18 +01002234 struct weston_seat *seat = resource->data;
2235 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002236
Daniel Stone37816df2012-05-16 18:45:18 +01002237 if (!seat->seat.pointer)
2238 return;
2239
2240 cr = wl_client_add_object(client, &wl_pointer_interface,
2241 &pointer_interface, id, seat);
2242 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002243 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002244
2245 if (seat->seat.pointer->focus &&
2246 seat->seat.pointer->focus->resource.client == client) {
2247 struct weston_surface *surface;
2248 wl_fixed_t sx, sy;
2249
2250 surface = (struct weston_surface *) seat->seat.pointer->focus;
2251 weston_surface_from_global_fixed(surface,
2252 seat->seat.pointer->x,
2253 seat->seat.pointer->y,
2254 &sx,
2255 &sy);
2256 wl_pointer_set_focus(seat->seat.pointer,
2257 seat->seat.pointer->focus,
2258 sx,
2259 sy);
2260 }
Daniel Stone37816df2012-05-16 18:45:18 +01002261}
2262
2263static void
2264seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2265 uint32_t id)
2266{
2267 struct weston_seat *seat = resource->data;
2268 struct wl_resource *cr;
2269
2270 if (!seat->seat.keyboard)
2271 return;
2272
2273 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2274 seat);
2275 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002276 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002277
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002278 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2279 seat->xkb_info.keymap_fd,
2280 seat->xkb_info.keymap_size);
2281
Daniel Stone17b13bd2012-05-30 16:31:39 +01002282 if (seat->seat.keyboard->focus &&
2283 seat->seat.keyboard->focus->resource.client == client) {
2284 wl_keyboard_set_focus(seat->seat.keyboard,
2285 seat->seat.keyboard->focus);
2286 }
Daniel Stone37816df2012-05-16 18:45:18 +01002287}
2288
2289static void
2290seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2291 uint32_t id)
2292{
2293 struct weston_seat *seat = resource->data;
2294 struct wl_resource *cr;
2295
2296 if (!seat->seat.touch)
2297 return;
2298
2299 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2300 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002301 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002302}
2303
2304static const struct wl_seat_interface seat_interface = {
2305 seat_get_pointer,
2306 seat_get_keyboard,
2307 seat_get_touch,
2308};
2309
2310static void
2311bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2312{
2313 struct wl_seat *seat = data;
2314 struct wl_resource *resource;
2315 enum wl_seat_capability caps = 0;
2316
2317 resource = wl_client_add_object(client, &wl_seat_interface,
2318 &seat_interface, id, data);
2319 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002320 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002321
2322 if (seat->pointer)
2323 caps |= WL_SEAT_CAPABILITY_POINTER;
2324 if (seat->keyboard)
2325 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2326 if (seat->touch)
2327 caps |= WL_SEAT_CAPABILITY_TOUCH;
2328
2329 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002330}
2331
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002332static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002333device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002334{
Daniel Stone37816df2012-05-16 18:45:18 +01002335 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002336
Daniel Stone37816df2012-05-16 18:45:18 +01002337 seat = container_of(listener, struct weston_seat,
2338 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002339
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002340 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002341}
2342
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002343static void weston_compositor_xkb_init(struct weston_compositor *ec,
2344 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002345{
Daniel Stonee379da92012-05-30 16:32:04 +01002346 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002347 ec->xkb_context = xkb_context_new(0);
2348 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002349 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002350 exit(1);
2351 }
Daniel Stone994679a2012-05-30 16:31:43 +01002352 }
2353
2354 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002355 ec->xkb_names = *names;
2356 if (!ec->xkb_names.rules)
2357 ec->xkb_names.rules = strdup("evdev");
2358 if (!ec->xkb_names.model)
2359 ec->xkb_names.model = strdup("pc105");
2360 if (!ec->xkb_names.layout)
2361 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002362}
2363
Daniel Stonee379da92012-05-30 16:32:04 +01002364static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2365{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002366 if (xkb_info->keymap)
2367 xkb_map_unref(xkb_info->keymap);
2368
2369 if (xkb_info->keymap_area)
2370 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2371 if (xkb_info->keymap_fd >= 0)
2372 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002373}
2374
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002375static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2376{
Daniel Stonee379da92012-05-30 16:32:04 +01002377 free((char *) ec->xkb_names.rules);
2378 free((char *) ec->xkb_names.model);
2379 free((char *) ec->xkb_names.layout);
2380 free((char *) ec->xkb_names.variant);
2381 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002382
Daniel Stonee379da92012-05-30 16:32:04 +01002383 xkb_info_destroy(&ec->xkb_info);
2384 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002385}
2386
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002387static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002388weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002389{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002390 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002391
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002392 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2393 XKB_MOD_NAME_SHIFT);
2394 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2395 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002396 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2397 XKB_MOD_NAME_CTRL);
2398 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2399 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002400 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2401 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002402 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2403 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002404 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002405
2406 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2407 XKB_LED_NAME_NUM);
2408 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2409 XKB_LED_NAME_CAPS);
2410 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2411 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002412
2413 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2414 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002415 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002416 exit(EXIT_FAILURE);
2417 }
2418 xkb_info->keymap_size = strlen(keymap_str) + 1;
2419
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002420 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002421 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002422 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002423 (unsigned long) xkb_info->keymap_size);
2424 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002425 }
2426
2427 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2428 PROT_READ | PROT_WRITE,
2429 MAP_SHARED, xkb_info->keymap_fd, 0);
2430 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002431 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002432 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002433 goto err_dev_zero;
2434 }
2435 strcpy(xkb_info->keymap_area, keymap_str);
2436 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002437
2438 return;
2439
2440err_dev_zero:
2441 close(xkb_info->keymap_fd);
2442 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002443err_keymap_str:
2444 free(keymap_str);
2445 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002446}
2447
2448static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002449weston_compositor_build_global_keymap(struct weston_compositor *ec)
2450{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002451 if (ec->xkb_info.keymap != NULL)
2452 return;
2453
Daniel Stonee379da92012-05-30 16:32:04 +01002454 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002455 &ec->xkb_names,
2456 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002457 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002458 weston_log("failed to compile global XKB keymap\n");
2459 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002460 "options %s",
2461 ec->xkb_names.rules, ec->xkb_names.model,
2462 ec->xkb_names.layout, ec->xkb_names.variant,
2463 ec->xkb_names.options);
2464 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002465 }
2466
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002467 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002468}
2469
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002470WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002471weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002472{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002473 if (seat->has_keyboard)
2474 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002475
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002476 if (keymap != NULL) {
2477 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002478 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002479 }
2480 else {
2481 weston_compositor_build_global_keymap(seat->compositor);
2482 seat->xkb_info = seat->compositor->xkb_info;
2483 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2484 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002485
2486 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2487 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002488 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01002489 exit(1);
2490 }
2491
Daniel Stone71c38772012-06-22 13:21:30 +01002492 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002493
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002494 wl_keyboard_init(&seat->keyboard);
2495 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2496
2497 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01002498}
2499
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002500WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002501weston_seat_init_pointer(struct weston_seat *seat)
2502{
2503 if (seat->has_pointer)
2504 return;
2505
2506 wl_pointer_init(&seat->pointer);
2507 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2508
2509 seat->has_pointer = 1;
2510}
2511
2512WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002513weston_seat_init_touch(struct weston_seat *seat)
2514{
2515 if (seat->has_touch)
2516 return;
2517
2518 wl_touch_init(&seat->touch);
2519 wl_seat_set_touch(&seat->seat, &seat->touch);
2520
2521 seat->has_touch = 1;
2522}
2523
2524WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002525weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002526{
Daniel Stone37816df2012-05-16 18:45:18 +01002527 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002528 seat->has_pointer = 0;
2529 seat->has_keyboard = 0;
2530 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002531
Daniel Stone37816df2012-05-16 18:45:18 +01002532 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2533 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002534
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002535 seat->sprite = NULL;
2536 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002537
Daniel Stone37816df2012-05-16 18:45:18 +01002538 seat->compositor = ec;
2539 seat->hotspot_x = 16;
2540 seat->hotspot_y = 16;
2541 seat->modifier_state = 0;
2542 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002543
Daniel Stone37816df2012-05-16 18:45:18 +01002544 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002545 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002546
Daniel Stone37816df2012-05-16 18:45:18 +01002547 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002548
Daniel Stone37816df2012-05-16 18:45:18 +01002549 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2550 wl_signal_add(&seat->seat.drag_icon_signal,
2551 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002552
2553 clipboard_create(seat);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002554 wl_signal_emit(&ec->seat_created_signal, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002555}
2556
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002557WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002558weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002559{
Daniel Stone37816df2012-05-16 18:45:18 +01002560 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002561 /* The global object is destroyed at wl_display_destroy() time. */
2562
Daniel Stone37816df2012-05-16 18:45:18 +01002563 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002564 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002565
Daniel Stone74419a22012-05-30 16:32:02 +01002566 if (seat->xkb_state.state != NULL)
2567 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01002568 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002569
Daniel Stone37816df2012-05-16 18:45:18 +01002570 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002571}
2572
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002573static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002574drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2575{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002576 empty_region(&es->pending.input);
2577
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002578 weston_surface_configure(es,
2579 es->geometry.x + sx, es->geometry.y + sy,
Pekka Paalanende685b82012-12-04 15:58:12 +02002580 es->buffer_ref.buffer->width,
2581 es->buffer_ref.buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002582}
2583
2584static int
Daniel Stone37816df2012-05-16 18:45:18 +01002585device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002586 struct weston_surface *surface)
2587{
Daniel Stone37816df2012-05-16 18:45:18 +01002588 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002589
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002590 if (surface->configure) {
2591 wl_resource_post_error(&surface->surface.resource,
2592 WL_DISPLAY_ERROR_INVALID_OBJECT,
2593 "surface->configure already set");
2594 return 0;
2595 }
2596
Daniel Stone37816df2012-05-16 18:45:18 +01002597 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002598
Daniel Stone37816df2012-05-16 18:45:18 +01002599 weston_surface_set_position(ws->drag_surface,
2600 wl_fixed_to_double(seat->pointer->x),
2601 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002602
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002603 surface->configure = drag_surface_configure;
2604
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002605 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002606 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002607
2608 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002609}
2610
2611static void
Daniel Stone37816df2012-05-16 18:45:18 +01002612device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002613{
Ander Conselvan de Oliveira5fd55802012-10-11 14:06:19 +03002614 if (weston_surface_is_mapped(seat->drag_surface))
2615 weston_surface_unmap(seat->drag_surface);
2616
Daniel Stone37816df2012-05-16 18:45:18 +01002617 seat->drag_surface->configure = NULL;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002618 empty_region(&seat->drag_surface->pending.input);
Daniel Stone37816df2012-05-16 18:45:18 +01002619 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2620 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002621}
2622
2623static void
Daniel Stone37816df2012-05-16 18:45:18 +01002624device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002625{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002626 struct wl_list *list;
2627
Daniel Stone37816df2012-05-16 18:45:18 +01002628 if (weston_surface_is_mapped(seat->drag_surface) ||
Pekka Paalanende685b82012-12-04 15:58:12 +02002629 !seat->drag_surface->buffer_ref.buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002630 return;
2631
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002632 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2633 list = &seat->sprite->layer_link;
2634 else
2635 list = &seat->compositor->cursor_layer.surface_list;
2636
2637 wl_list_insert(list, &seat->drag_surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002638 weston_surface_update_transform(seat->drag_surface);
Daniel Stone37816df2012-05-16 18:45:18 +01002639 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002640}
2641
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002642static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002643weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01002644 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002645{
2646 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002647
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002648 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002649 return;
2650
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002651 if (seat->drag_surface && seat->seat.drag_surface &&
2652 (&seat->drag_surface->surface.resource !=
2653 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002654 /* between calls to this funcion we got a new drag_surface */
2655 surface_changed = 1;
2656
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002657 if (!seat->seat.drag_surface || surface_changed) {
2658 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002659 if (!surface_changed)
2660 return;
2661 }
2662
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002663 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002664 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002665 seat->seat.drag_surface;
2666 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002667 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002668 }
2669
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002670 /* the client may not have attached a buffer to the drag surface
2671 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002672 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002673
2674 if (!dx && !dy)
2675 return;
2676
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002677 weston_surface_set_position(seat->drag_surface,
2678 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
2679 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002680}
2681
2682WL_EXPORT void
2683weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2684{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002685 struct weston_seat *seat;
2686
2687 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002688 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002689}
2690
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002691static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002692bind_output(struct wl_client *client,
2693 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002694{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002695 struct weston_output *output = data;
2696 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002697 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002698
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002699 resource = wl_client_add_object(client,
2700 &wl_output_interface, NULL, id, data);
2701
Casey Dahlin9074db52012-04-19 22:50:09 -04002702 wl_list_insert(&output->resource_list, &resource->link);
2703 resource->destroy = unbind_resource;
2704
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002705 wl_output_send_geometry(resource,
2706 output->x,
2707 output->y,
2708 output->mm_width,
2709 output->mm_height,
2710 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002711 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002712 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002713
2714 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002715 wl_output_send_mode(resource,
2716 mode->flags,
2717 mode->width,
2718 mode->height,
2719 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002720 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002721}
2722
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002723WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002724weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002725{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002726 struct weston_compositor *c = output->compositor;
2727
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002728 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002729 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002730 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002731
2732 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002733}
2734
Scott Moreau1bad5db2012-08-18 01:04:05 -06002735static void
2736weston_output_compute_transform(struct weston_output *output)
2737{
2738 struct weston_matrix transform;
2739 int flip;
2740
2741 weston_matrix_init(&transform);
2742
2743 switch(output->transform) {
2744 case WL_OUTPUT_TRANSFORM_FLIPPED:
2745 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2746 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2747 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2748 flip = -1;
2749 break;
2750 default:
2751 flip = 1;
2752 break;
2753 }
2754
2755 switch(output->transform) {
2756 case WL_OUTPUT_TRANSFORM_NORMAL:
2757 case WL_OUTPUT_TRANSFORM_FLIPPED:
2758 transform.d[0] = flip;
2759 transform.d[1] = 0;
2760 transform.d[4] = 0;
2761 transform.d[5] = 1;
2762 break;
2763 case WL_OUTPUT_TRANSFORM_90:
2764 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2765 transform.d[0] = 0;
2766 transform.d[1] = -flip;
2767 transform.d[4] = 1;
2768 transform.d[5] = 0;
2769 break;
2770 case WL_OUTPUT_TRANSFORM_180:
2771 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2772 transform.d[0] = -flip;
2773 transform.d[1] = 0;
2774 transform.d[4] = 0;
2775 transform.d[5] = -1;
2776 break;
2777 case WL_OUTPUT_TRANSFORM_270:
2778 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2779 transform.d[0] = 0;
2780 transform.d[1] = flip;
2781 transform.d[4] = -1;
2782 transform.d[5] = 0;
2783 break;
2784 default:
2785 break;
2786 }
2787
2788 weston_matrix_multiply(&output->matrix, &transform);
2789}
2790
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002791WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002792weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002793{
Scott Moreau850ca422012-05-21 15:21:25 -06002794 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002795 struct weston_matrix camera;
2796 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002797
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002798 weston_matrix_init(&output->matrix);
2799 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002800 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2801 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002802
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002803 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002804 2.0 / (output->width + output->border.left + output->border.right),
2805 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2806
2807 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002808
Scott Moreauccbf29d2012-02-22 14:21:41 -07002809 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002810 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002811 weston_matrix_init(&camera);
2812 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002813 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002814 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002815 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002816 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002817 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002818 weston_matrix_multiply(&output->matrix, &modelview);
2819 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002820
Scott Moreauccbf29d2012-02-22 14:21:41 -07002821 output->dirty = 0;
2822}
2823
Scott Moreau1bad5db2012-08-18 01:04:05 -06002824static void
2825weston_output_transform_init(struct weston_output *output, uint32_t transform)
2826{
2827 output->transform = transform;
2828
2829 switch (transform) {
2830 case WL_OUTPUT_TRANSFORM_90:
2831 case WL_OUTPUT_TRANSFORM_270:
2832 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2833 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2834 /* Swap width and height */
2835 output->width = output->current->height;
2836 output->height = output->current->width;
2837 break;
2838 case WL_OUTPUT_TRANSFORM_NORMAL:
2839 case WL_OUTPUT_TRANSFORM_180:
2840 case WL_OUTPUT_TRANSFORM_FLIPPED:
2841 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2842 output->width = output->current->width;
2843 output->height = output->current->height;
2844 break;
2845 default:
2846 break;
2847 }
2848}
2849
Scott Moreauccbf29d2012-02-22 14:21:41 -07002850WL_EXPORT void
2851weston_output_move(struct weston_output *output, int x, int y)
2852{
2853 output->x = x;
2854 output->y = y;
2855
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002856 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002857 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002858 output->width,
2859 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002860}
2861
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002862WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002863weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002864 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002865{
2866 output->compositor = c;
2867 output->x = x;
2868 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002869 output->border.top = 0;
2870 output->border.bottom = 0;
2871 output->border.left = 0;
2872 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002873 output->mm_width = width;
2874 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002875 output->dirty = 1;
2876
Scott Moreau1bad5db2012-08-18 01:04:05 -06002877 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06002878 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002879
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002880 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002881 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002882
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002883 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002884 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002885 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002886
Casey Dahlin58ba1372012-04-19 22:50:08 -04002887 output->id = ffs(~output->compositor->output_id_pool) - 1;
2888 output->compositor->output_id_pool |= 1 << output->id;
2889
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002890 output->global =
2891 wl_display_add_global(c->wl_display, &wl_output_interface,
2892 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002893}
2894
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002895static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002896compositor_bind(struct wl_client *client,
2897 void *data, uint32_t version, uint32_t id)
2898{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002899 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002900
2901 wl_client_add_object(client, &wl_compositor_interface,
2902 &compositor_interface, id, compositor);
2903}
2904
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002905static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002906log_uname(void)
2907{
2908 struct utsname usys;
2909
2910 uname(&usys);
2911
2912 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2913 usys.version, usys.machine);
2914}
2915
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002916WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002917weston_compositor_init(struct weston_compositor *ec,
2918 struct wl_display *display,
2919 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04002920 char *argv[],
2921 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002922{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002923 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002924 struct xkb_rule_names xkb_names;
2925 const struct config_key keyboard_config_keys[] = {
2926 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2927 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2928 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2929 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2930 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2931 };
2932 const struct config_section cs[] = {
2933 { "keyboard",
2934 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
2935 };
2936
2937 memset(&xkb_names, 0, sizeof(xkb_names));
2938 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002939
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002940 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002941 wl_signal_init(&ec->destroy_signal);
2942 wl_signal_init(&ec->activate_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002943 wl_signal_init(&ec->kill_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002944 wl_signal_init(&ec->lock_signal);
2945 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002946 wl_signal_init(&ec->show_input_panel_signal);
2947 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002948 wl_signal_init(&ec->seat_created_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002949 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002950
Casey Dahlin58ba1372012-04-19 22:50:08 -04002951 ec->output_id_pool = 0;
2952
Kristian Høgsberga8873122011-11-23 10:39:34 -05002953 if (!wl_display_add_global(display, &wl_compositor_interface,
2954 ec, compositor_bind))
2955 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002956
Daniel Stone725c2c32012-06-22 14:04:36 +01002957 wl_list_init(&ec->surface_list);
2958 wl_list_init(&ec->layer_list);
2959 wl_list_init(&ec->seat_list);
2960 wl_list_init(&ec->output_list);
2961 wl_list_init(&ec->key_binding_list);
2962 wl_list_init(&ec->button_binding_list);
2963 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002964 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002965 wl_list_init(&ec->fade.animation.link);
2966
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002967 weston_plane_init(&ec->primary_plane, 0, 0);
2968
Daniel Stone725c2c32012-06-22 14:04:36 +01002969 weston_compositor_xkb_init(ec, &xkb_names);
2970
2971 ec->ping_handler = NULL;
2972
2973 screenshooter_create(ec);
2974 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002975 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002976
2977 wl_data_device_manager_init(ec->wl_display);
2978
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002979 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002980
Daniel Stone725c2c32012-06-22 14:04:36 +01002981 loop = wl_display_get_event_loop(ec->wl_display);
2982 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2983 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2984
2985 ec->input_loop = wl_event_loop_create();
2986
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002987 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
2988 ec->fade.animation.frame = fade_frame;
2989
2990 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2991 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2992
2993 weston_compositor_schedule_repaint(ec);
2994
Daniel Stone725c2c32012-06-22 14:04:36 +01002995 return 0;
2996}
2997
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002998WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002999weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003000{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003001 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003002
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003003 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003004 if (ec->input_loop_source)
3005 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003006
Matt Roper361d2ad2011-08-29 13:52:23 -07003007 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003008 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003009 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003010
Daniel Stone325fc2d2012-05-30 16:31:58 +01003011 weston_binding_list_destroy_all(&ec->key_binding_list);
3012 weston_binding_list_destroy_all(&ec->button_binding_list);
3013 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003014 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003015
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003016 weston_plane_release(&ec->primary_plane);
3017
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003018 wl_array_release(&ec->vertices);
3019 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05003020 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003021
3022 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07003023}
3024
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003025static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003026{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003027 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003028
Martin Minarik6d118362012-06-07 18:01:59 +02003029 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003030 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003031
3032 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003033}
3034
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003035static void
3036on_segv_signal(int s, siginfo_t *siginfo, void *context)
3037{
3038 void *buffer[32];
3039 int i, count;
3040 Dl_info info;
3041
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003042 /* This SIGSEGV handler will do a best-effort backtrace, and
3043 * then call the backend restore function, which will switch
3044 * back to the vt we launched from or ungrab X etc and then
3045 * raise SIGTRAP. If we run weston under gdb from X or a
3046 * different vt, and tell gdb "handle SIGSEGV nostop", this
3047 * will allow weston to switch back to gdb on crash and then
3048 * gdb will catch the crash with SIGTRAP. */
3049
Martin Minarik6d118362012-06-07 18:01:59 +02003050 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003051
3052 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3053 for (i = 0; i < count; i++) {
3054 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02003055 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003056 (long) buffer[i],
3057 info.dli_sname ? info.dli_sname : "--",
3058 info.dli_fname);
3059 }
3060
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003061 segv_compositor->restore(segv_compositor);
3062
3063 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003064}
3065
3066
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003067static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003068load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003069{
3070 char path[PATH_MAX];
3071 void *module, *init;
3072
3073 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003074 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003075 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003076 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003077
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003078 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3079 if (module) {
3080 weston_log("Module '%s' already loaded\n", path);
3081 dlclose(module);
3082 return NULL;
3083 }
3084
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003085 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003086 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003087 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003088 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003089 return NULL;
3090 }
3091
3092 init = dlsym(module, entrypoint);
3093 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003094 weston_log("Failed to lookup init function: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003095 return NULL;
3096 }
3097
3098 return init;
3099}
3100
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003101static int
3102load_modules(struct weston_compositor *ec, const char *modules)
3103{
3104 const char *p, *end;
3105 char buffer[256];
3106 int (*module_init)(struct weston_compositor *ec);
3107
3108 if (modules == NULL)
3109 return 0;
3110
3111 p = modules;
3112 while (*p) {
3113 end = strchrnul(p, ',');
3114 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
3115 module_init = load_module(buffer, "module_init");
3116 if (module_init)
3117 module_init(ec);
3118 p = end;
3119 while (*p == ',')
3120 p++;
3121
3122 }
3123
3124 return 0;
3125}
3126
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003127static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003128 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3129
3130static const char xdg_wrong_message[] =
3131 "fatal: environment variable XDG_RUNTIME_DIR\n"
3132 "is set to \"%s\", which is not a directory.\n";
3133
3134static const char xdg_wrong_mode_message[] =
3135 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3136 "correctly. Unix access mode must be 0700 but is %o,\n"
3137 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003138 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003139
3140static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003141 "Refer to your distribution on how to get it, or\n"
3142 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3143 "on how to implement it.\n";
3144
Martin Minarik37032f82012-06-18 20:15:18 +02003145static void
3146verify_xdg_runtime_dir(void)
3147{
3148 char *dir = getenv("XDG_RUNTIME_DIR");
3149 struct stat s;
3150
3151 if (!dir) {
3152 weston_log(xdg_error_message);
3153 weston_log_continue(xdg_detail_message);
3154 exit(EXIT_FAILURE);
3155 }
3156
3157 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3158 weston_log(xdg_wrong_message, dir);
3159 weston_log_continue(xdg_detail_message);
3160 exit(EXIT_FAILURE);
3161 }
3162
3163 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3164 weston_log(xdg_wrong_mode_message,
3165 dir, s.st_mode & 0777, s.st_uid);
3166 weston_log_continue(xdg_detail_message);
3167 }
3168}
3169
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003170static int
3171usage(int error_code)
3172{
3173 fprintf(stderr,
3174 "Usage: weston [OPTIONS]\n\n"
3175 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3176 "Weston supports multiple backends, and depending on which backend is in use\n"
3177 "different options will be accepted.\n\n"
3178
3179
3180 "Core options:\n\n"
3181 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3182 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3183 " -S, --socket=NAME\tName of socket to listen on\n"
3184 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003185 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003186 " --log==FILE\t\tLog to the given file\n"
3187 " -h, --help\t\tThis help message\n\n");
3188
3189 fprintf(stderr,
3190 "Options for drm-backend.so:\n\n"
3191 " --connector=ID\tBring up only this connector\n"
3192 " --seat=SEAT\t\tThe seat that weston should run on\n"
3193 " --tty=TTY\t\tThe tty to use\n"
3194 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3195
3196 fprintf(stderr,
3197 "Options for x11-backend.so:\n\n"
3198 " --width=WIDTH\t\tWidth of X window\n"
3199 " --height=HEIGHT\tHeight of X window\n"
3200 " --fullscreen\t\tRun in fullscreen mode\n"
3201 " --output-count=COUNT\tCreate multiple outputs\n"
3202 " --no-input\t\tDont create input devices\n\n");
3203
3204 fprintf(stderr,
3205 "Options for wayland-backend.so:\n\n"
3206 " --width=WIDTH\t\tWidth of Wayland surface\n"
3207 " --height=HEIGHT\tHeight of Wayland surface\n"
3208 " --display=DISPLAY\tWayland display to connect to\n\n");
3209
3210 exit(error_code);
3211}
3212
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003213int main(int argc, char *argv[])
3214{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003215 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003216 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003217 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003218 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003219 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003220 struct sigaction segv_action;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003221 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003222 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003223 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003224 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003225 char *backend = NULL;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003226 const char *modules = "desktop-shell.so", *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003227 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003228 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003229 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003230 char *socket_name = "wayland-0";
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003231 char *config_file;
3232
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003233 const struct config_key core_config_keys[] = {
3234 { "modules", CONFIG_KEY_STRING, &modules },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003235 };
3236
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003237 const struct config_section cs[] = {
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003238 { "core",
3239 core_config_keys, ARRAY_LENGTH(core_config_keys) },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003240 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003241
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003242 const struct weston_option core_options[] = {
3243 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3244 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3245 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003246 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003247 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003248 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003249 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003250
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003251 argc = parse_options(core_options,
3252 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003253
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003254 if (help)
3255 usage(EXIT_SUCCESS);
3256
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003257 weston_log_file_open(log);
3258
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003259 weston_log("%s\n"
3260 STAMP_SPACE "%s\n"
3261 STAMP_SPACE "Bug reports to: %s\n"
3262 STAMP_SPACE "Build: %s\n",
3263 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003264 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003265 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003266
Martin Minarik37032f82012-06-18 20:15:18 +02003267 verify_xdg_runtime_dir();
3268
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003269 display = wl_display_create();
3270
Tiago Vignatti2116b892011-08-08 05:52:59 -07003271 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003272 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3273 display);
3274 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3275 display);
3276 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3277 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003278
3279 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003280 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3281 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003282
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003283 if (!backend) {
3284 if (getenv("WAYLAND_DISPLAY"))
3285 backend = "wayland-backend.so";
3286 else if (getenv("DISPLAY"))
3287 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003288 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003289 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003290 }
3291
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003292 config_file = config_file_path("weston.ini");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003293 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003294
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003295 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003296 if (!backend_init)
3297 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003298
Daniel Stonec1be8e52012-06-01 11:14:02 -04003299 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003300 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003301 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003302 exit(EXIT_FAILURE);
3303 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003304
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003305 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3306 segv_action.sa_sigaction = on_segv_signal;
3307 sigemptyset(&segv_action.sa_mask);
3308 sigaction(SIGSEGV, &segv_action, NULL);
3309 segv_compositor = ec;
3310
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003311 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03003312 weston_log("fatal: unhandled option: %s\n", argv[i]);
3313 if (argv[1]) {
3314 ret = EXIT_FAILURE;
3315 goto out;
3316 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003317
Daniel Stonec1be8e52012-06-01 11:14:02 -04003318 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01003319
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003320 ec->option_idle_time = idle_time;
3321 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003322
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003323 setenv("WAYLAND_DISPLAY", socket_name, 1);
3324
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003325 if (load_modules(ec, modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003326 goto out;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003327 if (load_modules(ec, option_modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003328 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003329
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003330 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003331 weston_log("fatal: failed to add socket: %m\n");
3332 ret = EXIT_FAILURE;
3333 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003334 }
3335
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003336 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003337 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003338
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003339 wl_display_run(display);
3340
3341 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003342 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05003343 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003344
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003345 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003346
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003347 for (i = ARRAY_LENGTH(signals); i;)
3348 wl_event_source_remove(signals[--i]);
3349
Daniel Stone855028f2012-05-01 20:37:10 +01003350 weston_compositor_xkb_destroy(ec);
3351
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003352 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003353 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003354
Martin Minarik19e6f262012-06-07 13:08:46 +02003355 weston_log_file_close();
3356
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003357 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003358}