blob: c7099f055c7fb7eaa0fe074a1239c3ea2ff91b15 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05008 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050017 */
18
Kristian Høgsberg06bc2642010-12-01 09:50:16 -050019#define _GNU_SOURCE
20
Kristian Høgsberga9410222011-01-14 17:22:35 -050021#include "config.h"
22
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040023#include <stdio.h>
24#include <string.h>
25#include <stdlib.h>
26#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010027#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050028#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020029#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040030#include <sys/ioctl.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040031#include <sys/wait.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040032#include <fcntl.h>
33#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050034#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040035#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040036#include <dlfcn.h>
Kristian Høgsbergd34912c2011-05-02 10:36:04 -040037#include <getopt.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040038#include <signal.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050039
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050040#include "wayland-server.h"
Kristian Høgsberg82863022010-06-04 21:52:02 -040041#include "compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050042
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010043/* The plan here is to generate a random anonymous socket name and
44 * advertise that through a service on the session dbus.
45 */
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -050046static const char *option_socket_name = NULL;
Kristian Høgsberg751b5bc2011-06-23 15:33:47 -040047static const char *option_background = "background.png";
Kristian Høgsbergd34912c2011-05-02 10:36:04 -040048static int option_idle_time = 300;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050049
Kristian Høgsberg27da5382011-06-21 17:32:25 -040050static struct wl_list child_process_list;
51
52static int
53sigchld_handler(int signal_number, void *data)
54{
55 struct wlsc_process *p;
56 int status;
57 pid_t pid;
58
59 pid = wait(&status);
60 wl_list_for_each(p, &child_process_list, link) {
61 if (p->pid == pid)
62 break;
63 }
64
65 if (&p->link == &child_process_list) {
66 fprintf(stderr, "unknown child process exited\n");
67 return 1;
68 }
69
70 wl_list_remove(&p->link);
71 p->cleanup(p, status);
72
73 return 1;
74}
75
76WL_EXPORT void
77wlsc_watch_process(struct wlsc_process *process)
78{
79 wl_list_insert(&child_process_list, &process->link);
80}
81
Kristian Høgsbergd34912c2011-05-02 10:36:04 -040082WL_EXPORT void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050083wlsc_matrix_init(struct wlsc_matrix *matrix)
84{
85 static const struct wlsc_matrix identity = {
86 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
87 };
88
89 memcpy(matrix, &identity, sizeof identity);
90}
91
92static void
93wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
94{
95 struct wlsc_matrix tmp;
Kristian Høgsberg27803c62010-06-06 22:23:21 -040096 const GLfloat *row, *column;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050097 div_t d;
98 int i, j;
99
100 for (i = 0; i < 16; i++) {
101 tmp.d[i] = 0;
102 d = div(i, 4);
103 row = m->d + d.quot * 4;
104 column = n->d + d.rem;
105 for (j = 0; j < 4; j++)
106 tmp.d[i] += row[j] * column[j * 4];
107 }
108 memcpy(m, &tmp, sizeof tmp);
109}
110
Kristian Høgsbergd880e142011-05-02 13:53:45 -0400111WL_EXPORT void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400112wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500113{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500114 struct wlsc_matrix translate = {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500115 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
116 };
117
118 wlsc_matrix_multiply(matrix, &translate);
119}
120
Kristian Høgsbergd880e142011-05-02 13:53:45 -0400121WL_EXPORT void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400122wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500123{
124 struct wlsc_matrix scale = {
125 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
126 };
127
128 wlsc_matrix_multiply(matrix, &scale);
129}
130
131static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400132wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
133{
134 int i, j;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400135 struct wlsc_vector t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400136
137 for (i = 0; i < 4; i++) {
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400138 t.f[i] = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400139 for (j = 0; j < 4; j++)
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400140 t.f[i] += v->f[j] * matrix->d[i + j * 4];
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400141 }
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500142
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400143 *v = t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400144}
145
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400146WL_EXPORT void
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400147wlsc_spring_init(struct wlsc_spring *spring,
148 double k, double current, double target)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400149{
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400150 spring->k = k;
151 spring->friction = 100.0;
152 spring->current = current;
153 spring->previous = current;
154 spring->target = target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400155}
156
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400157WL_EXPORT void
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400158wlsc_spring_update(struct wlsc_spring *spring, uint32_t msec)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400159{
Kristian Høgsberg4a9be132011-05-02 13:38:03 -0400160 double force, v, current, step;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400161
Kristian Høgsberg52612f12011-05-19 11:55:50 -0400162 step = (msec - spring->timestamp) / 300.0;
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400163 spring->timestamp = msec;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400164
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400165 current = spring->current;
166 v = current - spring->previous;
167 force = spring->k * (spring->target - current) / 10.0 +
168 (spring->previous - current) - v * spring->friction;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400169
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400170 spring->current =
171 current + (current - spring->previous) + force * step * step;
172 spring->previous = current;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400173
Kristian Høgsberg52612f12011-05-19 11:55:50 -0400174#if 0
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400175 if (spring->current >= 1.0) {
Kristian Høgsberg4a9be132011-05-02 13:38:03 -0400176#ifdef TWEENER_BOUNCE
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400177 spring->current = 2.0 - spring->current;
178 spring->previous = 2.0 - spring->previous;
Kristian Høgsberg4a9be132011-05-02 13:38:03 -0400179#else
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400180 spring->current = 1.0;
181 spring->previous = 1.0;
Kristian Høgsberg4a9be132011-05-02 13:38:03 -0400182#endif
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400183 }
184
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400185 if (spring->current <= 0.0) {
186 spring->current = 0.0;
187 spring->previous = 0.0;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400188 }
Kristian Høgsberg52612f12011-05-19 11:55:50 -0400189#endif
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400190}
191
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400192WL_EXPORT int
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400193wlsc_spring_done(struct wlsc_spring *spring)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400194{
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400195 return fabs(spring->previous - spring->target) < 0.0002 &&
196 fabs(spring->current - spring->target) < 0.0002;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400197}
198
Benjamin Franzke06286262011-05-06 19:12:33 +0200199static void
200surface_handle_buffer_destroy(struct wl_listener *listener,
201 struct wl_resource *resource, uint32_t time)
202{
203 struct wlsc_surface *es = container_of(listener, struct wlsc_surface,
204 buffer_destroy_listener);
205 struct wl_buffer *buffer = (struct wl_buffer *) resource;
206
207 if (es->buffer == buffer)
208 es->buffer = NULL;
209}
210
211static void
212output_handle_scanout_buffer_destroy(struct wl_listener *listener,
213 struct wl_resource *resource,
214 uint32_t time)
215{
216 struct wlsc_output *output =
217 container_of(listener, struct wlsc_output,
218 scanout_buffer_destroy_listener);
219 struct wl_buffer *buffer = (struct wl_buffer *) resource;
220
221 if (output->scanout_buffer == buffer)
222 output->scanout_buffer = NULL;
223}
224
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400225WL_EXPORT struct wlsc_surface *
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400226wlsc_surface_create(struct wlsc_compositor *compositor,
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400227 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500228{
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400229 struct wlsc_surface *surface;
230
231 surface = malloc(sizeof *surface);
232 if (surface == NULL)
233 return NULL;
234
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500235 wl_list_init(&surface->link);
Benjamin Franzkebab41fb2011-03-07 18:04:59 +0100236 wl_list_init(&surface->buffer_link);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500237 surface->map_type = WLSC_SURFACE_MAP_UNMAPPED;
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500238
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500239 glGenTextures(1, &surface->texture);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400240 glBindTexture(GL_TEXTURE_2D, surface->texture);
241 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
242 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400243
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400244 surface->surface.resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400245
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500246 surface->compositor = compositor;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400247 surface->visual = WLSC_NONE_VISUAL;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200248 surface->image = EGL_NO_IMAGE_KHR;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200249 surface->saved_texture = 0;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400250 surface->x = x;
251 surface->y = y;
252 surface->width = width;
253 surface->height = height;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400254
Benjamin Franzke06286262011-05-06 19:12:33 +0200255 surface->buffer = NULL;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400256 surface->output = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200257
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400258 pixman_region32_init(&surface->damage);
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400259 pixman_region32_init(&surface->opaque);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400260
Benjamin Franzke06286262011-05-06 19:12:33 +0200261 surface->buffer_destroy_listener.func = surface_handle_buffer_destroy;
262 wl_list_init(&surface->buffer_destroy_listener.link);
263
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400264 surface->transform = NULL;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500265
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400266 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500267}
268
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400269WL_EXPORT void
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500270wlsc_surface_damage_rectangle(struct wlsc_surface *surface,
271 int32_t x, int32_t y,
272 int32_t width, int32_t height)
273{
274 struct wlsc_compositor *compositor = surface->compositor;
275
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400276 pixman_region32_union_rect(&surface->damage,
277 &surface->damage,
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500278 surface->x + x, surface->y + y,
279 width, height);
280 wlsc_compositor_schedule_repaint(compositor);
281}
282
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400283WL_EXPORT void
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500284wlsc_surface_damage(struct wlsc_surface *surface)
285{
286 wlsc_surface_damage_rectangle(surface, 0, 0,
287 surface->width, surface->height);
288}
289
Kristian Høgsbergb8a98332011-06-23 21:00:04 -0400290WL_EXPORT void
291wlsc_surface_damage_below(struct wlsc_surface *surface)
292{
293 struct wlsc_surface *below;
294
295 if (surface->link.next == &surface->compositor->surface_list)
296 return;
297
298 below = container_of(surface->link.next, struct wlsc_surface, link);
299
300 pixman_region32_union_rect(&below->damage,
301 &below->damage,
302 surface->x, surface->y,
303 surface->width, surface->height);
304 wlsc_compositor_schedule_repaint(surface->compositor);
305}
306
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400307WL_EXPORT void
308wlsc_surface_configure(struct wlsc_surface *surface,
309 int x, int y, int width, int height)
310{
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400311 wlsc_surface_damage_below(surface);
312
313 surface->x = x;
314 surface->y = y;
315 surface->width = width;
316 surface->height = height;
317
318 wlsc_surface_assign_output(surface);
319 wlsc_surface_damage(surface);
320
321 pixman_region32_fini(&surface->opaque);
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400322 if (surface->visual == WLSC_RGB_VISUAL)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400323 pixman_region32_init_rect(&surface->opaque,
324 surface->x, surface->y,
325 surface->width, surface->height);
326 else
327 pixman_region32_init(&surface->opaque);
328}
329
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400330WL_EXPORT uint32_t
331wlsc_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500332{
333 struct timeval tv;
334
335 gettimeofday(&tv, NULL);
336
337 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
338}
339
Kristian Høgsberg54879822008-11-23 17:07:32 -0500340static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400341destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500342{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400343 struct wlsc_surface *surface =
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500344 container_of(resource, struct wlsc_surface, surface.resource);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400345 struct wlsc_compositor *compositor = surface->compositor;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400346
Kristian Høgsbergb8a98332011-06-23 21:00:04 -0400347 wlsc_surface_damage_below(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500348
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400349 wl_list_remove(&surface->link);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200350 if (surface->saved_texture == 0)
351 glDeleteTextures(1, &surface->texture);
352 else
353 glDeleteTextures(1, &surface->saved_texture);
354
Benjamin Franzke06286262011-05-06 19:12:33 +0200355 if (surface->buffer)
356 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400357
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200358 if (surface->image != EGL_NO_IMAGE_KHR)
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400359 compositor->destroy_image(compositor->display,
360 surface->image);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200361
Benjamin Franzkebab41fb2011-03-07 18:04:59 +0100362 wl_list_remove(&surface->buffer_link);
363
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400364 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500365}
366
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100367static void
368wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
369{
370 struct wlsc_surface *es = (struct wlsc_surface *) surface;
371 struct wlsc_compositor *ec = es->compositor;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100372 struct wl_list *surfaces_attached_to;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100373
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100374 if (es->saved_texture != 0)
375 es->texture = es->saved_texture;
376
377 glBindTexture(GL_TEXTURE_2D, es->texture);
378
379 if (wl_buffer_is_shm(buffer)) {
380 /* Unbind any EGLImage texture that may be bound, so we don't
381 * overwrite it.*/
382 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
383 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200384 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100385 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200386 es->pitch, buffer->height, 0,
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100387 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
388 wl_shm_buffer_get_data(buffer));
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400389
390 switch (wl_shm_buffer_get_format(buffer)) {
391 case WL_SHM_FORMAT_ARGB32:
392 es->visual = WLSC_ARGB_VISUAL;
393 break;
394 case WL_SHM_FORMAT_PREMULTIPLIED_ARGB32:
395 es->visual = WLSC_PREMUL_ARGB_VISUAL;
396 break;
397 case WL_SHM_FORMAT_XRGB32:
398 es->visual = WLSC_RGB_VISUAL;
399 break;
400 }
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100401
402 surfaces_attached_to = buffer->user_data;
403
Kristian Høgsbergfac11d22011-05-02 13:35:17 -0400404 wl_list_remove(&es->buffer_link);
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100405 wl_list_insert(surfaces_attached_to, &es->buffer_link);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100406 } else {
Benjamin Franzkefb4b5a22011-06-21 10:44:37 +0200407 if (es->image != EGL_NO_IMAGE_KHR)
408 ec->destroy_image(ec->display, es->image);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400409 es->image = ec->create_image(ec->display, NULL,
410 EGL_WAYLAND_BUFFER_WL,
411 buffer, NULL);
412
413 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400414
415 /* FIXME: we need to get the visual from the wl_buffer */
416 es->visual = WLSC_PREMUL_ARGB_VISUAL;
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200417 es->pitch = es->width;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100418 }
419}
420
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200421static void
422wlsc_sprite_attach(struct wlsc_sprite *sprite, struct wl_surface *surface)
423{
424 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400425 struct wlsc_compositor *ec = es->compositor;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200426
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200427 es->pitch = es->width;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200428 es->image = sprite->image;
429 if (sprite->image != EGL_NO_IMAGE_KHR) {
430 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400431 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200432 } else {
433 if (es->saved_texture == 0)
434 es->saved_texture = es->texture;
435 es->texture = sprite->texture;
436 }
437
438 es->visual = sprite->visual;
Benjamin Franzke06286262011-05-06 19:12:33 +0200439
440 if (es->buffer)
441 es->buffer = NULL;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200442}
443
444enum sprite_usage {
445 SPRITE_USE_CURSOR = (1 << 0),
446};
447
448static struct wlsc_sprite *
449create_sprite_from_png(struct wlsc_compositor *ec,
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400450 const char *filename, uint32_t usage)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500451{
452 uint32_t *pixels;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200453 struct wlsc_sprite *sprite;
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400454 int32_t width, height;
455 uint32_t stride;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500456
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400457 pixels = wlsc_load_image(filename, &width, &height, &stride);
458 if (pixels == NULL)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200459 return NULL;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500460
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200461 sprite = malloc(sizeof *sprite);
462 if (sprite == NULL) {
463 free(pixels);
464 return NULL;
465 }
466
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400467 sprite->visual = WLSC_PREMUL_ARGB_VISUAL;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200468 sprite->width = width;
469 sprite->height = height;
470 sprite->image = EGL_NO_IMAGE_KHR;
471
472 if (usage & SPRITE_USE_CURSOR && ec->create_cursor_image != NULL)
473 sprite->image = ec->create_cursor_image(ec, width, height);
474
475 glGenTextures(1, &sprite->texture);
476 glBindTexture(GL_TEXTURE_2D, sprite->texture);
477 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
478 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
479 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
480 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
481
482 if (sprite->image != EGL_NO_IMAGE_KHR) {
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400483 ec->image_target_texture_2d(GL_TEXTURE_2D, sprite->image);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200484 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
485 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
486 } else {
487 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, width, height, 0,
488 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
489 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500490
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500491 free(pixels);
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500492
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200493 return sprite;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500494}
495
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400496static const struct {
497 const char *filename;
498 int hotspot_x, hotspot_y;
499} pointer_images[] = {
Kristian Høgsberg4219a402010-08-16 16:43:03 -0400500 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
501 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
502 { DATADIR "/wayland/bottom_side.png", 16, 20 },
503 { DATADIR "/wayland/grabbing.png", 20, 17 },
504 { DATADIR "/wayland/left_ptr.png", 10, 5 },
505 { DATADIR "/wayland/left_side.png", 10, 20 },
506 { DATADIR "/wayland/right_side.png", 30, 19 },
507 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
508 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
509 { DATADIR "/wayland/top_side.png", 18, 8 },
510 { DATADIR "/wayland/xterm.png", 15, 15 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400511};
512
513static void
514create_pointer_images(struct wlsc_compositor *ec)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500515{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400516 int i, count;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400517
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400518 count = ARRAY_LENGTH(pointer_images);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200519 ec->pointer_sprites = malloc(count * sizeof *ec->pointer_sprites);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400520 for (i = 0; i < count; i++) {
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200521 ec->pointer_sprites[i] =
522 create_sprite_from_png(ec,
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500523 pointer_images[i].filename,
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200524 SPRITE_USE_CURSOR);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400525 }
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400526}
527
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500528static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500529background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500530{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500531 struct wlsc_surface *background;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200532 struct wlsc_sprite *sprite;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500533
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400534 background = wlsc_surface_create(output->compositor,
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400535 output->x, output->y,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400536 output->current->width,
537 output->current->height);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500538 if (background == NULL)
539 return NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500540
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400541 sprite = create_sprite_from_png(output->compositor, filename, 0);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200542 if (sprite == NULL) {
Benjamin Franzked3b023e2011-01-15 12:34:49 +0100543 free(background);
544 return NULL;
545 }
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100546
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200547 wlsc_sprite_attach(sprite, &background->surface);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500548
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500549 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500550}
551
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400552static int
553texture_region(struct wlsc_surface *es, pixman_region32_t *region)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500554{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500555 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500556 GLfloat *v, inv_width, inv_height;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500557 pixman_box32_t *rectangles;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400558 unsigned int *p;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500559 int i, n;
560
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400561 rectangles = pixman_region32_rectangles(region, &n);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500562 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
563 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200564 inv_width = 1.0 / es->pitch;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500565 inv_height = 1.0 / es->height;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400566
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500567 for (i = 0; i < n; i++, v += 16, p += 6) {
568 v[ 0] = rectangles[i].x1;
569 v[ 1] = rectangles[i].y1;
570 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
571 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500572
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500573 v[ 4] = rectangles[i].x1;
574 v[ 5] = rectangles[i].y2;
575 v[ 6] = v[ 2];
576 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500577
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500578 v[ 8] = rectangles[i].x2;
579 v[ 9] = rectangles[i].y1;
580 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
581 v[11] = v[ 3];
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500582
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500583 v[12] = rectangles[i].x2;
584 v[13] = rectangles[i].y2;
585 v[14] = v[10];
586 v[15] = v[ 7];
587
588 p[0] = i * 4 + 0;
589 p[1] = i * 4 + 1;
590 p[2] = i * 4 + 2;
591 p[3] = i * 4 + 2;
592 p[4] = i * 4 + 1;
593 p[5] = i * 4 + 3;
594 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500595
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400596 return n;
597}
598
599static void
600transform_vertex(struct wlsc_surface *surface,
601 GLfloat x, GLfloat y, GLfloat u, GLfloat v, GLfloat *r)
602{
603 struct wlsc_vector t;
604
605 t.f[0] = x;
606 t.f[1] = y;
607 t.f[2] = 0.0;
608 t.f[3] = 1.0;
609
Kristian Høgsberg0bc0e242011-05-02 14:35:40 -0400610 wlsc_matrix_transform(&surface->transform->matrix, &t);
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400611
612 r[ 0] = t.f[0];
613 r[ 1] = t.f[1];
614 r[ 2] = u;
615 r[ 3] = v;
616}
617
618static int
619texture_transformed_surface(struct wlsc_surface *es)
620{
621 struct wlsc_compositor *ec = es->compositor;
622 GLfloat *v;
623 unsigned int *p;
624
625 v = wl_array_add(&ec->vertices, 16 * sizeof *v);
626 p = wl_array_add(&ec->indices, 6 * sizeof *p);
627
628 transform_vertex(es, es->x, es->y, 0.0, 0.0, &v[0]);
629 transform_vertex(es, es->x, es->y + es->height, 0.0, 1.0, &v[4]);
630 transform_vertex(es, es->x + es->width, es->y, 1.0, 0.0, &v[8]);
631 transform_vertex(es, es->x + es->width, es->y + es->height,
632 1.0, 1.0, &v[12]);
633
634 p[0] = 0;
635 p[1] = 1;
636 p[2] = 2;
637 p[3] = 2;
638 p[4] = 1;
639 p[5] = 3;
640
641 return 1;
642}
643
644static void
645wlsc_surface_draw(struct wlsc_surface *es,
646 struct wlsc_output *output, pixman_region32_t *clip)
647{
648 struct wlsc_compositor *ec = es->compositor;
649 GLfloat *v;
650 pixman_region32_t repaint;
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400651 GLint filter;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400652 int n;
653
654 pixman_region32_init_rect(&repaint,
655 es->x, es->y, es->width, es->height);
656 pixman_region32_intersect(&repaint, &repaint, clip);
657 if (!pixman_region32_not_empty(&repaint))
658 return;
659
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400660 switch (es->visual) {
661 case WLSC_ARGB_VISUAL:
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400662 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
663 glEnable(GL_BLEND);
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400664 break;
665 case WLSC_PREMUL_ARGB_VISUAL:
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400666 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
667 glEnable(GL_BLEND);
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400668 break;
669 case WLSC_RGB_VISUAL:
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400670 glDisable(GL_BLEND);
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400671 break;
672 default:
673 fprintf(stderr, "bogus visual\n");
674 break;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400675 }
676
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400677 if (es->transform == NULL) {
678 filter = GL_NEAREST;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400679 n = texture_region(es, &repaint);
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400680 } else {
681 filter = GL_LINEAR;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400682 n = texture_transformed_surface(es);
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400683 }
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400684
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500685 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400686 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
687 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
688
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500689 v = ec->vertices.data;
690 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
691 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400692 glEnableVertexAttribArray(0);
693 glEnableVertexAttribArray(1);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500694 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
695
696 ec->vertices.size = 0;
697 ec->indices.size = 0;
698 pixman_region32_fini(&repaint);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500699}
700
701static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400702wlsc_surface_raise(struct wlsc_surface *surface)
703{
704 struct wlsc_compositor *compositor = surface->compositor;
705
706 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400707 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsbergcd0d10b2011-06-24 08:28:07 -0400708 wlsc_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400709}
710
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400711WL_EXPORT void
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400712wlsc_compositor_damage_all(struct wlsc_compositor *compositor)
713{
714 struct wlsc_output *output;
715
716 wl_list_for_each(output, &compositor->output_list, link)
717 wlsc_output_damage(output);
718}
719
Benjamin Franzke06286262011-05-06 19:12:33 +0200720static inline void
721wlsc_buffer_post_release(struct wl_buffer *buffer)
722{
Benjamin Franzke6f5fc692011-06-21 19:34:19 +0200723 if (buffer == NULL || --buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200724 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200725
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400726 assert(buffer->resource.client != NULL);
727 wl_resource_post_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200728}
729
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400730WL_EXPORT void
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400731wlsc_output_damage(struct wlsc_output *output)
732{
733 struct wlsc_compositor *compositor = output->compositor;
734
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400735 pixman_region32_union(&compositor->damage,
736 &compositor->damage, &output->region);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400737 wlsc_compositor_schedule_repaint(compositor);
738}
739
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400740static void
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400741fade_frame(struct wlsc_animation *animation,
742 struct wlsc_output *output, uint32_t msecs)
743{
744 struct wlsc_compositor *compositor =
745 container_of(animation,
746 struct wlsc_compositor, fade.animation);
747
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400748 wlsc_spring_update(&compositor->fade.spring, msecs);
749 if (wlsc_spring_done(&compositor->fade.spring)) {
750 if (compositor->fade.spring.current > 0.999) {
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400751 compositor->state = WLSC_COMPOSITOR_SLEEPING;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400752 compositor->shell->lock(compositor->shell);
753 }
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400754 compositor->fade.spring.current =
755 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400756 wl_list_remove(&animation->link);
757 wl_list_init(&animation->link);
758 }
759
760 wlsc_output_damage(output);
761}
762
763static void
764fade_output(struct wlsc_output *output,
765 GLfloat tint, pixman_region32_t *region)
766{
767 struct wlsc_compositor *compositor = output->compositor;
768 struct wlsc_surface surface;
769 GLfloat color[4] = { 0.0, 0.0, 0.0, tint };
770
771 surface.compositor = compositor;
772 surface.x = output->x;
773 surface.y = output->y;
Tiago Vignatti2cc8b872011-08-19 15:10:40 +0300774 surface.pitch = output->current->width;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400775 surface.width = output->current->width;
776 surface.height = output->current->height;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400777 surface.texture = GL_NONE;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400778 surface.transform = NULL;
Kristian Høgsbergc352ab02011-04-23 15:34:50 -0400779
780 if (tint <= 1.0)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400781 surface.visual = WLSC_PREMUL_ARGB_VISUAL;
Kristian Høgsbergc352ab02011-04-23 15:34:50 -0400782 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400783 surface.visual = WLSC_RGB_VISUAL;
Kristian Høgsbergc352ab02011-04-23 15:34:50 -0400784
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400785 glUseProgram(compositor->solid_shader.program);
786 glUniformMatrix4fv(compositor->solid_shader.proj_uniform,
787 1, GL_FALSE, output->matrix.d);
788 glUniform4fv(compositor->solid_shader.color_uniform, 1, color);
789 wlsc_surface_draw(&surface, output, region);
790}
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200791
792static void
793wlsc_output_set_cursor(struct wlsc_output *output,
794 struct wl_input_device *dev, int force_sw)
795{
796 struct wlsc_compositor *ec = output->compositor;
797 struct wlsc_input_device *device = (struct wlsc_input_device *) dev;
798 pixman_region32_t cursor_region;
799 int use_hardware_cursor = 1, prior_was_hardware;
800
801 pixman_region32_init_rect(&cursor_region,
802 device->sprite->x, device->sprite->y,
803 device->sprite->width,
804 device->sprite->height);
805
806 pixman_region32_intersect(&cursor_region, &cursor_region, &output->region);
807
808 if (!pixman_region32_not_empty(&cursor_region)) {
809 output->set_hardware_cursor(output, NULL);
810 goto out;
811 }
812
813 prior_was_hardware = wl_list_empty(&device->sprite->link);
814 if (force_sw || output->set_hardware_cursor(output, device) < 0) {
Matt Roper11568a72011-08-29 15:59:37 -0700815 if (prior_was_hardware) {
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200816 wlsc_surface_damage(device->sprite);
Matt Roper11568a72011-08-29 15:59:37 -0700817 output->set_hardware_cursor(output, NULL);
818 }
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200819 use_hardware_cursor = 0;
820 } else if (!prior_was_hardware) {
821 wlsc_surface_damage_below(device->sprite);
822 }
823
824 /* Remove always to be on top. */
825 wl_list_remove(&device->sprite->link);
Kristian Høgsberg4ebf3a02011-08-29 16:47:09 -0400826 if (!use_hardware_cursor && ec->focus)
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200827 wl_list_insert(&ec->surface_list, &device->sprite->link);
828 else
829 wl_list_init(&device->sprite->link);
830
831out:
832 pixman_region32_fini(&cursor_region);
833}
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400834
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400835static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400836wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400837{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500838 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500839 struct wlsc_surface *es;
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400840 pixman_region32_t opaque, new_damage, total_damage, repaint;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500841
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100842 output->prepare_render(output);
843
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400844 glViewport(0, 0, output->current->width, output->current->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500845
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -0400846 glUseProgram(ec->texture_shader.program);
847 glUniformMatrix4fv(ec->texture_shader.proj_uniform,
848 1, GL_FALSE, output->matrix.d);
849 glUniform1i(ec->texture_shader.tex_uniform, 0);
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500850
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200851 wlsc_output_set_cursor(output, ec->input_device,
Kristian Høgsberg4ebf3a02011-08-29 16:47:09 -0400852 ec->fade.spring.current >= 0.001);
Benjamin Franzkea8bdeae2011-06-28 22:56:43 +0200853
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500854 pixman_region32_init(&new_damage);
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400855 pixman_region32_copy(&new_damage, &ec->damage);
856 pixman_region32_init(&opaque);
857
Kristian Høgsberg8b72f602011-06-23 20:46:34 -0400858 wl_list_for_each(es, &ec->surface_list, link) {
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400859 pixman_region32_subtract(&es->damage, &es->damage, &opaque);
Kristian Høgsberg8b72f602011-06-23 20:46:34 -0400860 pixman_region32_union(&new_damage, &new_damage, &es->damage);
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400861 pixman_region32_union(&opaque, &opaque, &es->opaque);
Kristian Høgsberg8b72f602011-06-23 20:46:34 -0400862 }
863
864 pixman_region32_subtract(&ec->damage, &ec->damage, &output->region);
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400865
Kristian Høgsberg8b72f602011-06-23 20:46:34 -0400866 pixman_region32_init(&total_damage);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500867 pixman_region32_union(&total_damage, &new_damage,
Kristian Høgsberg8b72f602011-06-23 20:46:34 -0400868 &output->previous_damage);
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400869 pixman_region32_intersect(&output->previous_damage,
870 &new_damage, &output->region);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500871
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400872 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -0400873 pixman_region32_fini(&new_damage);
874
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500875 es = container_of(ec->surface_list.next, struct wlsc_surface, link);
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200876
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400877 if (es->visual == WLSC_RGB_VISUAL &&
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400878 output->prepare_scanout_surface(output, es) == 0) {
879 /* We're drawing nothing now,
880 * draw the damaged regions later. */
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400881 pixman_region32_union(&ec->damage, &ec->damage, &total_damage);
Benjamin Franzked72037c2011-06-21 16:33:27 +0200882
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400883 output->scanout_buffer = es->buffer;
884 output->scanout_buffer->busy_count++;
Benjamin Franzke06286262011-05-06 19:12:33 +0200885
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400886 wl_list_remove(&output->scanout_buffer_destroy_listener.link);
887 wl_list_insert(output->scanout_buffer->resource.destroy_listener_list.prev,
888 &output->scanout_buffer_destroy_listener.link);
Benjamin Franzke06286262011-05-06 19:12:33 +0200889
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400890 return;
Kristian Høgsberg4d07a1c2011-05-06 14:03:12 -0400891 }
892
893 if (es->fullscreen_output == output) {
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400894 if (es->width < output->current->width ||
895 es->height < output->current->height)
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200896 glClear(GL_COLOR_BUFFER_BIT);
897 wlsc_surface_draw(es, output, &total_damage);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500898 } else {
Kristian Høgsbergaee7f842011-06-23 21:25:20 -0400899 wl_list_for_each(es, &ec->surface_list, link) {
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400900 pixman_region32_copy(&es->damage, &total_damage);
901 pixman_region32_subtract(&total_damage, &total_damage, &es->opaque);
Kristian Høgsbergaee7f842011-06-23 21:25:20 -0400902 }
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500903
Kristian Høgsbergaee7f842011-06-23 21:25:20 -0400904 wl_list_for_each_reverse(es, &ec->surface_list, link) {
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -0400905 pixman_region32_init(&repaint);
906 pixman_region32_intersect(&repaint, &output->region,
907 &es->damage);
908 wlsc_surface_draw(es, output, &repaint);
909 pixman_region32_subtract(&es->damage,
910 &es->damage, &output->region);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500911 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500912 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400913
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400914 if (ec->fade.spring.current > 0.001)
915 fade_output(output, ec->fade.spring.current, &total_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500916}
917
Kristian Høgsberg33418202011-08-16 23:01:28 -0400918struct wlsc_frame_callback {
919 struct wl_resource resource;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400920 struct wl_list link;
921};
922
Kristian Høgsbergef044142011-06-21 15:02:12 -0400923static void
924repaint(void *data, int msecs)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500925{
Kristian Høgsbergef044142011-06-21 15:02:12 -0400926 struct wlsc_output *output = data;
927 struct wlsc_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -0400928 struct wlsc_animation *animation, *next;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400929 struct wlsc_frame_callback *cb, *cnext;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500930
Kristian Høgsbergef044142011-06-21 15:02:12 -0400931 wlsc_output_repaint(output);
932 output->repaint_needed = 0;
933 output->repaint_scheduled = 1;
934 output->present(output);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100935
Kristian Høgsberg33418202011-08-16 23:01:28 -0400936 wl_list_for_each_safe(cb, cnext, &output->frame_callback_list, link) {
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400937 wl_resource_post_event(&cb->resource, WL_CALLBACK_DONE, msecs);
938 wl_resource_destroy(&cb->resource, 0);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500939 }
940
Kristian Høgsbergef044142011-06-21 15:02:12 -0400941 wl_list_for_each_safe(animation, next,
942 &compositor->animation_list, link)
943 animation->frame(animation, output, msecs);
944}
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400945
Kristian Høgsbergef044142011-06-21 15:02:12 -0400946static void
947idle_repaint(void *data)
948{
949 repaint(data, wlsc_compositor_get_time());
950}
951
952WL_EXPORT void
953wlsc_output_finish_frame(struct wlsc_output *output, int msecs)
954{
955 wlsc_buffer_post_release(output->scanout_buffer);
956 output->scanout_buffer = NULL;
957 output->repaint_scheduled = 0;
958
959 if (output->repaint_needed)
960 repaint(output, msecs);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400961}
962
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400963WL_EXPORT void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400964wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400965{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100966 struct wlsc_output *output;
Kristian Høgsbergef044142011-06-21 15:02:12 -0400967 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100968
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400969 if (compositor->state == WLSC_COMPOSITOR_SLEEPING)
970 return;
971
Kristian Høgsbergef044142011-06-21 15:02:12 -0400972 loop = wl_display_get_event_loop(compositor->wl_display);
973 wl_list_for_each(output, &compositor->output_list, link) {
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100974 output->repaint_needed = 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -0400975 if (output->repaint_scheduled)
976 continue;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100977
Kristian Høgsbergef044142011-06-21 15:02:12 -0400978 wl_event_loop_add_idle(loop, idle_repaint, output);
979 output->repaint_scheduled = 1;
980 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400981}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500982
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400983WL_EXPORT void
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400984wlsc_compositor_fade(struct wlsc_compositor *compositor, float tint)
985{
986 int done;
987
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400988 done = wlsc_spring_done(&compositor->fade.spring);
989 compositor->fade.spring.target = tint;
990 if (wlsc_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400991 return;
992
993 if (done)
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400994 compositor->fade.spring.timestamp =
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400995 wlsc_compositor_get_time();
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400996
997 wlsc_compositor_damage_all(compositor);
998 if (wl_list_empty(&compositor->fade.animation.link))
999 wl_list_insert(compositor->animation_list.prev,
1000 &compositor->fade.animation.link);
1001}
1002
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001003static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001004surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001005{
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001006 wl_resource_destroy(resource, wlsc_compositor_get_time());
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001007}
1008
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001009WL_EXPORT void
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001010wlsc_surface_assign_output(struct wlsc_surface *es)
1011{
1012 struct wlsc_compositor *ec = es->compositor;
1013 struct wlsc_output *output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001014 pixman_region32_t region;
1015 uint32_t max, area;
1016 pixman_box32_t *e;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001017
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001018 max = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001019 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001020 pixman_region32_init_rect(&region,
1021 es->x, es->y, es->width, es->height);
1022 pixman_region32_intersect(&region, &region, &output->region);
1023
1024 e = pixman_region32_extents(&region);
1025 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1026
1027 if (area >= max) {
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001028 es->output = output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001029 max = area;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001030 }
1031 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001032}
1033
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001034static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001035surface_attach(struct wl_client *client,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001036 struct wl_resource *resource,
1037 struct wl_resource *buffer_resource, int32_t x, int32_t y)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001038{
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001039 struct wlsc_surface *es = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001040 struct wl_buffer *buffer = buffer_resource->data;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001041
Benjamin Franzke06286262011-05-06 19:12:33 +02001042 buffer->busy_count++;
Benjamin Franzke6f5fc692011-06-21 19:34:19 +02001043 wlsc_buffer_post_release(es->buffer);
Benjamin Franzke06286262011-05-06 19:12:33 +02001044
1045 es->buffer = buffer;
1046 wl_list_remove(&es->buffer_destroy_listener.link);
1047 wl_list_insert(es->buffer->resource.destroy_listener_list.prev,
1048 &es->buffer_destroy_listener.link);
1049
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001050 if (es->visual == WLSC_NONE_VISUAL)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001051 wl_list_insert(&es->compositor->surface_list, &es->link);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001052
Kristian Høgsberg9ffb6b92011-07-21 11:33:47 -07001053 if (x != 0 || y != 0 ||
1054 es->width != buffer->width || es->height != buffer->height)
1055 wlsc_surface_configure(es, es->x + x, es->y + y,
1056 buffer->width, buffer->height);
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001057
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001058 wlsc_buffer_attach(buffer, &es->surface);
Kristian Høgsberg2e02d242011-05-13 14:06:29 -04001059
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001060 es->compositor->shell->attach(es->compositor->shell, es);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001061}
1062
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001063static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001064surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001065 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001066 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001067{
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001068 struct wlsc_surface *es = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001069
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001070 wlsc_surface_damage_rectangle(es, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001071}
1072
Kristian Høgsberg33418202011-08-16 23:01:28 -04001073static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001074destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001075{
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001076 struct wlsc_frame_callback *cb = resource->data;
1077
1078 wl_list_remove(&cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001079 free(resource);
1080}
1081
1082static void
1083surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001084 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001085{
1086 struct wlsc_frame_callback *cb;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001087 struct wlsc_surface *es = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001088
1089 cb = malloc(sizeof *cb);
1090 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001091 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001092 return;
1093 }
1094
1095 cb->resource.object.interface = &wl_callback_interface;
1096 cb->resource.object.id = callback;
1097 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001098 cb->resource.client = client;
1099 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001100
1101 wl_client_add_resource(client, &cb->resource);
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -04001102
1103 if (es->output) {
1104 wl_list_insert(es->output->frame_callback_list.prev,
1105 &cb->link);
1106 } else {
1107 wl_list_init(&cb->link);
1108 wl_resource_destroy(&cb->resource, 0);
1109 }
Kristian Høgsberg33418202011-08-16 23:01:28 -04001110}
1111
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001112const static struct wl_surface_interface surface_interface = {
1113 surface_destroy,
1114 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001115 surface_damage,
1116 surface_frame
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001117};
1118
1119static void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001120wlsc_input_device_attach(struct wlsc_input_device *device,
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001121 int x, int y, int width, int height)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001122{
Kristian Høgsbergb8a98332011-06-23 21:00:04 -04001123 wlsc_surface_damage_below(device->sprite);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001124
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001125 device->hotspot_x = x;
1126 device->hotspot_y = y;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001127
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001128 device->sprite->x = device->input_device.x - device->hotspot_x;
1129 device->sprite->y = device->input_device.y - device->hotspot_y;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001130 device->sprite->width = width;
1131 device->sprite->height = height;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001132
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001133 wlsc_surface_damage(device->sprite);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001134}
1135
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001136static void
1137wlsc_input_device_attach_buffer(struct wlsc_input_device *device,
1138 struct wl_buffer *buffer, int x, int y)
1139{
1140 wlsc_buffer_attach(buffer, &device->sprite->surface);
1141 wlsc_input_device_attach(device, x, y, buffer->width, buffer->height);
1142}
1143
1144static void
1145wlsc_input_device_attach_sprite(struct wlsc_input_device *device,
1146 struct wlsc_sprite *sprite, int x, int y)
1147{
1148 wlsc_sprite_attach(sprite, &device->sprite->surface);
1149 wlsc_input_device_attach(device, x, y, sprite->width, sprite->height);
1150}
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001151
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001152WL_EXPORT void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001153wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
1154 enum wlsc_pointer_type type)
1155{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001156 struct wlsc_compositor *compositor =
1157 (struct wlsc_compositor *) device->input_device.compositor;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001158
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001159 wlsc_input_device_attach_sprite(device,
1160 compositor->pointer_sprites[type],
1161 pointer_images[type].hotspot_x,
1162 pointer_images[type].hotspot_y);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001163}
1164
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001165static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001166compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001167 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001168{
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001169 struct wlsc_compositor *ec = resource->data;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001170 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001171
Kristian Høgsbergc5d6be92011-01-14 16:22:37 -05001172 surface = wlsc_surface_create(ec, 0, 0, 0, 0);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001173 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001174 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001175 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001176 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001177
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001178 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001179
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001180 surface->surface.resource.object.id = id;
1181 surface->surface.resource.object.interface = &wl_surface_interface;
1182 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001183 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001184 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001185
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001186 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001187}
1188
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001189const static struct wl_compositor_interface compositor_interface = {
1190 compositor_create_surface,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001191};
1192
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001193static void
1194wlsc_surface_transform(struct wlsc_surface *surface,
1195 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1196{
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001197 *sx = x - surface->x;
1198 *sy = y - surface->y;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001199}
1200
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001201WL_EXPORT struct wlsc_surface *
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001202pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001203{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001204 struct wlsc_compositor *ec =
1205 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001206 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001207
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001208 wl_list_for_each(es, &ec->surface_list, link) {
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001209 if (es->surface.resource.client == NULL)
Kristian Høgsberg8244b442011-06-23 15:44:14 -04001210 continue;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001211 wlsc_surface_transform(es, device->x, device->y, sx, sy);
1212 if (0 <= *sx && *sx < es->width &&
1213 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001214 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001215 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001216
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001217 return NULL;
1218}
1219
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001220
1221static void
1222motion_grab_motion(struct wl_grab *grab,
1223 uint32_t time, int32_t x, int32_t y)
1224{
1225 struct wlsc_input_device *device =
1226 (struct wlsc_input_device *) grab->input_device;
1227 struct wlsc_surface *es =
1228 (struct wlsc_surface *) device->input_device.pointer_focus;
1229 int32_t sx, sy;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001230 struct wl_resource *resource;
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001231
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001232 resource = grab->input_device->pointer_focus_resource;
1233 if (resource) {
1234 wlsc_surface_transform(es, x, y, &sx, &sy);
1235 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
1236 time, x, y, sx, sy);
1237 }
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001238}
1239
1240static void
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001241motion_grab_button(struct wl_grab *grab,
1242 uint32_t time, int32_t button, int32_t state)
1243{
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001244 struct wl_resource *resource;
1245
1246 resource = grab->input_device->pointer_focus_resource;
1247 if (resource)
1248 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
1249 time, button, state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001250}
1251
1252static void
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001253motion_grab_end(struct wl_grab *grab, uint32_t time)
1254{
1255}
1256
1257static const struct wl_grab_interface motion_grab_interface = {
1258 motion_grab_motion,
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001259 motion_grab_button,
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001260 motion_grab_end
1261};
1262
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001263WL_EXPORT void
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001264wlsc_compositor_wake(struct wlsc_compositor *compositor)
1265{
1266 if (compositor->idle_inhibit)
1267 return;
1268
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001269 wlsc_compositor_fade(compositor, 0.0);
1270 compositor->state = WLSC_COMPOSITOR_ACTIVE;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001271
1272 wl_event_source_timer_update(compositor->idle_source,
1273 option_idle_time * 1000);
1274}
1275
1276static void
1277wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor)
1278{
1279 wlsc_compositor_wake(compositor);
1280 compositor->idle_inhibit++;
1281}
1282
1283static void
1284wlsc_compositor_idle_release(struct wlsc_compositor *compositor)
1285{
1286 compositor->idle_inhibit--;
1287 wlsc_compositor_wake(compositor);
1288}
1289
1290static int
1291idle_handler(void *data)
1292{
1293 struct wlsc_compositor *compositor = data;
1294
1295 if (compositor->idle_inhibit)
1296 return 1;
1297
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001298 wlsc_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001299
1300 return 1;
1301}
1302
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001303WL_EXPORT void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001304notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001305{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001306 struct wlsc_surface *es;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001307 struct wlsc_compositor *ec =
1308 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001309 struct wlsc_output *output;
Kristian Høgsberg6d65d5f2010-12-07 13:30:18 -05001310 const struct wl_grab_interface *interface;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001311 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsbergfc9c28a2010-12-07 13:04:43 -05001312 int32_t sx, sy;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001313 int x_valid = 0, y_valid = 0;
1314 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 -05001315
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001316 wlsc_compositor_wake(ec);
1317
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001318 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001319 if (output->x <= x && x <= output->x + output->current->width)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001320 x_valid = 1;
1321
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001322 if (output->y <= y && y <= output->y + output->current->height)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001323 y_valid = 1;
1324
1325 /* FIXME: calculate this only on output addition/deletion */
1326 if (output->x < min_x)
1327 min_x = output->x;
1328 if (output->y < min_y)
1329 min_y = output->y;
1330
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001331 if (output->x + output->current->width > max_x)
1332 max_x = output->x + output->current->width;
1333 if (output->y + output->current->height > max_y)
1334 max_y = output->y + output->current->height;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001335 }
1336
1337 if (!x_valid) {
1338 if (x < min_x)
1339 x = min_x;
1340 else if (x >= max_x)
1341 x = max_x;
1342 }
1343 if (!y_valid) {
1344 if (y < min_y)
1345 y = min_y;
1346 else if (y >= max_y)
1347 y = max_y;
1348 }
Ray Strode90e701d2008-12-18 23:05:43 -05001349
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -05001350 device->x = x;
1351 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001352
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001353 if (device->grab) {
1354 interface = device->grab->interface;
1355 interface->motion(device->grab, time, x, y);
1356 } else {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001357 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001358 wl_input_device_set_pointer_focus(device,
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001359 &es->surface,
Kristian Høgsberg26437072010-12-01 10:17:47 -05001360 time, x, y, sx, sy);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001361 if (device->pointer_focus_resource)
1362 wl_resource_post_event(device->pointer_focus_resource,
1363 WL_INPUT_DEVICE_MOTION,
1364 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001365 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001366
Kristian Høgsbergb8a98332011-06-23 21:00:04 -04001367 wlsc_surface_damage_below(wd->sprite);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001368
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001369 wd->sprite->x = device->x - wd->hotspot_x;
1370 wd->sprite->y = device->y - wd->hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001371
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001372 wlsc_surface_damage(wd->sprite);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001373}
1374
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001375WL_EXPORT void
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001376wlsc_surface_activate(struct wlsc_surface *surface,
1377 struct wlsc_input_device *device, uint32_t time)
1378{
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001379 struct wlsc_shell *shell = surface->compositor->shell;
1380
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001381 wlsc_surface_raise(surface);
1382 if (device->selection)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001383 shell->set_selection_focus(shell,
1384 device->selection,
1385 &surface->surface, time);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001386
1387 wl_input_device_set_keyboard_focus(&device->input_device,
1388 &surface->surface,
1389 time);
1390}
1391
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001392struct wlsc_binding {
1393 uint32_t key;
1394 uint32_t button;
1395 uint32_t modifier;
1396 wlsc_binding_handler_t handler;
1397 void *data;
1398 struct wl_list link;
1399};
1400
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001401WL_EXPORT void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001402notify_button(struct wl_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001403 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001404{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001405 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001406 struct wlsc_compositor *compositor =
1407 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001408 struct wlsc_binding *b;
1409 struct wlsc_surface *surface =
1410 (struct wlsc_surface *) device->pointer_focus;
Kristian Høgsbergea081152010-12-07 08:59:51 -05001411
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001412 if (state)
1413 wlsc_compositor_idle_inhibit(compositor);
1414 else
1415 wlsc_compositor_idle_release(compositor);
1416
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -05001417 if (state && surface && device->grab == NULL) {
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001418 wlsc_surface_activate(surface, wd, time);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001419 wl_input_device_start_grab(device,
1420 &device->motion_grab,
1421 button, time);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001422 }
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -05001423
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001424 wl_list_for_each(b, &compositor->binding_list, link) {
1425 if (b->button == button &&
1426 b->modifier == wd->modifier_state && state) {
1427 b->handler(&wd->input_device,
1428 time, 0, button, state, b->data);
1429 break;
1430 }
Benjamin Franzke5a2218a2011-02-01 16:30:31 +01001431 }
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001432
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -05001433 if (device->grab)
1434 device->grab->interface->button(device->grab, time,
1435 button, state);
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -05001436
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001437 if (!state && device->grab && device->grab_button == button)
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001438 wl_input_device_end_grab(device, time);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001439}
1440
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001441static void
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001442terminate_binding(struct wl_input_device *device, uint32_t time,
1443 uint32_t key, uint32_t button, uint32_t state, void *data)
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001444{
1445 struct wlsc_compositor *compositor = data;
1446
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001447 if (state)
1448 wl_display_terminate(compositor->wl_display);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001449}
1450
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001451WL_EXPORT struct wlsc_binding *
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001452wlsc_compositor_add_binding(struct wlsc_compositor *compositor,
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001453 uint32_t key, uint32_t button, uint32_t modifier,
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001454 wlsc_binding_handler_t handler, void *data)
1455{
1456 struct wlsc_binding *binding;
1457
1458 binding = malloc(sizeof *binding);
1459 if (binding == NULL)
1460 return NULL;
1461
1462 binding->key = key;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001463 binding->button = button;
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001464 binding->modifier = modifier;
1465 binding->handler = handler;
1466 binding->data = data;
1467 wl_list_insert(compositor->binding_list.prev, &binding->link);
1468
1469 return binding;
1470}
1471
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001472WL_EXPORT void
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001473wlsc_binding_destroy(struct wlsc_binding *binding)
1474{
1475 wl_list_remove(&binding->link);
1476 free(binding);
1477}
1478
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001479static void
1480update_modifier_state(struct wlsc_input_device *device,
1481 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001482{
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001483 uint32_t modifier;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001484
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001485 switch (key) {
1486 case KEY_LEFTCTRL:
1487 case KEY_RIGHTCTRL:
1488 modifier = MODIFIER_CTRL;
1489 break;
1490
1491 case KEY_LEFTALT:
1492 case KEY_RIGHTALT:
1493 modifier = MODIFIER_ALT;
1494 break;
1495
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -04001496 case KEY_LEFTMETA:
1497 case KEY_RIGHTMETA:
1498 modifier = MODIFIER_SUPER;
1499 break;
1500
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001501 default:
1502 modifier = 0;
1503 break;
1504 }
1505
1506 if (state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001507 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001508 else
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001509 device->modifier_state &= ~modifier;
1510}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001511
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001512WL_EXPORT void
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001513notify_key(struct wl_input_device *device,
1514 uint32_t time, uint32_t key, uint32_t state)
1515{
1516 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1517 struct wlsc_compositor *compositor =
1518 (struct wlsc_compositor *) device->compositor;
1519 uint32_t *k, *end;
1520 struct wlsc_binding *b;
1521
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001522 if (state)
1523 wlsc_compositor_idle_inhibit(compositor);
1524 else
1525 wlsc_compositor_idle_release(compositor);
1526
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001527 wl_list_for_each(b, &compositor->binding_list, link) {
1528 if (b->key == key &&
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001529 b->modifier == wd->modifier_state) {
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001530 b->handler(&wd->input_device,
1531 time, key, 0, state, b->data);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001532 break;
1533 }
1534 }
1535
1536 update_modifier_state(wd, key, state);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001537 end = device->keys.data + device->keys.size;
1538 for (k = device->keys.data; k < end; k++) {
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001539 if (*k == key)
1540 *k = *--end;
1541 }
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001542 device->keys.size = (void *) end - device->keys.data;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001543 if (state) {
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001544 k = wl_array_add(&device->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001545 *k = key;
1546 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001547
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001548 if (device->keyboard_focus_resource)
1549 wl_resource_post_event(device->keyboard_focus_resource,
1550 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001551}
1552
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001553WL_EXPORT void
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001554notify_pointer_focus(struct wl_input_device *device,
1555 uint32_t time, struct wlsc_output *output,
1556 int32_t x, int32_t y)
1557{
1558 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1559 struct wlsc_compositor *compositor =
1560 (struct wlsc_compositor *) device->compositor;
1561 struct wlsc_surface *es;
1562 int32_t sx, sy;
1563
1564 if (output) {
1565 device->x = x;
1566 device->y = y;
1567 es = pick_surface(device, &sx, &sy);
1568 wl_input_device_set_pointer_focus(device,
1569 &es->surface,
1570 time, x, y, sx, sy);
1571
1572 compositor->focus = 1;
1573
1574 wd->sprite->x = device->x - wd->hotspot_x;
1575 wd->sprite->y = device->y - wd->hotspot_y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001576 } else {
1577 wl_input_device_set_pointer_focus(device, NULL,
1578 time, 0, 0, 0, 0);
1579 compositor->focus = 0;
1580 }
1581
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001582 wlsc_surface_damage(wd->sprite);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001583}
1584
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001585WL_EXPORT void
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001586notify_keyboard_focus(struct wl_input_device *device,
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001587 uint32_t time, struct wlsc_output *output,
1588 struct wl_array *keys)
1589{
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001590 struct wlsc_input_device *wd =
1591 (struct wlsc_input_device *) device;
1592 struct wlsc_compositor *compositor =
1593 (struct wlsc_compositor *) device->compositor;
1594 struct wlsc_surface *es;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001595 uint32_t *k, *end;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001596
1597 if (!wl_list_empty(&compositor->surface_list))
1598 es = container_of(compositor->surface_list.next,
1599 struct wlsc_surface, link);
1600 else
1601 es = NULL;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001602
1603 if (output) {
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001604 wl_array_copy(&wd->input_device.keys, keys);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001605 wd->modifier_state = 0;
1606 end = device->keys.data + device->keys.size;
1607 for (k = device->keys.data; k < end; k++) {
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001608 wlsc_compositor_idle_inhibit(compositor);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001609 update_modifier_state(wd, *k, 1);
1610 }
1611
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001612 if (es->surface.resource.client)
Benjamin Franzkeb7c00a42011-06-23 23:30:30 +02001613 wl_input_device_set_keyboard_focus(&wd->input_device,
1614 &es->surface, time);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001615 } else {
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001616 end = device->keys.data + device->keys.size;
1617 for (k = device->keys.data; k < end; k++)
1618 wlsc_compositor_idle_release(compositor);
1619
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001620 wd->modifier_state = 0;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001621 wl_input_device_set_keyboard_focus(&wd->input_device,
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001622 NULL, time);
1623 }
1624}
1625
1626
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001627static void
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001628input_device_attach(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001629 struct wl_resource *resource,
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001630 uint32_t time,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001631 struct wl_resource *buffer_resource, int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001632{
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001633 struct wlsc_input_device *device = resource->data;
Kristian Høgsberg57295eb2011-08-29 16:02:57 -04001634 struct wl_buffer *buffer;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001635
1636 if (time < device->input_device.pointer_focus_time)
1637 return;
1638 if (device->input_device.pointer_focus == NULL)
1639 return;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001640 if (device->input_device.pointer_focus->resource.client != client)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001641 return;
1642
Kristian Høgsberg57295eb2011-08-29 16:02:57 -04001643 if (buffer_resource) {
1644 buffer = buffer_resource->data;
1645 wlsc_input_device_attach_buffer(device, buffer, x, y);
1646 } else {
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001647 wlsc_input_device_set_pointer_image(device,
1648 WLSC_POINTER_LEFT_PTR);
1649 return;
1650 }
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001651}
1652
1653const static struct wl_input_device_interface input_device_interface = {
1654 input_device_attach,
1655};
1656
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001657static void
1658bind_input_device(struct wl_client *client,
1659 void *data, uint32_t version, uint32_t id)
1660{
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04001661 struct wl_input_device *device = data;
1662 struct wl_resource *resource;
1663
1664 resource = wl_client_add_object(client, &wl_input_device_interface,
1665 &input_device_interface, id, data);
1666 wl_list_insert(&device->resource_list, &resource->link);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001667}
1668
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001669WL_EXPORT void
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001670wlsc_input_device_init(struct wlsc_input_device *device,
1671 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001672{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001673 wl_input_device_init(&device->input_device, &ec->compositor);
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05001674
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001675 wl_display_add_global(ec->wl_display, &wl_input_device_interface,
1676 device, bind_input_device);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001677
Kristian Høgsbergc5d6be92011-01-14 16:22:37 -05001678 device->sprite = wlsc_surface_create(ec,
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001679 device->input_device.x,
1680 device->input_device.y, 32, 32);
Kristian Høgsberg8244b442011-06-23 15:44:14 -04001681 wl_list_insert(&ec->surface_list, &device->sprite->link);
1682
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001683 device->hotspot_x = 16;
1684 device->hotspot_y = 16;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001685 device->modifier_state = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001686
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001687 device->input_device.motion_grab.interface = &motion_grab_interface;
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001688
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001689 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001690
1691 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001692}
1693
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001694static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001695bind_output(struct wl_client *client,
1696 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001697{
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001698 struct wlsc_output *output = data;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001699 struct wlsc_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04001700 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001701
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04001702 resource = wl_client_add_object(client,
1703 &wl_output_interface, NULL, id, data);
1704
1705 wl_resource_post_event(resource,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001706 WL_OUTPUT_GEOMETRY,
1707 output->x,
1708 output->y,
1709 output->mm_width,
1710 output->mm_height,
1711 output->subpixel,
1712 output->make, output->model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001713
1714 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04001715 wl_resource_post_event(resource,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001716 WL_OUTPUT_MODE,
1717 mode->flags,
1718 mode->width,
1719 mode->height,
1720 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001721 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001722}
1723
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001724static const char vertex_shader[] =
1725 "uniform mat4 proj;\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001726 "attribute vec2 position;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001727 "attribute vec2 texcoord;\n"
1728 "varying vec2 v_texcoord;\n"
1729 "void main()\n"
1730 "{\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001731 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001732 " v_texcoord = texcoord;\n"
1733 "}\n";
1734
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001735static const char texture_fragment_shader[] =
Kristian Høgsbergdfce71d2010-12-07 20:19:10 -05001736 "precision mediump float;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001737 "varying vec2 v_texcoord;\n"
1738 "uniform sampler2D tex;\n"
1739 "void main()\n"
1740 "{\n"
1741 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1742 "}\n";
1743
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001744static const char solid_fragment_shader[] =
1745 "precision mediump float;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001746 "uniform vec4 color;\n"
1747 "void main()\n"
1748 "{\n"
1749 " gl_FragColor = color\n;"
1750 "}\n";
1751
Kristian Høgsberga9468212010-06-14 21:03:11 -04001752static int
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001753compile_shader(GLenum type, const char *source)
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001754{
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001755 GLuint s;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001756 char msg[512];
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001757 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001758
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001759 s = glCreateShader(type);
1760 glShaderSource(s, 1, &source, NULL);
1761 glCompileShader(s);
1762 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001763 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001764 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1765 fprintf(stderr, "shader info: %s\n", msg);
1766 return GL_NONE;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001767 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001768
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001769 return s;
1770}
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001771
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001772static int
1773wlsc_shader_init(struct wlsc_shader *shader,
1774 const char *vertex_source, const char *fragment_source)
1775{
1776 char msg[512];
1777 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001778
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001779 shader->vertex_shader =
1780 compile_shader(GL_VERTEX_SHADER, vertex_source);
1781 shader->fragment_shader =
1782 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1783
1784 shader->program = glCreateProgram();
1785 glAttachShader(shader->program, shader->vertex_shader);
1786 glAttachShader(shader->program, shader->fragment_shader);
1787 glBindAttribLocation(shader->program, 0, "position");
1788 glBindAttribLocation(shader->program, 1, "texcoord");
1789
1790 glLinkProgram(shader->program);
1791 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001792 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001793 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001794 fprintf(stderr, "link info: %s\n", msg);
1795 return -1;
1796 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001797
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001798 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1799 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001800
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001801 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001802}
1803
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001804static int
1805init_solid_shader(struct wlsc_shader *shader,
1806 GLuint vertex_shader, const char *fragment_source)
1807{
1808 GLint status;
1809 char msg[512];
1810
1811 shader->vertex_shader = vertex_shader;
1812 shader->fragment_shader =
1813 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1814
1815 shader->program = glCreateProgram();
1816 glAttachShader(shader->program, shader->vertex_shader);
1817 glAttachShader(shader->program, shader->fragment_shader);
1818 glBindAttribLocation(shader->program, 0, "position");
1819 glBindAttribLocation(shader->program, 1, "texcoord");
1820
1821 glLinkProgram(shader->program);
1822 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1823 if (!status) {
1824 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1825 fprintf(stderr, "link info: %s\n", msg);
1826 return -1;
1827 }
1828
1829 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1830 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1831
1832 return 0;
1833}
1834
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001835WL_EXPORT void
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001836wlsc_output_destroy(struct wlsc_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001837{
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04001838 pixman_region32_fini(&output->region);
Kristian Høgsberg8b72f602011-06-23 20:46:34 -04001839 pixman_region32_fini(&output->previous_damage);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001840 destroy_surface(&output->background->surface.resource);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001841}
1842
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001843WL_EXPORT void
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001844wlsc_output_move(struct wlsc_output *output, int x, int y)
1845{
1846 struct wlsc_compositor *c = output->compositor;
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01001847 int flip;
1848
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001849 output->x = x;
1850 output->y = y;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001851
Benjamin Franzke264b3f92011-03-16 13:48:42 +01001852 if (output->background) {
1853 output->background->x = x;
1854 output->background->y = y;
Benjamin Franzke264b3f92011-03-16 13:48:42 +01001855 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001856
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øgsbergce5325d2010-06-14 11:54:00 -04001862 wlsc_matrix_init(&output->matrix);
1863 wlsc_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øgsbergce5325d2010-06-14 11:54:00 -04001868 wlsc_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øgsberg20300ba2011-06-23 20:29:12 -04001872 pixman_region32_union(&c->damage, &c->damage, &output->region);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001873}
1874
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001875WL_EXPORT void
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001876wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1877 int x, int y, int width, int height, uint32_t flags)
1878{
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
1885 output->background =
1886 background_create(output, option_background);
Kristian Høgsberg758dd3e2011-06-23 15:51:43 -04001887
1888 if (output->background != NULL)
1889 wl_list_insert(c->surface_list.prev,
1890 &output->background->link);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001891
1892 output->flags = flags;
1893 wlsc_output_move(output, x, y);
1894
Benjamin Franzke06286262011-05-06 19:12:33 +02001895 output->scanout_buffer_destroy_listener.func =
1896 output_handle_scanout_buffer_destroy;
1897 wl_list_init(&output->scanout_buffer_destroy_listener.link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001898 wl_list_init(&output->frame_callback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02001899
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001900 output->resource.object.interface = &wl_output_interface;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001901 wl_display_add_global(c->wl_display,
1902 &wl_output_interface, output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001903}
1904
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001905static void
1906shm_buffer_created(struct wl_buffer *buffer)
1907{
1908 struct wl_list *surfaces_attached_to;
1909
1910 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1911 if (!surfaces_attached_to) {
1912 buffer->user_data = NULL;
1913 return;
1914 }
1915
1916 wl_list_init(surfaces_attached_to);
1917
1918 buffer->user_data = surfaces_attached_to;
1919}
1920
1921static void
1922shm_buffer_damaged(struct wl_buffer *buffer,
1923 int32_t x, int32_t y, int32_t width, int32_t height)
1924{
1925 struct wl_list *surfaces_attached_to = buffer->user_data;
1926 struct wlsc_surface *es;
Benjamin Franzkefab5ec12011-04-25 19:44:47 +02001927 GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001928
1929 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1930 glBindTexture(GL_TEXTURE_2D, es->texture);
1931 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Benjamin Franzkefab5ec12011-04-25 19:44:47 +02001932 tex_width, buffer->height, 0,
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001933 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1934 wl_shm_buffer_get_data(buffer));
1935 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
1936 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
1937 }
1938}
1939
1940static void
1941shm_buffer_destroyed(struct wl_buffer *buffer)
1942{
1943 struct wl_list *surfaces_attached_to = buffer->user_data;
1944 struct wlsc_surface *es, *next;
1945
1946 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001947 wl_list_remove(&es->buffer_link);
Kristian Høgsbergfac11d22011-05-02 13:35:17 -04001948 wl_list_init(&es->buffer_link);
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001949 }
1950
1951 free(surfaces_attached_to);
1952}
1953
1954const static struct wl_shm_callbacks shm_callbacks = {
1955 shm_buffer_created,
1956 shm_buffer_damaged,
1957 shm_buffer_destroyed
1958};
1959
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001960WL_EXPORT int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001961wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1962{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001963 struct wl_event_loop *loop;
Kristian Høgsbergd711d0c2011-01-14 17:28:21 -05001964 const char *extensions;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001965
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001966 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001967
Kristian Høgsbergc5c510e2010-12-08 15:12:58 -05001968 wl_compositor_init(&ec->compositor, &compositor_interface, display);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001969
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001970 ec->shm = wl_shm_init(display, &shm_callbacks);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04001971
1972 ec->image_target_texture_2d =
1973 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1974 ec->image_target_renderbuffer_storage = (void *)
1975 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1976 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1977 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1978 ec->bind_display =
1979 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1980 ec->unbind_display =
1981 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1982
1983 extensions = (const char *) glGetString(GL_EXTENSIONS);
1984 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1985 fprintf(stderr,
1986 "GL_EXT_texture_format_BGRA8888 not available\n");
1987 return -1;
1988 }
1989
1990 extensions =
1991 (const char *) eglQueryString(ec->display, EGL_EXTENSIONS);
1992 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1993 ec->has_bind_display = 1;
1994 if (ec->has_bind_display)
1995 ec->bind_display(ec->display, ec->wl_display);
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04001996
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001997 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001998 wl_list_init(&ec->input_device_list);
1999 wl_list_init(&ec->output_list);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04002000 wl_list_init(&ec->binding_list);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002001 wl_list_init(&ec->animation_list);
Kristian Høgsberg269c7822011-05-02 14:38:18 -04002002 wlsc_spring_init(&ec->fade.spring, 0.8, 0.0, 0.0);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002003 ec->fade.animation.frame = fade_frame;
2004 wl_list_init(&ec->fade.animation.link);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04002005
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04002006 wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0,
Kristian Høgsberg3555d092011-04-11 13:58:13 -04002007 MODIFIER_CTRL | MODIFIER_ALT,
2008 terminate_binding, ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002009
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04002010 create_pointer_images(ec);
2011
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002012 screenshooter_create(ec);
2013
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002014 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002015
2016 if (wlsc_shader_init(&ec->texture_shader,
2017 vertex_shader, texture_fragment_shader) < 0)
Kristian Høgsberga9468212010-06-14 21:03:11 -04002018 return -1;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002019 if (init_solid_shader(&ec->solid_shader,
2020 ec->texture_shader.vertex_shader,
2021 solid_fragment_shader) < 0)
2022 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05002023
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002024 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002025 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2026 wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000);
2027
Kristian Høgsberg20300ba2011-06-23 20:29:12 -04002028 pixman_region32_init(&ec->damage);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04002029 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002030
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002031 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002032}
2033
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002034WL_EXPORT void
Matt Roper361d2ad2011-08-29 13:52:23 -07002035wlsc_compositor_shutdown(struct wlsc_compositor *ec)
2036{
2037 struct wlsc_output *output;
2038
2039 /* Destroy all outputs associated with this compositor */
2040 wl_list_for_each(output, &ec->output_list, link)
2041 output->destroy(output);
2042}
2043
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002044static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002045{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002046 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002047
Kristian Høgsberg3cad4362011-07-15 21:09:24 -04002048 fprintf(stderr, "caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002049 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002050
2051 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002052}
2053
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002054static void *
2055load_module(const char *name, const char *entrypoint, void **handle)
2056{
2057 char path[PATH_MAX];
2058 void *module, *init;
2059
2060 if (name[0] != '/')
2061 snprintf(path, sizeof path, MODULEDIR "/%s", name);
2062 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02002063 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002064
2065 module = dlopen(path, RTLD_LAZY);
2066 if (!module) {
2067 fprintf(stderr,
2068 "failed to load module: %s\n", dlerror());
2069 return NULL;
2070 }
2071
2072 init = dlsym(module, entrypoint);
2073 if (!init) {
2074 fprintf(stderr,
2075 "failed to lookup init function: %s\n", dlerror());
2076 return NULL;
2077 }
2078
2079 return init;
2080}
2081
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002082int main(int argc, char *argv[])
2083{
2084 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05002085 struct wlsc_compositor *ec;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002086 struct wl_event_loop *loop;
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002087 int o, xserver = 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002088 void *shell_module, *backend_module;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002089 int (*shell_init)(struct wlsc_compositor *ec);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002090 struct wlsc_compositor
2091 *(*backend_init)(struct wl_display *display, char *options);
2092 char *backend = NULL;
2093 char *backend_options = "";
2094 char *shell = NULL;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002095 char *p;
2096
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002097 static const char opts[] = "B:b:o:S:i:s:x";
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002098 static const struct option longopts[ ] = {
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002099 { "backend", 1, NULL, 'B' },
2100 { "backend-options", 1, NULL, 'o' },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002101 { "background", 1, NULL, 'b' },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002102 { "socket", 1, NULL, 'S' },
2103 { "idle-time", 1, NULL, 'i' },
2104 { "shell", 1, NULL, 's' },
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002105 { "xserver", 0, NULL, 'x' },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002106 { NULL, }
2107 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05002108
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002109 while (o = getopt_long(argc, argv, opts, longopts, &o), o > 0) {
2110 switch (o) {
2111 case 'b':
2112 option_background = optarg;
2113 break;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002114 case 'B':
2115 backend = optarg;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002116 break;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002117 case 'o':
2118 backend_options = optarg;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002119 break;
2120 case 'S':
2121 option_socket_name = optarg;
2122 break;
2123 case 'i':
2124 option_idle_time = strtol(optarg, &p, 0);
2125 if (*p != '\0') {
2126 fprintf(stderr,
2127 "invalid idle time option: %s\n",
2128 optarg);
2129 exit(EXIT_FAILURE);
2130 }
2131 break;
2132 case 's':
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002133 shell = optarg;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002134 break;
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002135 case 'x':
2136 xserver = 1;
2137 break;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04002138 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04002139 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002140
2141 display = wl_display_create();
2142
Tiago Vignatti2116b892011-08-08 05:52:59 -07002143 loop = wl_display_get_event_loop(display);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002144 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, display);
2145 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, display);
2146 wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal, display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07002147
2148 wl_list_init(&child_process_list);
2149 wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler, NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05002150
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002151 if (!backend) {
2152 if (getenv("WAYLAND_DISPLAY"))
2153 backend = "wayland-backend.so";
2154 else if (getenv("DISPLAY"))
2155 backend = "x11-backend.so";
2156 else if (getenv("OPENWFD"))
2157 backend = "openwfd-backend.so";
2158 else
2159 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002160 }
2161
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002162 if (!shell)
2163 shell = "desktop-shell.so";
Kristian Høgsberga9410222011-01-14 17:22:35 -05002164
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002165 backend_init = load_module(backend, "backend_init", &backend_module);
2166 if (!backend_init)
2167 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05002168
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002169 shell_init = load_module(shell, "shell_init", &shell_module);
2170 if (!shell_init)
2171 exit(EXIT_FAILURE);
Benjamin Franzke5d007092011-04-04 00:30:25 +02002172
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002173 ec = backend_init(display, backend_options);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05002174 if (ec == NULL) {
2175 fprintf(stderr, "failed to create compositor\n");
2176 exit(EXIT_FAILURE);
2177 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04002178
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002179 if (shell_init(ec) < 0)
2180 exit(EXIT_FAILURE);
2181
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002182 if (xserver)
Kristian Høgsberg3cad4362011-07-15 21:09:24 -04002183 wlsc_xserver_init(ec);
Kristian Høgsberg27da5382011-06-21 17:32:25 -04002184
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002185 if (wl_display_add_socket(display, option_socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002186 fprintf(stderr, "failed to add socket: %m\n");
2187 exit(EXIT_FAILURE);
2188 }
2189
2190 wl_display_run(display);
2191
Kristian Høgsberg3cad4362011-07-15 21:09:24 -04002192 if (xserver)
2193 wlsc_xserver_destroy(ec);
2194
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002195 if (ec->has_bind_display)
2196 ec->unbind_display(ec->display, display);
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002197 wl_display_destroy(display);
2198
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002199 ec->destroy(ec);
2200
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002201 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002202}