blob: 110e5232126c2b7beffc304e6c28971ca314e34c [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
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05004 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04005 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050014 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040015 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050022 */
23
Kristian Høgsberg06bc2642010-12-01 09:50:16 -050024#define _GNU_SOURCE
25
Kristian Høgsberga9410222011-01-14 17:22:35 -050026#include "config.h"
27
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040028#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010032#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050033#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020034#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040035#include <sys/ioctl.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040036#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020037#include <sys/types.h>
38#include <sys/socket.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040039#include <fcntl.h>
40#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050041#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040042#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040043#include <dlfcn.h>
Kristian Høgsbergd34912c2011-05-02 10:36:04 -040044#include <getopt.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040045#include <signal.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050046
Pekka Paalanen50719bc2011-11-22 14:18:50 +020047#include <wayland-server.h>
Kristian Høgsberg82863022010-06-04 21:52:02 -040048#include "compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050049
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -050050static const char *option_socket_name = NULL;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050051
Kristian Høgsberg27da5382011-06-21 17:32:25 -040052static struct wl_list child_process_list;
53
54static int
55sigchld_handler(int signal_number, void *data)
56{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050057 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040058 int status;
59 pid_t pid;
60
61 pid = wait(&status);
62 wl_list_for_each(p, &child_process_list, link) {
63 if (p->pid == pid)
64 break;
65 }
66
67 if (&p->link == &child_process_list) {
68 fprintf(stderr, "unknown child process exited\n");
69 return 1;
70 }
71
72 wl_list_remove(&p->link);
73 p->cleanup(p, status);
74
75 return 1;
76}
77
78WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050079weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -040080{
81 wl_list_insert(&child_process_list, &process->link);
82}
83
Benjamin Franzke06286262011-05-06 19:12:33 +020084static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020085child_client_exec(int sockfd, const char *path)
86{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -050087 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020088 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +020089 sigset_t allsigs;
90
91 /* do not give our signal mask to the new process */
92 sigfillset(&allsigs);
93 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020094
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -050095 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
96 * non-CLOEXEC fd to pass through exec. */
97 clientfd = dup(sockfd);
98 if (clientfd == -1) {
99 fprintf(stderr, "compositor: dup failed: %m\n");
100 return;
101 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200102
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500103 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200104 setenv("WAYLAND_SOCKET", s, 1);
105
106 if (execl(path, path, NULL) < 0)
107 fprintf(stderr, "compositor: executing '%s' failed: %m\n",
108 path);
109}
110
111WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500112weston_client_launch(struct weston_compositor *compositor,
113 struct weston_process *proc,
114 const char *path,
115 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200116{
117 int sv[2];
118 pid_t pid;
119 struct wl_client *client;
120
121 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500122 fprintf(stderr, "weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200123 "socketpair failed while launching '%s': %m\n",
124 path);
125 return NULL;
126 }
127
128 pid = fork();
129 if (pid == -1) {
130 close(sv[0]);
131 close(sv[1]);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500132 fprintf(stderr, "weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200133 "fork failed while launching '%s': %m\n", path);
134 return NULL;
135 }
136
137 if (pid == 0) {
138 child_client_exec(sv[1], path);
139 exit(-1);
140 }
141
142 close(sv[1]);
143
144 client = wl_client_create(compositor->wl_display, sv[0]);
145 if (!client) {
146 close(sv[0]);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500147 fprintf(stderr, "weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200148 "wl_client_create failed while launching '%s'.\n",
149 path);
150 return NULL;
151 }
152
153 proc->pid = pid;
154 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500155 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200156
157 return client;
158}
159
160static void
Benjamin Franzke06286262011-05-06 19:12:33 +0200161surface_handle_buffer_destroy(struct wl_listener *listener,
162 struct wl_resource *resource, uint32_t time)
163{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500164 struct weston_surface *es =
165 container_of(listener, struct weston_surface,
166 buffer_destroy_listener);
Benjamin Franzke06286262011-05-06 19:12:33 +0200167
Kristian Høgsberg24596942011-10-24 17:51:02 -0400168 es->buffer = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200169}
170
171static void
172output_handle_scanout_buffer_destroy(struct wl_listener *listener,
173 struct wl_resource *resource,
174 uint32_t time)
175{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500176 struct weston_output *output =
177 container_of(listener, struct weston_output,
Benjamin Franzke06286262011-05-06 19:12:33 +0200178 scanout_buffer_destroy_listener);
Benjamin Franzke06286262011-05-06 19:12:33 +0200179
Kristian Høgsberg24596942011-10-24 17:51:02 -0400180 output->scanout_buffer = NULL;
Ander Conselvan de Oliveira9a38a0a2011-10-26 16:48:45 +0300181
182 if (!output->pending_scanout_buffer)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500183 weston_compositor_schedule_repaint(output->compositor);
Benjamin Franzke06286262011-05-06 19:12:33 +0200184}
185
Ander Conselvan de Oliveiraf1621d22011-10-18 17:02:07 +0300186static void
Kristian Høgsberg191454e2011-10-20 17:51:45 -0400187output_handle_pending_scanout_buffer_destroy(struct wl_listener *listener,
188 struct wl_resource *resource,
189 uint32_t time)
Ander Conselvan de Oliveiraf1621d22011-10-18 17:02:07 +0300190{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500191 struct weston_output *output =
192 container_of(listener, struct weston_output,
Kristian Høgsberg191454e2011-10-20 17:51:45 -0400193 pending_scanout_buffer_destroy_listener);
Ander Conselvan de Oliveiraf1621d22011-10-18 17:02:07 +0300194
Kristian Høgsberg24596942011-10-24 17:51:02 -0400195 output->pending_scanout_buffer = NULL;
Ander Conselvan de Oliveira9a38a0a2011-10-26 16:48:45 +0300196
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500197 weston_compositor_schedule_repaint(output->compositor);
Ander Conselvan de Oliveiraf1621d22011-10-18 17:02:07 +0300198}
199
200
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500201WL_EXPORT struct weston_surface *
202weston_surface_create(struct weston_compositor *compositor,
203 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500204{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500205 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400206
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200207 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400208 if (surface == NULL)
209 return NULL;
210
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500211 wl_list_init(&surface->link);
Benjamin Franzkebab41fb2011-03-07 18:04:59 +0100212 wl_list_init(&surface->buffer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500213
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500214 glGenTextures(1, &surface->texture);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400215 glBindTexture(GL_TEXTURE_2D, surface->texture);
216 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
217 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400218
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400219 surface->surface.resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400220
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500221 surface->compositor = compositor;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500222 surface->visual = WESTON_NONE_VISUAL;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200223 surface->image = EGL_NO_IMAGE_KHR;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200224 surface->saved_texture = 0;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400225 surface->x = x;
226 surface->y = y;
227 surface->width = width;
228 surface->height = height;
Kristian Høgsberg541e5552011-12-14 09:24:11 -0500229 surface->alpha = 255;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400230
Kristian Høgsbergcbd06f92011-10-12 16:29:31 -0400231 surface->fullscreen_output = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200232 surface->buffer = NULL;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400233 surface->output = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200234
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400235 pixman_region32_init(&surface->damage);
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400236 pixman_region32_init(&surface->opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500237 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400238
Benjamin Franzke06286262011-05-06 19:12:33 +0200239 surface->buffer_destroy_listener.func = surface_handle_buffer_destroy;
Benjamin Franzke06286262011-05-06 19:12:33 +0200240
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400241 surface->transform = NULL;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500242
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400243 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500244}
245
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400246WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500247weston_surface_damage_rectangle(struct weston_surface *surface,
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500248 int32_t x, int32_t y,
249 int32_t width, int32_t height)
250{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500251 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500252
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400253 pixman_region32_union_rect(&surface->damage,
254 &surface->damage,
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500255 surface->x + x, surface->y + y,
256 width, height);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500257 weston_compositor_schedule_repaint(compositor);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500258}
259
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400260WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500261weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500262{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500263 weston_surface_damage_rectangle(surface, 0, 0,
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500264 surface->width, surface->height);
265}
266
Kristian Høgsbergb8a98332011-06-23 21:00:04 -0400267WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500268weston_surface_damage_below(struct weston_surface *surface)
Kristian Høgsbergb8a98332011-06-23 21:00:04 -0400269{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500270 struct weston_surface *below;
Kristian Høgsbergb8a98332011-06-23 21:00:04 -0400271
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500272 if (surface->output == NULL)
Pekka Paalanend503a0b2011-11-15 13:34:53 +0200273 return;
274
Kristian Høgsbergb8a98332011-06-23 21:00:04 -0400275 if (surface->link.next == &surface->compositor->surface_list)
276 return;
277
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500278 below = container_of(surface->link.next, struct weston_surface, link);
Kristian Høgsbergb8a98332011-06-23 21:00:04 -0400279
280 pixman_region32_union_rect(&below->damage,
281 &below->damage,
282 surface->x, surface->y,
283 surface->width, surface->height);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500284 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsbergb8a98332011-06-23 21:00:04 -0400285}
286
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400287WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500288weston_surface_configure(struct weston_surface *surface,
289 int x, int y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400290{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500291 weston_surface_damage_below(surface);
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400292
293 surface->x = x;
294 surface->y = y;
295 surface->width = width;
296 surface->height = height;
297
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500298 weston_surface_assign_output(surface);
299 weston_surface_damage(surface);
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400300
301 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500302 if (surface->visual == WESTON_RGB_VISUAL)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400303 pixman_region32_init_rect(&surface->opaque,
304 surface->x, surface->y,
305 surface->width, surface->height);
306 else
307 pixman_region32_init(&surface->opaque);
308}
309
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400310WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500311weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500312{
313 struct timeval tv;
314
315 gettimeofday(&tv, NULL);
316
317 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
318}
319
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200320WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500321weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400322{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500323 struct weston_input_device *device;
324 struct weston_surface *surface;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400325 int32_t sx, sy;
326 uint32_t time;
327
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500328 if (!compositor->focus)
329 return;
330
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500331 time = weston_compositor_get_time();
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400332 wl_list_for_each(device, &compositor->input_device_list, link) {
Kristian Høgsberg32ff1f52011-10-12 00:36:54 -0400333 if (device->input_device.grab)
334 continue;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400335 surface = pick_surface(&device->input_device, &sx, &sy);
336 wl_input_device_set_pointer_focus(&device->input_device,
337 &surface->surface,
338 time,
339 device->input_device.x,
340 device->input_device.y,
341 sx, sy);
342 }
343}
344
345static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400346destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500347{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500348 struct weston_surface *surface =
349 container_of(resource,
350 struct weston_surface, surface.resource);
351 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400352
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500353 weston_surface_damage_below(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500354
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400355 wl_list_remove(&surface->link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500356 weston_compositor_repick(compositor);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400357
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200358 if (surface->saved_texture == 0)
359 glDeleteTextures(1, &surface->texture);
360 else
361 glDeleteTextures(1, &surface->saved_texture);
362
Benjamin Franzke06286262011-05-06 19:12:33 +0200363 if (surface->buffer)
364 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400365
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200366 if (surface->image != EGL_NO_IMAGE_KHR)
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400367 compositor->destroy_image(compositor->display,
368 surface->image);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200369
Benjamin Franzkebab41fb2011-03-07 18:04:59 +0100370 wl_list_remove(&surface->buffer_link);
371
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200372 pixman_region32_fini(&surface->damage);
373 pixman_region32_fini(&surface->opaque);
374
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400375 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500376}
377
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100378static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500379weston_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100380{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500381 struct weston_surface *es = (struct weston_surface *) surface;
382 struct weston_compositor *ec = es->compositor;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100383 struct wl_list *surfaces_attached_to;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100384
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100385 if (es->saved_texture != 0)
386 es->texture = es->saved_texture;
387
388 glBindTexture(GL_TEXTURE_2D, es->texture);
389
390 if (wl_buffer_is_shm(buffer)) {
391 /* Unbind any EGLImage texture that may be bound, so we don't
392 * overwrite it.*/
393 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
394 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200395 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100396 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200397 es->pitch, buffer->height, 0,
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100398 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
399 wl_shm_buffer_get_data(buffer));
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400400
401 switch (wl_shm_buffer_get_format(buffer)) {
402 case WL_SHM_FORMAT_ARGB32:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500403 es->visual = WESTON_ARGB_VISUAL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400404 break;
405 case WL_SHM_FORMAT_PREMULTIPLIED_ARGB32:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500406 es->visual = WESTON_PREMUL_ARGB_VISUAL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400407 break;
408 case WL_SHM_FORMAT_XRGB32:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500409 es->visual = WESTON_RGB_VISUAL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400410 break;
411 }
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100412
413 surfaces_attached_to = buffer->user_data;
414
Kristian Høgsbergfac11d22011-05-02 13:35:17 -0400415 wl_list_remove(&es->buffer_link);
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100416 wl_list_insert(surfaces_attached_to, &es->buffer_link);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100417 } else {
Benjamin Franzkefb4b5a22011-06-21 10:44:37 +0200418 if (es->image != EGL_NO_IMAGE_KHR)
419 ec->destroy_image(ec->display, es->image);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400420 es->image = ec->create_image(ec->display, NULL,
421 EGL_WAYLAND_BUFFER_WL,
422 buffer, NULL);
423
424 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400425
426 /* FIXME: we need to get the visual from the wl_buffer */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500427 es->visual = WESTON_PREMUL_ARGB_VISUAL;
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200428 es->pitch = es->width;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100429 }
430}
431
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400432static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500433texture_region(struct weston_surface *es, pixman_region32_t *region)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500434{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500435 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500436 GLfloat *v, inv_width, inv_height;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500437 pixman_box32_t *rectangles;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400438 unsigned int *p;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500439 int i, n;
440
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400441 rectangles = pixman_region32_rectangles(region, &n);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500442 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
443 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200444 inv_width = 1.0 / es->pitch;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500445 inv_height = 1.0 / es->height;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400446
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500447 for (i = 0; i < n; i++, v += 16, p += 6) {
448 v[ 0] = rectangles[i].x1;
449 v[ 1] = rectangles[i].y1;
450 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
451 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500452
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500453 v[ 4] = rectangles[i].x1;
454 v[ 5] = rectangles[i].y2;
455 v[ 6] = v[ 2];
456 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500457
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500458 v[ 8] = rectangles[i].x2;
459 v[ 9] = rectangles[i].y1;
460 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
461 v[11] = v[ 3];
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500462
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500463 v[12] = rectangles[i].x2;
464 v[13] = rectangles[i].y2;
465 v[14] = v[10];
466 v[15] = v[ 7];
467
468 p[0] = i * 4 + 0;
469 p[1] = i * 4 + 1;
470 p[2] = i * 4 + 2;
471 p[3] = i * 4 + 2;
472 p[4] = i * 4 + 1;
473 p[5] = i * 4 + 3;
474 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500475
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400476 return n;
477}
478
479static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500480transform_vertex(struct weston_surface *surface,
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400481 GLfloat x, GLfloat y, GLfloat u, GLfloat v, GLfloat *r)
482{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500483 struct weston_vector t;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400484
485 t.f[0] = x;
486 t.f[1] = y;
487 t.f[2] = 0.0;
488 t.f[3] = 1.0;
489
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500490 weston_matrix_transform(&surface->transform->matrix, &t);
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400491
492 r[ 0] = t.f[0];
493 r[ 1] = t.f[1];
494 r[ 2] = u;
495 r[ 3] = v;
496}
497
498static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500499texture_transformed_surface(struct weston_surface *es)
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400500{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500501 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400502 GLfloat *v;
503 unsigned int *p;
504
505 v = wl_array_add(&ec->vertices, 16 * sizeof *v);
506 p = wl_array_add(&ec->indices, 6 * sizeof *p);
507
508 transform_vertex(es, es->x, es->y, 0.0, 0.0, &v[0]);
509 transform_vertex(es, es->x, es->y + es->height, 0.0, 1.0, &v[4]);
510 transform_vertex(es, es->x + es->width, es->y, 1.0, 0.0, &v[8]);
511 transform_vertex(es, es->x + es->width, es->y + es->height,
512 1.0, 1.0, &v[12]);
513
514 p[0] = 0;
515 p[1] = 1;
516 p[2] = 2;
517 p[3] = 2;
518 p[4] = 1;
519 p[5] = 3;
520
521 return 1;
522}
523
524static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500525weston_surface_draw(struct weston_surface *es,
526 struct weston_output *output, pixman_region32_t *clip)
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400527{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500528 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400529 GLfloat *v;
530 pixman_region32_t repaint;
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400531 GLint filter;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400532 int n;
533
534 pixman_region32_init_rect(&repaint,
535 es->x, es->y, es->width, es->height);
536 pixman_region32_intersect(&repaint, &repaint, clip);
537 if (!pixman_region32_not_empty(&repaint))
538 return;
539
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400540 switch (es->visual) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500541 case WESTON_ARGB_VISUAL:
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400542 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
543 glEnable(GL_BLEND);
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400544 break;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500545 case WESTON_PREMUL_ARGB_VISUAL:
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400546 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
547 glEnable(GL_BLEND);
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400548 break;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500549 case WESTON_RGB_VISUAL:
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400550 glDisable(GL_BLEND);
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400551 break;
552 default:
553 fprintf(stderr, "bogus visual\n");
554 break;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400555 }
556
Kristian Høgsberg541e5552011-12-14 09:24:11 -0500557 if (es->alpha != ec->current_alpha) {
558 glUniform1f(ec->texture_shader.alpha_uniform,
559 es->alpha / 255.0);
560 ec->current_alpha = es->alpha;
561 }
562
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400563 if (es->transform == NULL) {
564 filter = GL_NEAREST;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400565 n = texture_region(es, &repaint);
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400566 } else {
567 filter = GL_LINEAR;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400568 n = texture_transformed_surface(es);
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400569 }
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400570
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500571 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400572 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
573 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
574
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500575 v = ec->vertices.data;
576 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
577 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400578 glEnableVertexAttribArray(0);
579 glEnableVertexAttribArray(1);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500580 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
581
582 ec->vertices.size = 0;
583 ec->indices.size = 0;
584 pixman_region32_fini(&repaint);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500585}
586
587static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500588weston_surface_raise(struct weston_surface *surface)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400589{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500590 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400591
592 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400593 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500594 weston_compositor_repick(compositor);
595 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400596}
597
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400598WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500599weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400600{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500601 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400602
603 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500604 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400605}
606
Benjamin Franzke06286262011-05-06 19:12:33 +0200607static inline void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500608weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +0200609{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400610 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200611 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200612
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400613 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -0500614 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200615}
616
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400617WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500618weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400619{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500620 struct weston_compositor *compositor = output->compositor;
621 struct weston_surface *es;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400622
Kristian Høgsbergd8264042011-11-11 11:48:12 -0500623 if (wl_list_empty(&compositor->surface_list))
624 return;
625
626 es = container_of(compositor->surface_list.next,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500627 struct weston_surface, link);
Kristian Høgsbergd8264042011-11-11 11:48:12 -0500628 pixman_region32_union(&es->damage, &es->damage, &output->region);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500629 weston_compositor_schedule_repaint(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400630}
631
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400632static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500633fade_frame(struct weston_animation *animation,
634 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400635{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500636 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400637 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500638 struct weston_compositor, fade.animation);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400639
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500640 weston_spring_update(&compositor->fade.spring, msecs);
641 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400642 compositor->fade.spring.current =
643 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400644 wl_list_remove(&animation->link);
645 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200646
647 if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500648 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200649 compositor->shell->lock(compositor->shell);
650 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400651 }
652
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500653 weston_output_damage(output);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400654}
655
656static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500657fade_output(struct weston_output *output,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400658 GLfloat tint, pixman_region32_t *region)
659{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500660 struct weston_compositor *compositor = output->compositor;
661 struct weston_surface surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400662 GLfloat color[4] = { 0.0, 0.0, 0.0, tint };
663
664 surface.compositor = compositor;
665 surface.x = output->x;
666 surface.y = output->y;
Tiago Vignatti2cc8b872011-08-19 15:10:40 +0300667 surface.pitch = output->current->width;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400668 surface.width = output->current->width;
669 surface.height = output->current->height;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400670 surface.texture = GL_NONE;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400671 surface.transform = NULL;
Pekka Paalanen3429a722011-12-20 16:38:25 +0200672 surface.alpha = compositor->current_alpha;
Kristian Høgsbergc352ab02011-04-23 15:34:50 -0400673
674 if (tint <= 1.0)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500675 surface.visual = WESTON_PREMUL_ARGB_VISUAL;
Kristian Høgsbergc352ab02011-04-23 15:34:50 -0400676 else
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500677 surface.visual = WESTON_RGB_VISUAL;
Kristian Høgsbergc352ab02011-04-23 15:34:50 -0400678
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400679 glUseProgram(compositor->solid_shader.program);
680 glUniformMatrix4fv(compositor->solid_shader.proj_uniform,
681 1, GL_FALSE, output->matrix.d);
682 glUniform4fv(compositor->solid_shader.color_uniform, 1, color);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500683 weston_surface_draw(&surface, output, region);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400684}
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200685
686static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500687weston_output_set_cursor(struct weston_output *output,
688 struct wl_input_device *dev, int force_sw)
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200689{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500690 struct weston_compositor *ec = output->compositor;
691 struct weston_input_device *device =
692 (struct weston_input_device *) dev;
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200693 pixman_region32_t cursor_region;
694 int use_hardware_cursor = 1, prior_was_hardware;
695
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -0500696 if (device->sprite == NULL)
697 return;
698
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200699 pixman_region32_init_rect(&cursor_region,
700 device->sprite->x, device->sprite->y,
701 device->sprite->width,
702 device->sprite->height);
703
704 pixman_region32_intersect(&cursor_region, &cursor_region, &output->region);
705
706 if (!pixman_region32_not_empty(&cursor_region)) {
Ander Conselvan de Oliveira02924bc2011-10-24 16:30:15 +0300707 output->set_hardware_cursor(output, NULL);
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200708 goto out;
709 }
710
711 prior_was_hardware = wl_list_empty(&device->sprite->link);
712 if (force_sw || output->set_hardware_cursor(output, device) < 0) {
Matt Roper11568a72011-08-29 15:59:37 -0700713 if (prior_was_hardware) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500714 weston_surface_damage(device->sprite);
Matt Roper11568a72011-08-29 15:59:37 -0700715 output->set_hardware_cursor(output, NULL);
716 }
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200717 use_hardware_cursor = 0;
718 } else if (!prior_was_hardware) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500719 weston_surface_damage_below(device->sprite);
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200720 }
721
722 /* Remove always to be on top. */
723 wl_list_remove(&device->sprite->link);
Kristian Høgsberg2584cb52011-11-22 19:47:57 -0500724 if (!use_hardware_cursor && ec->focus) {
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200725 wl_list_insert(&ec->surface_list, &device->sprite->link);
Kristian Høgsberg2584cb52011-11-22 19:47:57 -0500726 device->sprite->output = output;
727 } else {
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200728 wl_list_init(&device->sprite->link);
Kristian Høgsberg2584cb52011-11-22 19:47:57 -0500729 device->sprite->output = NULL;
730 }
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200731
732out:
733 pixman_region32_fini(&cursor_region);
734}
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400735
Ander Conselvan de Oliveira3b199662011-10-18 17:02:06 +0300736static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500737setup_scanout_surface(struct weston_output *output, struct weston_surface *es)
Ander Conselvan de Oliveira3b199662011-10-18 17:02:06 +0300738{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500739 if (es->visual != WESTON_RGB_VISUAL ||
Ander Conselvan de Oliveira3b199662011-10-18 17:02:06 +0300740 output->prepare_scanout_surface(output, es) != 0)
Kristian Høgsberg191454e2011-10-20 17:51:45 -0400741 return -1;
Ander Conselvan de Oliveira3b199662011-10-18 17:02:06 +0300742
Kristian Høgsberg191454e2011-10-20 17:51:45 -0400743 /* assert output->pending_scanout_buffer == NULL */
744 output->pending_scanout_buffer = es->buffer;
745 output->pending_scanout_buffer->busy_count++;
Ander Conselvan de Oliveira3b199662011-10-18 17:02:06 +0300746
Kristian Høgsberg191454e2011-10-20 17:51:45 -0400747 wl_list_insert(output->pending_scanout_buffer->resource.destroy_listener_list.prev,
748 &output->pending_scanout_buffer_destroy_listener.link);
Ander Conselvan de Oliveiraf1621d22011-10-18 17:02:07 +0300749
Kristian Høgsberg191454e2011-10-20 17:51:45 -0400750 return 0;
Ander Conselvan de Oliveira3b199662011-10-18 17:02:06 +0300751}
752
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400753static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500754weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400755{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500756 struct weston_compositor *ec = output->compositor;
757 struct weston_surface *es;
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400758 pixman_region32_t opaque, new_damage, total_damage, repaint;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500759
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100760 output->prepare_render(output);
761
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400762 glViewport(0, 0, output->current->width, output->current->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500763
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -0400764 glUseProgram(ec->texture_shader.program);
765 glUniformMatrix4fv(ec->texture_shader.proj_uniform,
766 1, GL_FALSE, output->matrix.d);
767 glUniform1i(ec->texture_shader.tex_uniform, 0);
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500768
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500769 weston_output_set_cursor(output, ec->input_device,
Kristian Høgsberg4ebf3a02011-08-29 16:47:09 -0400770 ec->fade.spring.current >= 0.001);
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200771
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500772 pixman_region32_init(&new_damage);
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400773 pixman_region32_init(&opaque);
Pekka Paalanencc9102e2012-01-02 15:42:44 +0200774
Kristian Høgsberg8b72f602011-06-23 20:46:34 -0400775 wl_list_for_each(es, &ec->surface_list, link) {
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400776 pixman_region32_subtract(&es->damage, &es->damage, &opaque);
Kristian Høgsberg8b72f602011-06-23 20:46:34 -0400777 pixman_region32_union(&new_damage, &new_damage, &es->damage);
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400778 pixman_region32_union(&opaque, &opaque, &es->opaque);
Kristian Høgsberg8b72f602011-06-23 20:46:34 -0400779 }
780
Kristian Høgsberg8b72f602011-06-23 20:46:34 -0400781 pixman_region32_init(&total_damage);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500782 pixman_region32_union(&total_damage, &new_damage,
Kristian Høgsberg8b72f602011-06-23 20:46:34 -0400783 &output->previous_damage);
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400784 pixman_region32_intersect(&output->previous_damage,
785 &new_damage, &output->region);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500786
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400787 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -0400788 pixman_region32_fini(&new_damage);
789
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500790 es = container_of(ec->surface_list.next, struct weston_surface, link);
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200791
Kristian Høgsbergd8264042011-11-11 11:48:12 -0500792 if (setup_scanout_surface(output, es) == 0)
793 /* We're drawing nothing, just let the damage accumulate */
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400794 return;
Kristian Høgsberg4d07a1c2011-05-06 14:03:12 -0400795
796 if (es->fullscreen_output == output) {
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400797 if (es->width < output->current->width ||
798 es->height < output->current->height)
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200799 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500800 weston_surface_draw(es, output, &total_damage);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500801 } else {
Kristian Høgsbergaee7f842011-06-23 21:25:20 -0400802 wl_list_for_each(es, &ec->surface_list, link) {
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400803 pixman_region32_copy(&es->damage, &total_damage);
804 pixman_region32_subtract(&total_damage, &total_damage, &es->opaque);
Kristian Høgsbergaee7f842011-06-23 21:25:20 -0400805 }
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500806
Kristian Høgsbergaee7f842011-06-23 21:25:20 -0400807 wl_list_for_each_reverse(es, &ec->surface_list, link) {
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400808 pixman_region32_init(&repaint);
809 pixman_region32_intersect(&repaint, &output->region,
810 &es->damage);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500811 weston_surface_draw(es, output, &repaint);
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400812 pixman_region32_subtract(&es->damage,
813 &es->damage, &output->region);
Pekka Paalanencc9102e2012-01-02 15:42:44 +0200814 pixman_region32_fini(&repaint);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500815 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500816 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400817
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400818 if (ec->fade.spring.current > 0.001)
819 fade_output(output, ec->fade.spring.current, &total_damage);
Pekka Paalanencc9102e2012-01-02 15:42:44 +0200820
821 pixman_region32_fini(&total_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500822}
823
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500824struct weston_frame_callback {
Kristian Høgsberg33418202011-08-16 23:01:28 -0400825 struct wl_resource resource;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400826 struct wl_list link;
827};
828
Kristian Høgsbergef044142011-06-21 15:02:12 -0400829static void
830repaint(void *data, int msecs)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500831{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500832 struct weston_output *output = data;
833 struct weston_compositor *compositor = output->compositor;
834 struct weston_animation *animation, *next;
835 struct weston_frame_callback *cb, *cnext;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500836
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500837 weston_output_repaint(output);
Kristian Høgsbergef044142011-06-21 15:02:12 -0400838 output->repaint_needed = 0;
839 output->repaint_scheduled = 1;
840 output->present(output);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100841
Kristian Høgsberg33418202011-08-16 23:01:28 -0400842 wl_list_for_each_safe(cb, cnext, &output->frame_callback_list, link) {
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400843 wl_resource_post_event(&cb->resource, WL_CALLBACK_DONE, msecs);
844 wl_resource_destroy(&cb->resource, 0);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500845 }
846
Kristian Høgsbergef044142011-06-21 15:02:12 -0400847 wl_list_for_each_safe(animation, next,
848 &compositor->animation_list, link)
849 animation->frame(animation, output, msecs);
850}
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400851
Kristian Høgsbergef044142011-06-21 15:02:12 -0400852static void
853idle_repaint(void *data)
854{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500855 repaint(data, weston_compositor_get_time());
Kristian Høgsbergef044142011-06-21 15:02:12 -0400856}
857
858WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500859weston_output_finish_frame(struct weston_output *output, int msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -0400860{
Kristian Høgsberg191454e2011-10-20 17:51:45 -0400861 if (output->scanout_buffer) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500862 weston_buffer_post_release(output->scanout_buffer);
Kristian Høgsberg191454e2011-10-20 17:51:45 -0400863 wl_list_remove(&output->scanout_buffer_destroy_listener.link);
864 output->scanout_buffer = NULL;
865 }
866
867 if (output->pending_scanout_buffer) {
868 output->scanout_buffer = output->pending_scanout_buffer;
869 wl_list_remove(&output->pending_scanout_buffer_destroy_listener.link);
870 wl_list_insert(output->scanout_buffer->resource.destroy_listener_list.prev,
871 &output->scanout_buffer_destroy_listener.link);
872 output->pending_scanout_buffer = NULL;
873 }
874
Kristian Høgsbergef044142011-06-21 15:02:12 -0400875 if (output->repaint_needed)
876 repaint(output, msecs);
Ander Conselvan de Oliveira22e22a52011-10-24 16:30:14 +0300877 else
878 output->repaint_scheduled = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400879}
880
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400881WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500882weston_compositor_schedule_repaint(struct weston_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400883{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500884 struct weston_output *output;
Kristian Høgsbergef044142011-06-21 15:02:12 -0400885 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100886
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500887 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400888 return;
889
Kristian Høgsbergef044142011-06-21 15:02:12 -0400890 loop = wl_display_get_event_loop(compositor->wl_display);
891 wl_list_for_each(output, &compositor->output_list, link) {
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100892 output->repaint_needed = 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -0400893 if (output->repaint_scheduled)
894 continue;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100895
Kristian Høgsbergef044142011-06-21 15:02:12 -0400896 wl_event_loop_add_idle(loop, idle_repaint, output);
897 output->repaint_scheduled = 1;
898 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400899}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500900
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400901WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500902weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400903{
904 int done;
905
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500906 done = weston_spring_done(&compositor->fade.spring);
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400907 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500908 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400909 return;
910
911 if (done)
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400912 compositor->fade.spring.timestamp =
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500913 weston_compositor_get_time();
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400914
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500915 weston_compositor_damage_all(compositor);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400916 if (wl_list_empty(&compositor->fade.animation.link))
917 wl_list_insert(compositor->animation_list.prev,
918 &compositor->fade.animation.link);
919}
920
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500921static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400922surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400923{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500924 wl_resource_destroy(resource, weston_compositor_get_time());
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400925}
926
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400927WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500928weston_surface_assign_output(struct weston_surface *es)
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100929{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500930 struct weston_compositor *ec = es->compositor;
931 struct weston_output *output, *new_output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -0700932 pixman_region32_t region;
933 uint32_t max, area;
934 pixman_box32_t *e;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100935
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500936 new_output = NULL;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -0700937 max = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100938 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg4f0df042011-07-21 11:30:22 -0700939 pixman_region32_init_rect(&region,
940 es->x, es->y, es->width, es->height);
941 pixman_region32_intersect(&region, &region, &output->region);
942
943 e = pixman_region32_extents(&region);
944 area = (e->x2 - e->x1) * (e->y2 - e->y1);
945
946 if (area >= max) {
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500947 new_output = output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -0700948 max = area;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100949 }
950 }
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500951
952 es->output = new_output;
953 if (!wl_list_empty(&es->frame_callback_list)) {
954 wl_list_insert_list(new_output->frame_callback_list.prev,
955 &es->frame_callback_list);
956 wl_list_init(&es->frame_callback_list);
957 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100958}
959
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500960static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500961surface_attach(struct wl_client *client,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400962 struct wl_resource *resource,
963 struct wl_resource *buffer_resource, int32_t x, int32_t y)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400964{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500965 struct weston_surface *es = resource->data;
966 struct weston_shell *shell = es->compositor->shell;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400967 struct wl_buffer *buffer = buffer_resource->data;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400968
Kristian Høgsberg24596942011-10-24 17:51:02 -0400969 if (es->buffer) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500970 weston_buffer_post_release(es->buffer);
Kristian Høgsberg24596942011-10-24 17:51:02 -0400971 wl_list_remove(&es->buffer_destroy_listener.link);
972 }
Benjamin Franzke06286262011-05-06 19:12:33 +0200973
Kristian Høgsberg24596942011-10-24 17:51:02 -0400974 buffer->busy_count++;
Benjamin Franzke06286262011-05-06 19:12:33 +0200975 es->buffer = buffer;
Benjamin Franzke06286262011-05-06 19:12:33 +0200976 wl_list_insert(es->buffer->resource.destroy_listener_list.prev,
977 &es->buffer_destroy_listener.link);
978
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500979 if (es->visual == WESTON_NONE_VISUAL) {
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500980 shell->map(shell, es, buffer->width, buffer->height);
981 } else if (x != 0 || y != 0 ||
982 es->width != buffer->width ||
983 es->height != buffer->height) {
984 shell->configure(shell, es, es->x + x, es->y + y,
985 buffer->width, buffer->height);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400986 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400987
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500988 weston_buffer_attach(buffer, &es->surface);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400989}
990
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500991static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500992surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400993 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500994 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500995{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500996 struct weston_surface *es = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400997
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500998 weston_surface_damage_rectangle(es, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500999}
1000
Kristian Høgsberg33418202011-08-16 23:01:28 -04001001static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001002destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001003{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001004 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001005
1006 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001007 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001008}
1009
1010static void
1011surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001012 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001013{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001014 struct weston_frame_callback *cb;
1015 struct weston_surface *es = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001016
1017 cb = malloc(sizeof *cb);
1018 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001019 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001020 return;
1021 }
1022
1023 cb->resource.object.interface = &wl_callback_interface;
1024 cb->resource.object.id = callback;
1025 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001026 cb->resource.client = client;
1027 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001028
1029 wl_client_add_resource(client, &cb->resource);
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -04001030
1031 if (es->output) {
1032 wl_list_insert(es->output->frame_callback_list.prev,
1033 &cb->link);
1034 } else {
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001035 wl_list_insert(es->frame_callback_list.prev, &cb->link);
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -04001036 }
Kristian Høgsberg33418202011-08-16 23:01:28 -04001037}
1038
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001039const static struct wl_surface_interface surface_interface = {
1040 surface_destroy,
1041 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001042 surface_damage,
1043 surface_frame
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001044};
1045
1046static void
1047compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001048 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001049{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001050 struct weston_compositor *ec = resource->data;
1051 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001052
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001053 surface = weston_surface_create(ec, 0, 0, 0, 0);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001054 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001055 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001056 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001057 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001058
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001059 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001060
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001061 surface->surface.resource.object.id = id;
1062 surface->surface.resource.object.interface = &wl_surface_interface;
1063 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001064 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001065 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001066
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001067 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001068}
1069
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001070const static struct wl_compositor_interface compositor_interface = {
1071 compositor_create_surface,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001072};
1073
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001074static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001075weston_surface_transform(struct weston_surface *surface,
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001076 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1077{
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001078 *sx = x - surface->x;
1079 *sy = y - surface->y;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001080}
1081
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001082static struct weston_surface *
1083weston_compositor_pick_surface(struct weston_compositor *compositor,
1084 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
Kristian Høgsberg773d13b2011-12-21 15:15:08 -05001085{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001086 struct weston_surface *surface;
Kristian Høgsberg773d13b2011-12-21 15:15:08 -05001087
1088 wl_list_for_each(surface, &compositor->surface_list, link) {
1089 if (surface->surface.resource.client == NULL)
1090 continue;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001091 weston_surface_transform(surface, x, y, sx, sy);
Kristian Høgsberg773d13b2011-12-21 15:15:08 -05001092 if (0 <= *sx && *sx < surface->width &&
1093 0 <= *sy && *sy < surface->height)
1094 return surface;
1095 }
1096
1097 return NULL;
1098}
1099
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001100WL_EXPORT struct weston_surface *
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001101pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001102{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001103 struct weston_input_device *wd = (struct weston_input_device *) device;
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001104
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001105 return weston_compositor_pick_surface(wd->compositor,
Kristian Høgsberg773d13b2011-12-21 15:15:08 -05001106 device->x, device->y, sx, sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001107}
1108
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001109
1110static void
Kristian Høgsberg9c4eecb2011-09-06 18:13:14 -04001111implicit_grab_motion(struct wl_grab *grab,
1112 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001113{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001114 struct weston_input_device *device =
1115 (struct weston_input_device *) grab->input_device;
1116 struct weston_surface *es =
1117 (struct weston_surface *) device->input_device.pointer_focus;
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001118 int32_t sx, sy;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001119 struct wl_resource *resource;
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001120
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001121 resource = grab->input_device->pointer_focus_resource;
1122 if (resource) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001123 weston_surface_transform(es, x, y, &sx, &sy);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001124 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
1125 time, x, y, sx, sy);
1126 }
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001127}
1128
1129static void
Kristian Høgsberg9c4eecb2011-09-06 18:13:14 -04001130implicit_grab_button(struct wl_grab *grab,
1131 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001132{
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001133 struct wl_resource *resource;
1134
1135 resource = grab->input_device->pointer_focus_resource;
1136 if (resource)
1137 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
1138 time, button, state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001139}
1140
1141static void
Kristian Høgsberg9c4eecb2011-09-06 18:13:14 -04001142implicit_grab_end(struct wl_grab *grab, uint32_t time)
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001143{
1144}
1145
Kristian Høgsberg9c4eecb2011-09-06 18:13:14 -04001146static const struct wl_grab_interface implicit_grab_interface = {
1147 implicit_grab_motion,
1148 implicit_grab_button,
1149 implicit_grab_end
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001150};
1151
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001152WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001153weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001154{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001155 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1156 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001157
1158 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001159 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001160}
1161
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001162WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001163weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001164{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001165 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1166 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001167 } else {
1168 compositor->shell->unlock(compositor->shell);
1169 }
1170}
1171
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001172static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001173weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001174{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001175 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001176 compositor->idle_inhibit++;
1177}
1178
1179static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001180weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001181{
1182 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001183 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001184}
1185
1186static int
1187idle_handler(void *data)
1188{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001189 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001190
1191 if (compositor->idle_inhibit)
1192 return 1;
1193
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001194 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001195
1196 return 1;
1197}
1198
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001199WL_EXPORT void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001200notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001201{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001202 struct weston_surface *es;
1203 struct weston_output *output;
Kristian Høgsberg6d65d5f2010-12-07 13:30:18 -05001204 const struct wl_grab_interface *interface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001205 struct weston_input_device *wd = (struct weston_input_device *) device;
1206 struct weston_compositor *ec = wd->compositor;
Kristian Høgsbergfc9c28a2010-12-07 13:04:43 -05001207 int32_t sx, sy;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001208 int x_valid = 0, y_valid = 0;
1209 int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001210
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001211 weston_compositor_activity(ec);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001212
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001213 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001214 if (output->x <= x && x <= output->x + output->current->width)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001215 x_valid = 1;
1216
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001217 if (output->y <= y && y <= output->y + output->current->height)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001218 y_valid = 1;
1219
1220 /* FIXME: calculate this only on output addition/deletion */
1221 if (output->x < min_x)
1222 min_x = output->x;
1223 if (output->y < min_y)
1224 min_y = output->y;
1225
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001226 if (output->x + output->current->width > max_x)
1227 max_x = output->x + output->current->width;
1228 if (output->y + output->current->height > max_y)
1229 max_y = output->y + output->current->height;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001230 }
1231
1232 if (!x_valid) {
1233 if (x < min_x)
1234 x = min_x;
1235 else if (x >= max_x)
1236 x = max_x;
1237 }
1238 if (!y_valid) {
1239 if (y < min_y)
1240 y = min_y;
1241 else if (y >= max_y)
1242 y = max_y;
1243 }
Ray Strode90e701d2008-12-18 23:05:43 -05001244
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -05001245 device->x = x;
1246 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001247
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001248 if (device->grab) {
1249 interface = device->grab->interface;
1250 interface->motion(device->grab, time, x, y);
1251 } else {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001252 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001253 wl_input_device_set_pointer_focus(device,
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001254 &es->surface,
Kristian Høgsberg26437072010-12-01 10:17:47 -05001255 time, x, y, sx, sy);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001256 if (device->pointer_focus_resource)
1257 wl_resource_post_event(device->pointer_focus_resource,
1258 WL_INPUT_DEVICE_MOTION,
1259 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001260 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001261
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001262 if (wd->sprite) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001263 weston_surface_damage_below(wd->sprite);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001264
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001265 wd->sprite->x = device->x - wd->hotspot_x;
1266 wd->sprite->y = device->y - wd->hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001267
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001268 weston_surface_damage(wd->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001269 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001270}
1271
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001272WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001273weston_surface_activate(struct weston_surface *surface,
1274 struct weston_input_device *device, uint32_t time)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001275{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001276 weston_surface_raise(surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001277 wl_input_device_set_keyboard_focus(&device->input_device,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001278 &surface->surface, time);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001279 weston_data_device_set_keyboard_focus(device);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001280}
1281
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001282WL_EXPORT void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001283notify_button(struct wl_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001284 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001285{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001286 struct weston_input_device *wd = (struct weston_input_device *) device;
1287 struct weston_compositor *compositor = wd->compositor;
1288 struct weston_surface *surface =
1289 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg4eaa8302011-09-06 13:41:47 -04001290 int32_t sx, sy;
Kristian Høgsbergea081152010-12-07 08:59:51 -05001291
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001292 if (state)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001293 weston_compositor_idle_inhibit(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001294 else
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001295 weston_compositor_idle_release(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001296
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001297 weston_compositor_run_binding(compositor, wd, time, 0, button, state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001298
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001299 if (state && surface && device->grab == NULL) {
1300 wl_input_device_start_grab(device,
1301 &device->implicit_grab,
1302 button, time);
1303 }
1304
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -05001305 if (device->grab)
1306 device->grab->interface->button(device->grab, time,
1307 button, state);
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -05001308
Kristian Høgsberg4eaa8302011-09-06 13:41:47 -04001309 if (!state && device->grab && device->grab_button == button) {
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001310 wl_input_device_end_grab(device, time);
Kristian Høgsberg4eaa8302011-09-06 13:41:47 -04001311 surface = pick_surface(device, &sx, &sy);
1312 wl_input_device_set_pointer_focus(device, &surface->surface,
1313 time, device->x, device->y,
1314 sx, sy);
1315 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001316}
1317
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001318static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001319update_modifier_state(struct weston_input_device *device,
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001320 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001321{
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001322 uint32_t modifier;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001323
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001324 switch (key) {
1325 case KEY_LEFTCTRL:
1326 case KEY_RIGHTCTRL:
1327 modifier = MODIFIER_CTRL;
1328 break;
1329
1330 case KEY_LEFTALT:
1331 case KEY_RIGHTALT:
1332 modifier = MODIFIER_ALT;
1333 break;
1334
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -04001335 case KEY_LEFTMETA:
1336 case KEY_RIGHTMETA:
1337 modifier = MODIFIER_SUPER;
1338 break;
1339
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001340 default:
1341 modifier = 0;
1342 break;
1343 }
1344
1345 if (state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001346 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001347 else
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001348 device->modifier_state &= ~modifier;
1349}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001350
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001351WL_EXPORT void
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001352notify_key(struct wl_input_device *device,
1353 uint32_t time, uint32_t key, uint32_t state)
1354{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001355 struct weston_input_device *wd = (struct weston_input_device *) device;
1356 struct weston_compositor *compositor = wd->compositor;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001357 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001358
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001359 if (state)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001360 weston_compositor_idle_inhibit(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001361 else
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001362 weston_compositor_idle_release(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001363
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001364 weston_compositor_run_binding(compositor, wd, time, key, 0, state);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001365
1366 update_modifier_state(wd, key, state);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001367 end = device->keys.data + device->keys.size;
1368 for (k = device->keys.data; k < end; k++) {
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001369 if (*k == key)
1370 *k = *--end;
1371 }
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001372 device->keys.size = (void *) end - device->keys.data;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001373 if (state) {
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001374 k = wl_array_add(&device->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001375 *k = key;
1376 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001377
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001378 if (device->keyboard_focus_resource)
1379 wl_resource_post_event(device->keyboard_focus_resource,
1380 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001381}
1382
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001383WL_EXPORT void
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001384notify_pointer_focus(struct wl_input_device *device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001385 uint32_t time, struct weston_output *output,
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001386 int32_t x, int32_t y)
1387{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001388 struct weston_input_device *wd = (struct weston_input_device *) device;
1389 struct weston_compositor *compositor = wd->compositor;
1390 struct weston_surface *es;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001391 int32_t sx, sy;
1392
1393 if (output) {
1394 device->x = x;
1395 device->y = y;
1396 es = pick_surface(device, &sx, &sy);
1397 wl_input_device_set_pointer_focus(device,
1398 &es->surface,
1399 time, x, y, sx, sy);
1400
1401 compositor->focus = 1;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001402 } else {
1403 wl_input_device_set_pointer_focus(device, NULL,
1404 time, 0, 0, 0, 0);
1405 compositor->focus = 0;
1406 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001407}
1408
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001409WL_EXPORT void
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001410notify_keyboard_focus(struct wl_input_device *device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001411 uint32_t time, struct weston_output *output,
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001412 struct wl_array *keys)
1413{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001414 struct weston_input_device *wd =
1415 (struct weston_input_device *) device;
1416 struct weston_compositor *compositor = wd->compositor;
1417 struct weston_surface *es;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001418 uint32_t *k, *end;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001419
1420 if (!wl_list_empty(&compositor->surface_list))
1421 es = container_of(compositor->surface_list.next,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001422 struct weston_surface, link);
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001423 else
1424 es = NULL;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001425
1426 if (output) {
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001427 wl_array_copy(&wd->input_device.keys, keys);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001428 wd->modifier_state = 0;
1429 end = device->keys.data + device->keys.size;
1430 for (k = device->keys.data; k < end; k++) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001431 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001432 update_modifier_state(wd, *k, 1);
1433 }
1434
Kristian Høgsbergf59da392011-09-06 13:44:56 -04001435 if (es && es->surface.resource.client)
Benjamin Franzkeb7c00a42011-06-23 23:30:30 +02001436 wl_input_device_set_keyboard_focus(&wd->input_device,
1437 &es->surface, time);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001438 } else {
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001439 end = device->keys.data + device->keys.size;
1440 for (k = device->keys.data; k < end; k++)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001441 weston_compositor_idle_release(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001442
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001443 wd->modifier_state = 0;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001444 wl_input_device_set_keyboard_focus(&wd->input_device,
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001445 NULL, time);
1446 }
1447}
1448
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001449/* TODO: share this function with wayland-server.c */
1450static struct wl_resource *
1451find_resource_for_surface(struct wl_list *list, struct wl_surface *surface)
1452{
1453 struct wl_resource *r;
1454
1455 if (!surface)
1456 return NULL;
1457
1458 wl_list_for_each(r, list, link) {
1459 if (r->client == surface->resource.client)
1460 return r;
1461 }
1462
1463 return NULL;
1464}
1465
1466static void
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001467lose_touch_focus_resource(struct wl_listener *listener,
1468 struct wl_resource *resource, uint32_t time)
1469{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001470 struct weston_input_device *device =
1471 container_of(listener, struct weston_input_device,
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001472 touch_focus_resource_listener);
1473
1474 device->touch_focus_resource = NULL;
1475}
1476
1477static void
1478lose_touch_focus(struct wl_listener *listener,
1479 struct wl_resource *resource, uint32_t time)
1480{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001481 struct weston_input_device *device =
1482 container_of(listener, struct weston_input_device,
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001483 touch_focus_listener);
1484
1485 device->touch_focus = NULL;
1486}
1487
1488static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001489touch_set_focus(struct weston_input_device *device,
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001490 struct wl_surface *surface, uint32_t time)
1491{
1492 struct wl_input_device *input_device = &device->input_device;
1493 struct wl_resource *resource;
1494
1495 if (device->touch_focus == surface)
1496 return;
1497
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001498 if (surface) {
1499 resource =
1500 find_resource_for_surface(&input_device->resource_list,
1501 surface);
1502 if (!resource) {
1503 fprintf(stderr, "couldn't find resource\n");
1504 return;
1505 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001506
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001507 device->touch_focus_resource_listener.func =
1508 lose_touch_focus_resource;
1509 wl_list_insert(resource->destroy_listener_list.prev,
1510 &device->touch_focus_resource_listener.link);
1511 device->touch_focus_listener.func = lose_touch_focus;
1512 wl_list_insert(surface->resource.destroy_listener_list.prev,
1513 &device->touch_focus_listener.link);
1514
1515 device->touch_focus = surface;
1516 device->touch_focus_resource = resource;
1517 } else {
1518 if (device->touch_focus)
1519 wl_list_remove(&device->touch_focus_listener.link);
1520 if (device->touch_focus_resource)
1521 wl_list_remove(&device->touch_focus_resource_listener.link);
1522 device->touch_focus = NULL;
1523 device->touch_focus_resource = NULL;
1524 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001525}
1526
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001527/**
1528 * notify_touch - emulates button touches and notifies surfaces accordingly.
1529 *
1530 * It assumes always the correct cycle sequence until it gets here: touch_down
1531 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1532 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001533 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001534 */
1535WL_EXPORT void
1536notify_touch(struct wl_input_device *device, uint32_t time, int touch_id,
1537 int x, int y, int touch_type)
1538{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001539 struct weston_input_device *wd = (struct weston_input_device *) device;
1540 struct weston_compositor *ec = wd->compositor;
1541 struct weston_surface *es;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001542 int32_t sx, sy;
1543
1544 switch (touch_type) {
1545 case WL_INPUT_DEVICE_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001546 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001547
1548 wd->num_tp++;
1549
1550 /* the first finger down picks the surface, and all further go
1551 * to that surface for the remainder of the touch session i.e.
1552 * until all touch points are up again. */
1553 if (wd->num_tp == 1) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001554 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001555 touch_set_focus(wd, &es->surface, time);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001556 } else if (wd->touch_focus) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001557 es = (struct weston_surface *) wd->touch_focus;
1558 weston_surface_transform(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001559 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001560
1561 if (wd->touch_focus_resource && wd->touch_focus)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001562 wl_resource_post_event(wd->touch_focus_resource,
1563 touch_type, time,
1564 wd->touch_focus,
1565 touch_id, sx, sy);
1566 break;
1567 case WL_INPUT_DEVICE_TOUCH_MOTION:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001568 es = (struct weston_surface *) wd->touch_focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001569 if (!es)
1570 break;
1571
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001572 weston_surface_transform(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001573 if (wd->touch_focus_resource)
1574 wl_resource_post_event(wd->touch_focus_resource,
Kristian Høgsberg3543ff42011-12-21 17:25:54 -05001575 touch_type, time,
1576 touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001577 break;
1578 case WL_INPUT_DEVICE_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001579 weston_compositor_idle_release(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001580 wd->num_tp--;
1581
Kristian Høgsberg3543ff42011-12-21 17:25:54 -05001582 if (wd->touch_focus_resource)
1583 wl_resource_post_event(wd->touch_focus_resource,
1584 touch_type, time, touch_id);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001585 if (wd->num_tp == 0)
1586 touch_set_focus(wd, NULL, time);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001587 break;
1588 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02001589}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001590
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001591static void
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001592input_device_attach(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001593 struct wl_resource *resource,
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001594 uint32_t time,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001595 struct wl_resource *buffer_resource, int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001596{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001597 struct weston_input_device *device = resource->data;
1598 struct weston_compositor *compositor = device->compositor;
Kristian Høgsberg57295eb2011-08-29 16:02:57 -04001599 struct wl_buffer *buffer;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001600
1601 if (time < device->input_device.pointer_focus_time)
1602 return;
1603 if (device->input_device.pointer_focus == NULL)
1604 return;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001605 if (device->input_device.pointer_focus->resource.client != client)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001606 return;
1607
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001608 if (device->sprite)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001609 weston_surface_damage_below(device->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001610
1611 if (!buffer_resource) {
1612 destroy_surface(&device->sprite->surface.resource);
1613 device->sprite = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001614 return;
1615 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001616
1617 if (!device->sprite) {
1618 device->sprite =
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001619 weston_surface_create(compositor,
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001620 device->input_device.x,
1621 device->input_device.y, 32, 32);
1622 wl_list_init(&device->sprite->link);
1623 }
1624
1625 buffer = buffer_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001626 weston_buffer_attach(buffer, &device->sprite->surface);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001627
1628 device->hotspot_x = x;
1629 device->hotspot_y = y;
1630 device->sprite->width = buffer->width;
1631 device->sprite->height = buffer->height;
1632 device->sprite->x = device->input_device.x - device->hotspot_x;
1633 device->sprite->y = device->input_device.y - device->hotspot_y;
1634
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001635 weston_surface_damage(device->sprite);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001636}
1637
1638const static struct wl_input_device_interface input_device_interface = {
1639 input_device_attach,
1640};
1641
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001642static void unbind_input_device(struct wl_resource *resource)
1643{
1644 wl_list_remove(&resource->link);
1645 free(resource);
1646}
1647
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001648static void
1649bind_input_device(struct wl_client *client,
1650 void *data, uint32_t version, uint32_t id)
1651{
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04001652 struct wl_input_device *device = data;
1653 struct wl_resource *resource;
1654
1655 resource = wl_client_add_object(client, &wl_input_device_interface,
1656 &input_device_interface, id, data);
1657 wl_list_insert(&device->resource_list, &resource->link);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001658 resource->destroy = unbind_input_device;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001659}
1660
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001661WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001662weston_input_device_init(struct weston_input_device *device,
1663 struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001664{
Kristian Høgsberga8873122011-11-23 10:39:34 -05001665 wl_input_device_init(&device->input_device);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001666 wl_list_init(&device->drag_resource_list);
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05001667
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001668 wl_display_add_global(ec->wl_display, &wl_input_device_interface,
1669 device, bind_input_device);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001670
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001671 device->sprite = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04001672
Kristian Høgsberga8873122011-11-23 10:39:34 -05001673 device->compositor = ec;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001674 device->hotspot_x = 16;
1675 device->hotspot_y = 16;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001676 device->modifier_state = 0;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001677 device->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001678
Kristian Høgsberg9c4eecb2011-09-06 18:13:14 -04001679 device->input_device.implicit_grab.interface = &implicit_grab_interface;
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001680
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001681 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001682
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001683 device->selection_data_source = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001684}
1685
Pekka Paalanen07753fb2012-01-02 15:31:01 +02001686WL_EXPORT void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001687weston_input_device_release(struct weston_input_device *device)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02001688{
1689 wl_list_remove(&device->link);
1690 /* The global object is destroyed at wl_display_destroy() time. */
1691
1692 if (device->sprite)
1693 destroy_surface(&device->sprite->surface.resource);
1694
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001695 wl_input_device_release(&device->input_device);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02001696}
1697
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001698static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001699bind_output(struct wl_client *client,
1700 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001701{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001702 struct weston_output *output = data;
1703 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04001704 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001705
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04001706 resource = wl_client_add_object(client,
1707 &wl_output_interface, NULL, id, data);
1708
1709 wl_resource_post_event(resource,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001710 WL_OUTPUT_GEOMETRY,
1711 output->x,
1712 output->y,
1713 output->mm_width,
1714 output->mm_height,
1715 output->subpixel,
1716 output->make, output->model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001717
1718 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04001719 wl_resource_post_event(resource,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001720 WL_OUTPUT_MODE,
1721 mode->flags,
1722 mode->width,
1723 mode->height,
1724 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001725 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001726}
1727
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001728static const char vertex_shader[] =
1729 "uniform mat4 proj;\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001730 "attribute vec2 position;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001731 "attribute vec2 texcoord;\n"
1732 "varying vec2 v_texcoord;\n"
1733 "void main()\n"
1734 "{\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001735 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001736 " v_texcoord = texcoord;\n"
1737 "}\n";
1738
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001739static const char texture_fragment_shader[] =
Kristian Høgsbergdfce71d2010-12-07 20:19:10 -05001740 "precision mediump float;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001741 "varying vec2 v_texcoord;\n"
1742 "uniform sampler2D tex;\n"
Kristian Høgsberg541e5552011-12-14 09:24:11 -05001743 "uniform float alpha;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001744 "void main()\n"
1745 "{\n"
1746 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
Kristian Høgsberg541e5552011-12-14 09:24:11 -05001747 " gl_FragColor = alpha * gl_FragColor;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001748 "}\n";
1749
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001750static const char solid_fragment_shader[] =
1751 "precision mediump float;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001752 "uniform vec4 color;\n"
1753 "void main()\n"
1754 "{\n"
1755 " gl_FragColor = color\n;"
1756 "}\n";
1757
Kristian Høgsberga9468212010-06-14 21:03:11 -04001758static int
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001759compile_shader(GLenum type, const char *source)
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001760{
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001761 GLuint s;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001762 char msg[512];
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001763 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001764
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001765 s = glCreateShader(type);
1766 glShaderSource(s, 1, &source, NULL);
1767 glCompileShader(s);
1768 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001769 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001770 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1771 fprintf(stderr, "shader info: %s\n", msg);
1772 return GL_NONE;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001773 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001774
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001775 return s;
1776}
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001777
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001778static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001779weston_shader_init(struct weston_shader *shader,
1780 const char *vertex_source, const char *fragment_source)
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001781{
1782 char msg[512];
1783 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001784
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001785 shader->vertex_shader =
1786 compile_shader(GL_VERTEX_SHADER, vertex_source);
1787 shader->fragment_shader =
1788 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1789
1790 shader->program = glCreateProgram();
1791 glAttachShader(shader->program, shader->vertex_shader);
1792 glAttachShader(shader->program, shader->fragment_shader);
1793 glBindAttribLocation(shader->program, 0, "position");
1794 glBindAttribLocation(shader->program, 1, "texcoord");
1795
1796 glLinkProgram(shader->program);
1797 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001798 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001799 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001800 fprintf(stderr, "link info: %s\n", msg);
1801 return -1;
1802 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001803
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001804 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1805 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
Kristian Høgsberg541e5552011-12-14 09:24:11 -05001806 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001807
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001808 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001809}
1810
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001811static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001812init_solid_shader(struct weston_shader *shader,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001813 GLuint vertex_shader, const char *fragment_source)
1814{
1815 GLint status;
1816 char msg[512];
1817
1818 shader->vertex_shader = vertex_shader;
1819 shader->fragment_shader =
1820 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1821
1822 shader->program = glCreateProgram();
1823 glAttachShader(shader->program, shader->vertex_shader);
1824 glAttachShader(shader->program, shader->fragment_shader);
1825 glBindAttribLocation(shader->program, 0, "position");
1826 glBindAttribLocation(shader->program, 1, "texcoord");
1827
1828 glLinkProgram(shader->program);
1829 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1830 if (!status) {
1831 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1832 fprintf(stderr, "link info: %s\n", msg);
1833 return -1;
1834 }
1835
1836 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1837 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1838
1839 return 0;
1840}
1841
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001842WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001843weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001844{
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04001845 pixman_region32_fini(&output->region);
Kristian Høgsberg8b72f602011-06-23 20:46:34 -04001846 pixman_region32_fini(&output->previous_damage);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001847}
1848
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001849WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001850weston_output_move(struct weston_output *output, int x, int y)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001851{
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01001852 int flip;
1853
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001854 output->x = x;
1855 output->y = y;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001856
Kristian Høgsberg8b72f602011-06-23 20:46:34 -04001857 pixman_region32_init(&output->previous_damage);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04001858 pixman_region32_init_rect(&output->region, x, y,
1859 output->current->width,
1860 output->current->height);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001861
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001862 weston_matrix_init(&output->matrix);
1863 weston_matrix_translate(&output->matrix,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001864 -output->x - output->current->width / 2.0,
1865 -output->y - output->current->height / 2.0, 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01001866
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001867 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001868 weston_matrix_scale(&output->matrix,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001869 2.0 / output->current->width,
1870 flip * 2.0 / output->current->height, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001871
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001872 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001873}
1874
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001875WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001876weston_output_init(struct weston_output *output, struct weston_compositor *c,
1877 int x, int y, int width, int height, uint32_t flags)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001878{
1879 output->compositor = c;
1880 output->x = x;
1881 output->y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001882 output->mm_width = width;
1883 output->mm_height = height;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001884
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001885 output->flags = flags;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001886 weston_output_move(output, x, y);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001887
Benjamin Franzke06286262011-05-06 19:12:33 +02001888 output->scanout_buffer_destroy_listener.func =
1889 output_handle_scanout_buffer_destroy;
Kristian Høgsberg191454e2011-10-20 17:51:45 -04001890 output->pending_scanout_buffer_destroy_listener.func =
1891 output_handle_pending_scanout_buffer_destroy;
Ander Conselvan de Oliveiraf1621d22011-10-18 17:02:07 +03001892
Kristian Høgsberg33418202011-08-16 23:01:28 -04001893 wl_list_init(&output->frame_callback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02001894
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001895 wl_display_add_global(c->wl_display,
1896 &wl_output_interface, output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001897}
1898
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001899static void
1900shm_buffer_created(struct wl_buffer *buffer)
1901{
1902 struct wl_list *surfaces_attached_to;
1903
1904 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1905 if (!surfaces_attached_to) {
1906 buffer->user_data = NULL;
1907 return;
1908 }
1909
1910 wl_list_init(surfaces_attached_to);
1911
1912 buffer->user_data = surfaces_attached_to;
1913}
1914
1915static void
1916shm_buffer_damaged(struct wl_buffer *buffer,
1917 int32_t x, int32_t y, int32_t width, int32_t height)
1918{
1919 struct wl_list *surfaces_attached_to = buffer->user_data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001920 struct weston_surface *es;
Benjamin Franzkefab5ec12011-04-25 19:44:47 +02001921 GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001922
1923 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1924 glBindTexture(GL_TEXTURE_2D, es->texture);
1925 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Benjamin Franzkefab5ec12011-04-25 19:44:47 +02001926 tex_width, buffer->height, 0,
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001927 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1928 wl_shm_buffer_get_data(buffer));
1929 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
1930 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
1931 }
1932}
1933
1934static void
1935shm_buffer_destroyed(struct wl_buffer *buffer)
1936{
1937 struct wl_list *surfaces_attached_to = buffer->user_data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001938 struct weston_surface *es, *next;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001939
1940 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001941 wl_list_remove(&es->buffer_link);
Kristian Høgsbergfac11d22011-05-02 13:35:17 -04001942 wl_list_init(&es->buffer_link);
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001943 }
1944
1945 free(surfaces_attached_to);
1946}
1947
1948const static struct wl_shm_callbacks shm_callbacks = {
1949 shm_buffer_created,
1950 shm_buffer_damaged,
1951 shm_buffer_destroyed
1952};
1953
Kristian Høgsberga8873122011-11-23 10:39:34 -05001954static void
1955compositor_bind(struct wl_client *client,
1956 void *data, uint32_t version, uint32_t id)
1957{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001958 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05001959
1960 wl_client_add_object(client, &wl_compositor_interface,
1961 &compositor_interface, id, compositor);
1962}
1963
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001964WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001965weston_compositor_init(struct weston_compositor *ec, struct wl_display *display)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001966{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001967 struct wl_event_loop *loop;
Kristian Høgsbergd711d0c2011-01-14 17:28:21 -05001968 const char *extensions;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001969
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001970 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001971
Kristian Høgsberga8873122011-11-23 10:39:34 -05001972 if (!wl_display_add_global(display, &wl_compositor_interface,
1973 ec, compositor_bind))
1974 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001975
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001976 ec->shm = wl_shm_init(display, &shm_callbacks);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04001977
1978 ec->image_target_texture_2d =
1979 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1980 ec->image_target_renderbuffer_storage = (void *)
1981 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1982 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1983 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1984 ec->bind_display =
1985 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1986 ec->unbind_display =
1987 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1988
1989 extensions = (const char *) glGetString(GL_EXTENSIONS);
1990 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1991 fprintf(stderr,
1992 "GL_EXT_texture_format_BGRA8888 not available\n");
1993 return -1;
1994 }
1995
1996 extensions =
1997 (const char *) eglQueryString(ec->display, EGL_EXTENSIONS);
1998 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1999 ec->has_bind_display = 1;
2000 if (ec->has_bind_display)
2001 ec->bind_display(ec->display, ec->wl_display);
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04002002
Kristian Høgsberg201a9042008-12-10 00:40:50 -05002003 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002004 wl_list_init(&ec->input_device_list);
2005 wl_list_init(&ec->output_list);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04002006 wl_list_init(&ec->binding_list);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002007 wl_list_init(&ec->animation_list);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002008 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002009 ec->fade.animation.frame = fade_frame;
2010 wl_list_init(&ec->fade.animation.link);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04002011
Pekka Paalanen35ce06d2012-01-03 11:39:13 +02002012 ec->screenshooter = screenshooter_create(ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002013
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002014 weston_data_device_manager_init(ec);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002015
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002016 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002017
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002018 if (weston_shader_init(&ec->texture_shader,
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002019 vertex_shader, texture_fragment_shader) < 0)
Kristian Høgsberga9468212010-06-14 21:03:11 -04002020 return -1;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002021 if (init_solid_shader(&ec->solid_shader,
2022 ec->texture_shader.vertex_shader,
2023 solid_fragment_shader) < 0)
2024 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05002025
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002026 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002027 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Pekka Paalanen7296e792011-12-07 16:22:00 +02002028 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002029
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002030 weston_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002031
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002032 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002033}
2034
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002035WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002036weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002037{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002038 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002039
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002040 wl_event_source_remove(ec->idle_source);
2041
Pekka Paalanen35ce06d2012-01-03 11:39:13 +02002042 if (ec->screenshooter)
2043 screenshooter_destroy(ec->screenshooter);
2044
Matt Roper361d2ad2011-08-29 13:52:23 -07002045 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002046 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002047 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002048
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05002049 weston_binding_list_destroy_all(&ec->binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002050
2051 wl_shm_finish(ec->shm);
2052
2053 wl_array_release(&ec->vertices);
2054 wl_array_release(&ec->indices);
Matt Roper361d2ad2011-08-29 13:52:23 -07002055}
2056
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002057static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002058{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002059 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002060
Kristian Høgsberg3cad4362011-07-15 21:09:24 -04002061 fprintf(stderr, "caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002062 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002063
2064 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002065}
2066
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002067static void *
2068load_module(const char *name, const char *entrypoint, void **handle)
2069{
2070 char path[PATH_MAX];
2071 void *module, *init;
2072
2073 if (name[0] != '/')
2074 snprintf(path, sizeof path, MODULEDIR "/%s", name);
2075 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02002076 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002077
2078 module = dlopen(path, RTLD_LAZY);
2079 if (!module) {
2080 fprintf(stderr,
2081 "failed to load module: %s\n", dlerror());
2082 return NULL;
2083 }
2084
2085 init = dlsym(module, entrypoint);
2086 if (!init) {
2087 fprintf(stderr,
2088 "failed to lookup init function: %s\n", dlerror());
2089 return NULL;
2090 }
2091
2092 return init;
2093}
2094
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002095int main(int argc, char *argv[])
2096{
2097 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002098 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02002099 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002100 struct wl_event_loop *loop;
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002101 int o, xserver = 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002102 void *shell_module, *backend_module;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002103 int (*shell_init)(struct weston_compositor *ec);
2104 struct weston_compositor
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002105 *(*backend_init)(struct wl_display *display, char *options);
2106 char *backend = NULL;
2107 char *backend_options = "";
2108 char *shell = NULL;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002109 char *p;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002110 int option_idle_time = 300;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02002111 int i;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002112
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002113 static const char opts[] = "B:b:o:S:i:s:x";
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002114 static const struct option longopts[ ] = {
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002115 { "backend", 1, NULL, 'B' },
2116 { "backend-options", 1, NULL, 'o' },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002117 { "socket", 1, NULL, 'S' },
2118 { "idle-time", 1, NULL, 'i' },
2119 { "shell", 1, NULL, 's' },
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002120 { "xserver", 0, NULL, 'x' },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002121 { NULL, }
2122 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05002123
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002124 while (o = getopt_long(argc, argv, opts, longopts, &o), o > 0) {
2125 switch (o) {
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002126 case 'B':
2127 backend = optarg;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002128 break;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002129 case 'o':
2130 backend_options = optarg;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002131 break;
2132 case 'S':
2133 option_socket_name = optarg;
2134 break;
2135 case 'i':
2136 option_idle_time = strtol(optarg, &p, 0);
2137 if (*p != '\0') {
2138 fprintf(stderr,
2139 "invalid idle time option: %s\n",
2140 optarg);
2141 exit(EXIT_FAILURE);
2142 }
2143 break;
2144 case 's':
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002145 shell = optarg;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002146 break;
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002147 case 'x':
2148 xserver = 1;
2149 break;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002150 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04002151 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002152
2153 display = wl_display_create();
2154
Tiago Vignatti2116b892011-08-08 05:52:59 -07002155 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02002156 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
2157 display);
2158 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
2159 display);
2160 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
2161 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07002162
2163 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02002164 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
2165 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05002166
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002167 if (!backend) {
2168 if (getenv("WAYLAND_DISPLAY"))
2169 backend = "wayland-backend.so";
2170 else if (getenv("DISPLAY"))
2171 backend = "x11-backend.so";
2172 else if (getenv("OPENWFD"))
2173 backend = "openwfd-backend.so";
2174 else
2175 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002176 }
2177
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002178 if (!shell)
2179 shell = "desktop-shell.so";
Kristian Høgsberga9410222011-01-14 17:22:35 -05002180
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002181 backend_init = load_module(backend, "backend_init", &backend_module);
2182 if (!backend_init)
2183 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05002184
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002185 shell_init = load_module(shell, "shell_init", &shell_module);
2186 if (!shell_init)
2187 exit(EXIT_FAILURE);
Benjamin Franzke5d007092011-04-04 00:30:25 +02002188
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002189 ec = backend_init(display, backend_options);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05002190 if (ec == NULL) {
2191 fprintf(stderr, "failed to create compositor\n");
2192 exit(EXIT_FAILURE);
2193 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04002194
Pekka Paalanen7296e792011-12-07 16:22:00 +02002195 ec->option_idle_time = option_idle_time;
2196 ec->idle_time = option_idle_time;
2197
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002198 if (shell_init(ec) < 0)
2199 exit(EXIT_FAILURE);
2200
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002201 if (xserver)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002202 weston_xserver_init(ec);
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002203
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002204 if (wl_display_add_socket(display, option_socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002205 fprintf(stderr, "failed to add socket: %m\n");
2206 exit(EXIT_FAILURE);
2207 }
2208
2209 wl_display_run(display);
2210
Pekka Paalanen0135abe2012-01-03 10:01:20 +02002211 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05002212 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02002213
Kristian Høgsberg3cad4362011-07-15 21:09:24 -04002214 if (xserver)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002215 weston_xserver_destroy(ec);
Kristian Høgsberg3cad4362011-07-15 21:09:24 -04002216
Pekka Paalanen3c647232011-12-22 13:43:43 +02002217 ec->shell->destroy(ec->shell);
2218
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002219 if (ec->has_bind_display)
2220 ec->unbind_display(ec->display, display);
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002221
Pekka Paalanen51c769f2012-01-02 16:03:26 +02002222 for (i = ARRAY_LENGTH(signals); i;)
2223 wl_event_source_remove(signals[--i]);
2224
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002225 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02002226 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002227
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002228 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002229}