blob: 4cd50090ee549b19bab164917bea34a984acc20a [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>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040029#include <sys/ioctl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040030#include <fcntl.h>
31#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050032#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040033#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040034#include <dlfcn.h>
Kristian Høgsbergd34912c2011-05-02 10:36:04 -040035#include <getopt.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040036#include <signal.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050037
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038#include "wayland-server.h"
Kristian Høgsberg82863022010-06-04 21:52:02 -040039#include "compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050040
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010041/* The plan here is to generate a random anonymous socket name and
42 * advertise that through a service on the session dbus.
43 */
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -050044static const char *option_socket_name = NULL;
Kristian Høgsberg61a82512010-10-26 11:26:44 -040045static const char *option_background = "background.jpg";
Kristian Høgsbergd34912c2011-05-02 10:36:04 -040046static int option_idle_time = 300;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050047
Kristian Høgsbergd34912c2011-05-02 10:36:04 -040048WL_EXPORT void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050049wlsc_matrix_init(struct wlsc_matrix *matrix)
50{
51 static const struct wlsc_matrix identity = {
52 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
53 };
54
55 memcpy(matrix, &identity, sizeof identity);
56}
57
58static void
59wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
60{
61 struct wlsc_matrix tmp;
Kristian Høgsberg27803c62010-06-06 22:23:21 -040062 const GLfloat *row, *column;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050063 div_t d;
64 int i, j;
65
66 for (i = 0; i < 16; i++) {
67 tmp.d[i] = 0;
68 d = div(i, 4);
69 row = m->d + d.quot * 4;
70 column = n->d + d.rem;
71 for (j = 0; j < 4; j++)
72 tmp.d[i] += row[j] * column[j * 4];
73 }
74 memcpy(m, &tmp, sizeof tmp);
75}
76
Kristian Høgsbergd880e142011-05-02 13:53:45 -040077WL_EXPORT void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040078wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050079{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -050080 struct wlsc_matrix translate = {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050081 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
82 };
83
84 wlsc_matrix_multiply(matrix, &translate);
85}
86
Kristian Høgsbergd880e142011-05-02 13:53:45 -040087WL_EXPORT void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040088wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050089{
90 struct wlsc_matrix scale = {
91 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
92 };
93
94 wlsc_matrix_multiply(matrix, &scale);
95}
96
97static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040098wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
99{
100 int i, j;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400101 struct wlsc_vector t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400102
103 for (i = 0; i < 4; i++) {
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400104 t.f[i] = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400105 for (j = 0; j < 4; j++)
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400106 t.f[i] += v->f[j] * matrix->d[i + j * 4];
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400107 }
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500108
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400109 *v = t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400110}
111
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400112WL_EXPORT void
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400113wlsc_spring_init(struct wlsc_spring *spring,
114 double k, double current, double target)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400115{
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400116 spring->k = k;
117 spring->friction = 100.0;
118 spring->current = current;
119 spring->previous = current;
120 spring->target = target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400121}
122
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400123WL_EXPORT void
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400124wlsc_spring_update(struct wlsc_spring *spring, uint32_t msec)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400125{
Kristian Høgsberg4a9be132011-05-02 13:38:03 -0400126 double force, v, current, step;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400127
Kristian Høgsberg52612f12011-05-19 11:55:50 -0400128 step = (msec - spring->timestamp) / 300.0;
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400129 spring->timestamp = msec;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400130
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400131 current = spring->current;
132 v = current - spring->previous;
133 force = spring->k * (spring->target - current) / 10.0 +
134 (spring->previous - current) - v * spring->friction;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400135
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400136 spring->current =
137 current + (current - spring->previous) + force * step * step;
138 spring->previous = current;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400139
Kristian Høgsberg52612f12011-05-19 11:55:50 -0400140#if 0
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400141 if (spring->current >= 1.0) {
Kristian Høgsberg4a9be132011-05-02 13:38:03 -0400142#ifdef TWEENER_BOUNCE
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400143 spring->current = 2.0 - spring->current;
144 spring->previous = 2.0 - spring->previous;
Kristian Høgsberg4a9be132011-05-02 13:38:03 -0400145#else
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400146 spring->current = 1.0;
147 spring->previous = 1.0;
Kristian Høgsberg4a9be132011-05-02 13:38:03 -0400148#endif
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400149 }
150
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400151 if (spring->current <= 0.0) {
152 spring->current = 0.0;
153 spring->previous = 0.0;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400154 }
Kristian Høgsberg52612f12011-05-19 11:55:50 -0400155#endif
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400156}
157
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400158WL_EXPORT int
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400159wlsc_spring_done(struct wlsc_spring *spring)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400160{
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400161 return fabs(spring->previous - spring->target) < 0.0002 &&
162 fabs(spring->current - spring->target) < 0.0002;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400163}
164
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400165WL_EXPORT struct wlsc_surface *
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400166wlsc_surface_create(struct wlsc_compositor *compositor,
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400167 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500168{
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400169 struct wlsc_surface *surface;
170
171 surface = malloc(sizeof *surface);
172 if (surface == NULL)
173 return NULL;
174
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500175 wl_list_init(&surface->link);
Benjamin Franzkebab41fb2011-03-07 18:04:59 +0100176 wl_list_init(&surface->buffer_link);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500177 surface->map_type = WLSC_SURFACE_MAP_UNMAPPED;
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500178
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500179 glGenTextures(1, &surface->texture);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400180 glBindTexture(GL_TEXTURE_2D, surface->texture);
181 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
182 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
183 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
185
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500186 surface->compositor = compositor;
Kristian Høgsbergc5d6be92011-01-14 16:22:37 -0500187 surface->visual = NULL;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200188 surface->image = EGL_NO_IMAGE_KHR;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200189 surface->saved_texture = 0;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400190 surface->x = x;
191 surface->y = y;
192 surface->width = width;
193 surface->height = height;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400194
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400195 surface->transform = NULL;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500196
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400197 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500198}
199
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400200WL_EXPORT void
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500201wlsc_surface_damage_rectangle(struct wlsc_surface *surface,
202 int32_t x, int32_t y,
203 int32_t width, int32_t height)
204{
205 struct wlsc_compositor *compositor = surface->compositor;
206
207 pixman_region32_union_rect(&compositor->damage_region,
208 &compositor->damage_region,
209 surface->x + x, surface->y + y,
210 width, height);
211 wlsc_compositor_schedule_repaint(compositor);
212}
213
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400214WL_EXPORT void
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500215wlsc_surface_damage(struct wlsc_surface *surface)
216{
217 wlsc_surface_damage_rectangle(surface, 0, 0,
218 surface->width, surface->height);
219}
220
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400221WL_EXPORT uint32_t
222wlsc_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500223{
224 struct timeval tv;
225
226 gettimeofday(&tv, NULL);
227
228 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
229}
230
Kristian Høgsberg54879822008-11-23 17:07:32 -0500231static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400232destroy_surface(struct wl_resource *resource, struct wl_client *client)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500233{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400234 struct wlsc_surface *surface =
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500235 container_of(resource, struct wlsc_surface, surface.resource);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400236 struct wlsc_compositor *compositor = surface->compositor;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400237
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500238 wlsc_surface_damage(surface);
239
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400240 wl_list_remove(&surface->link);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200241 if (surface->saved_texture == 0)
242 glDeleteTextures(1, &surface->texture);
243 else
244 glDeleteTextures(1, &surface->saved_texture);
245
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400246
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200247 if (surface->image != EGL_NO_IMAGE_KHR)
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400248 compositor->destroy_image(compositor->display,
249 surface->image);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200250
Benjamin Franzkebab41fb2011-03-07 18:04:59 +0100251 wl_list_remove(&surface->buffer_link);
252
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400253 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500254}
255
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100256static void
257wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
258{
259 struct wlsc_surface *es = (struct wlsc_surface *) surface;
260 struct wlsc_compositor *ec = es->compositor;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100261 struct wl_list *surfaces_attached_to;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100262
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100263 if (es->saved_texture != 0)
264 es->texture = es->saved_texture;
265
266 glBindTexture(GL_TEXTURE_2D, es->texture);
267
268 if (wl_buffer_is_shm(buffer)) {
269 /* Unbind any EGLImage texture that may be bound, so we don't
270 * overwrite it.*/
271 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
272 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200273 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100274 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200275 es->pitch, buffer->height, 0,
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100276 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
277 wl_shm_buffer_get_data(buffer));
278 es->visual = buffer->visual;
279
280 surfaces_attached_to = buffer->user_data;
281
Kristian Høgsbergfac11d22011-05-02 13:35:17 -0400282 wl_list_remove(&es->buffer_link);
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100283 wl_list_insert(surfaces_attached_to, &es->buffer_link);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100284 } else {
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400285 es->image = ec->create_image(ec->display, NULL,
286 EGL_WAYLAND_BUFFER_WL,
287 buffer, NULL);
288
289 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100290 es->visual = buffer->visual;
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200291 es->pitch = es->width;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100292 }
293}
294
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200295static void
296wlsc_sprite_attach(struct wlsc_sprite *sprite, struct wl_surface *surface)
297{
298 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400299 struct wlsc_compositor *ec = es->compositor;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200300
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200301 es->pitch = es->width;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200302 es->image = sprite->image;
303 if (sprite->image != EGL_NO_IMAGE_KHR) {
304 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400305 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200306 } else {
307 if (es->saved_texture == 0)
308 es->saved_texture = es->texture;
309 es->texture = sprite->texture;
310 }
311
312 es->visual = sprite->visual;
313}
314
315enum sprite_usage {
316 SPRITE_USE_CURSOR = (1 << 0),
317};
318
319static struct wlsc_sprite *
320create_sprite_from_png(struct wlsc_compositor *ec,
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400321 const char *filename, uint32_t usage)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500322{
323 uint32_t *pixels;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200324 struct wlsc_sprite *sprite;
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400325 int32_t width, height;
326 uint32_t stride;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500327
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400328 pixels = wlsc_load_image(filename, &width, &height, &stride);
329 if (pixels == NULL)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200330 return NULL;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500331
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200332 sprite = malloc(sizeof *sprite);
333 if (sprite == NULL) {
334 free(pixels);
335 return NULL;
336 }
337
338 sprite->visual = &ec->compositor.premultiplied_argb_visual;
339 sprite->width = width;
340 sprite->height = height;
341 sprite->image = EGL_NO_IMAGE_KHR;
342
343 if (usage & SPRITE_USE_CURSOR && ec->create_cursor_image != NULL)
344 sprite->image = ec->create_cursor_image(ec, width, height);
345
346 glGenTextures(1, &sprite->texture);
347 glBindTexture(GL_TEXTURE_2D, sprite->texture);
348 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
349 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
350 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
351 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
352
353 if (sprite->image != EGL_NO_IMAGE_KHR) {
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400354 ec->image_target_texture_2d(GL_TEXTURE_2D, sprite->image);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200355 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
356 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
357 } else {
358 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, width, height, 0,
359 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
360 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500361
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500362 free(pixels);
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500363
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200364 return sprite;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500365}
366
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400367static const struct {
368 const char *filename;
369 int hotspot_x, hotspot_y;
370} pointer_images[] = {
Kristian Høgsberg4219a402010-08-16 16:43:03 -0400371 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
372 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
373 { DATADIR "/wayland/bottom_side.png", 16, 20 },
374 { DATADIR "/wayland/grabbing.png", 20, 17 },
375 { DATADIR "/wayland/left_ptr.png", 10, 5 },
376 { DATADIR "/wayland/left_side.png", 10, 20 },
377 { DATADIR "/wayland/right_side.png", 30, 19 },
378 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
379 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
380 { DATADIR "/wayland/top_side.png", 18, 8 },
381 { DATADIR "/wayland/xterm.png", 15, 15 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400382};
383
384static void
385create_pointer_images(struct wlsc_compositor *ec)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500386{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400387 int i, count;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400388
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400389 count = ARRAY_LENGTH(pointer_images);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200390 ec->pointer_sprites = malloc(count * sizeof *ec->pointer_sprites);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400391 for (i = 0; i < count; i++) {
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200392 ec->pointer_sprites[i] =
393 create_sprite_from_png(ec,
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500394 pointer_images[i].filename,
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200395 SPRITE_USE_CURSOR);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400396 }
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400397}
398
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500399static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500400background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500401{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500402 struct wlsc_surface *background;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200403 struct wlsc_sprite *sprite;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500404
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400405 background = wlsc_surface_create(output->compositor,
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400406 output->x, output->y,
407 output->width, output->height);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500408 if (background == NULL)
409 return NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500410
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400411 sprite = create_sprite_from_png(output->compositor, filename, 0);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200412 if (sprite == NULL) {
Benjamin Franzked3b023e2011-01-15 12:34:49 +0100413 free(background);
414 return NULL;
415 }
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100416
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200417 wlsc_sprite_attach(sprite, &background->surface);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500418
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500419 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500420}
421
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400422static int
423texture_region(struct wlsc_surface *es, pixman_region32_t *region)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500424{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500425 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500426 GLfloat *v, inv_width, inv_height;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500427 pixman_box32_t *rectangles;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400428 unsigned int *p;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500429 int i, n;
430
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400431 rectangles = pixman_region32_rectangles(region, &n);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500432 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
433 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200434 inv_width = 1.0 / es->pitch;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500435 inv_height = 1.0 / es->height;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400436
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500437 for (i = 0; i < n; i++, v += 16, p += 6) {
438 v[ 0] = rectangles[i].x1;
439 v[ 1] = rectangles[i].y1;
440 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
441 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500442
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500443 v[ 4] = rectangles[i].x1;
444 v[ 5] = rectangles[i].y2;
445 v[ 6] = v[ 2];
446 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500447
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500448 v[ 8] = rectangles[i].x2;
449 v[ 9] = rectangles[i].y1;
450 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
451 v[11] = v[ 3];
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500452
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500453 v[12] = rectangles[i].x2;
454 v[13] = rectangles[i].y2;
455 v[14] = v[10];
456 v[15] = v[ 7];
457
458 p[0] = i * 4 + 0;
459 p[1] = i * 4 + 1;
460 p[2] = i * 4 + 2;
461 p[3] = i * 4 + 2;
462 p[4] = i * 4 + 1;
463 p[5] = i * 4 + 3;
464 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500465
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400466 return n;
467}
468
469static void
470transform_vertex(struct wlsc_surface *surface,
471 GLfloat x, GLfloat y, GLfloat u, GLfloat v, GLfloat *r)
472{
473 struct wlsc_vector t;
474
475 t.f[0] = x;
476 t.f[1] = y;
477 t.f[2] = 0.0;
478 t.f[3] = 1.0;
479
Kristian Høgsberg0bc0e242011-05-02 14:35:40 -0400480 wlsc_matrix_transform(&surface->transform->matrix, &t);
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400481
482 r[ 0] = t.f[0];
483 r[ 1] = t.f[1];
484 r[ 2] = u;
485 r[ 3] = v;
486}
487
488static int
489texture_transformed_surface(struct wlsc_surface *es)
490{
491 struct wlsc_compositor *ec = es->compositor;
492 GLfloat *v;
493 unsigned int *p;
494
495 v = wl_array_add(&ec->vertices, 16 * sizeof *v);
496 p = wl_array_add(&ec->indices, 6 * sizeof *p);
497
498 transform_vertex(es, es->x, es->y, 0.0, 0.0, &v[0]);
499 transform_vertex(es, es->x, es->y + es->height, 0.0, 1.0, &v[4]);
500 transform_vertex(es, es->x + es->width, es->y, 1.0, 0.0, &v[8]);
501 transform_vertex(es, es->x + es->width, es->y + es->height,
502 1.0, 1.0, &v[12]);
503
504 p[0] = 0;
505 p[1] = 1;
506 p[2] = 2;
507 p[3] = 2;
508 p[4] = 1;
509 p[5] = 3;
510
511 return 1;
512}
513
514static void
515wlsc_surface_draw(struct wlsc_surface *es,
516 struct wlsc_output *output, pixman_region32_t *clip)
517{
518 struct wlsc_compositor *ec = es->compositor;
519 GLfloat *v;
520 pixman_region32_t repaint;
521 int n;
522
523 pixman_region32_init_rect(&repaint,
524 es->x, es->y, es->width, es->height);
525 pixman_region32_intersect(&repaint, &repaint, clip);
526 if (!pixman_region32_not_empty(&repaint))
527 return;
528
529 if (es->visual == &ec->compositor.argb_visual) {
530 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
531 glEnable(GL_BLEND);
532 } else if (es->visual == &ec->compositor.premultiplied_argb_visual) {
533 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
534 glEnable(GL_BLEND);
535 } else {
536 glDisable(GL_BLEND);
537 }
538
539 if (es->transform == NULL)
540 n = texture_region(es, &repaint);
541 else
542 n = texture_transformed_surface(es);
543
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500544 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500545 v = ec->vertices.data;
546 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
547 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400548 glEnableVertexAttribArray(0);
549 glEnableVertexAttribArray(1);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500550 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
551
552 ec->vertices.size = 0;
553 ec->indices.size = 0;
554 pixman_region32_fini(&repaint);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500555}
556
557static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400558wlsc_surface_raise(struct wlsc_surface *surface)
559{
560 struct wlsc_compositor *compositor = surface->compositor;
561
562 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400563 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400564}
565
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400566WL_EXPORT void
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400567wlsc_compositor_damage_all(struct wlsc_compositor *compositor)
568{
569 struct wlsc_output *output;
570
571 wl_list_for_each(output, &compositor->output_list, link)
572 wlsc_output_damage(output);
573}
574
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400575WL_EXPORT void
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100576wlsc_output_finish_frame(struct wlsc_output *output, int msecs)
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400577{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100578 struct wlsc_compositor *compositor = output->compositor;
579 struct wlsc_surface *es;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400580 struct wlsc_animation *animation, *next;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100581
582 wl_list_for_each(es, &compositor->surface_list, link) {
583 if (es->output == output) {
584 wl_display_post_frame(compositor->wl_display,
585 &es->surface, msecs);
586 }
587 }
588
589 output->finished = 1;
590
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400591 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsbergf30c67e2011-02-06 12:58:44 -0500592 compositor->repaint_on_timeout = 1;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400593
594 wl_list_for_each_safe(animation, next,
595 &compositor->animation_list, link)
596 animation->frame(animation, output, msecs);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400597}
598
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400599WL_EXPORT void
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400600wlsc_output_damage(struct wlsc_output *output)
601{
602 struct wlsc_compositor *compositor = output->compositor;
603
604 pixman_region32_union_rect(&compositor->damage_region,
605 &compositor->damage_region,
606 output->x, output->y,
607 output->width, output->height);
608 wlsc_compositor_schedule_repaint(compositor);
609}
610
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400611static void
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400612fade_frame(struct wlsc_animation *animation,
613 struct wlsc_output *output, uint32_t msecs)
614{
615 struct wlsc_compositor *compositor =
616 container_of(animation,
617 struct wlsc_compositor, fade.animation);
618
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400619 wlsc_spring_update(&compositor->fade.spring, msecs);
620 if (wlsc_spring_done(&compositor->fade.spring)) {
621 if (compositor->fade.spring.current > 0.999) {
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400622 compositor->state = WLSC_COMPOSITOR_SLEEPING;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400623 compositor->shell->lock(compositor->shell);
624 }
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400625 compositor->fade.spring.current =
626 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400627 wl_list_remove(&animation->link);
628 wl_list_init(&animation->link);
629 }
630
631 wlsc_output_damage(output);
632}
633
634static void
635fade_output(struct wlsc_output *output,
636 GLfloat tint, pixman_region32_t *region)
637{
638 struct wlsc_compositor *compositor = output->compositor;
639 struct wlsc_surface surface;
640 GLfloat color[4] = { 0.0, 0.0, 0.0, tint };
641
642 surface.compositor = compositor;
643 surface.x = output->x;
644 surface.y = output->y;
645 surface.width = output->width;
646 surface.height = output->height;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400647 surface.texture = GL_NONE;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400648 surface.transform = NULL;
Kristian Høgsbergc352ab02011-04-23 15:34:50 -0400649
650 if (tint <= 1.0)
651 surface.visual =
652 &compositor->compositor.premultiplied_argb_visual;
653 else
654 surface.visual = &compositor->compositor.rgb_visual;
655
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400656 glUseProgram(compositor->solid_shader.program);
657 glUniformMatrix4fv(compositor->solid_shader.proj_uniform,
658 1, GL_FALSE, output->matrix.d);
659 glUniform4fv(compositor->solid_shader.color_uniform, 1, color);
660 wlsc_surface_draw(&surface, output, region);
661}
662
663static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400664wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400665{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500666 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500667 struct wlsc_surface *es;
Kristian Høgsberge4c40a42011-05-06 14:04:21 -0400668 struct wlsc_input_device *eid, *hw_cursor;
Kristian Høgsberga04eecc2011-05-02 13:52:00 -0400669 pixman_region32_t new_damage, total_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500670
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100671 output->prepare_render(output);
672
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500673 glViewport(0, 0, output->width, output->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500674
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -0400675 glUseProgram(ec->texture_shader.program);
676 glUniformMatrix4fv(ec->texture_shader.proj_uniform,
677 1, GL_FALSE, output->matrix.d);
678 glUniform1i(ec->texture_shader.tex_uniform, 0);
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500679
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500680 pixman_region32_init(&new_damage);
681 pixman_region32_init(&total_damage);
682 pixman_region32_intersect_rect(&new_damage,
683 &ec->damage_region,
684 output->x, output->y,
685 output->width, output->height);
686 pixman_region32_subtract(&ec->damage_region,
687 &ec->damage_region, &new_damage);
688 pixman_region32_union(&total_damage, &new_damage,
689 &output->previous_damage_region);
690 pixman_region32_copy(&output->previous_damage_region, &new_damage);
691
Kristian Høgsberge4c40a42011-05-06 14:04:21 -0400692 hw_cursor = NULL;
693 if (ec->focus && ec->fade.spring.current < 0.001) {
694 hw_cursor = (struct wlsc_input_device *) ec->input_device;
695 if (output->set_hardware_cursor(output, hw_cursor) < 0)
696 hw_cursor = NULL;
697 } else {
698 output->set_hardware_cursor(output, NULL);
699 }
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200700
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500701 es = container_of(ec->surface_list.next, struct wlsc_surface, link);
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200702
Kristian Høgsberge4c40a42011-05-06 14:04:21 -0400703 if (es->visual == &ec->compositor.rgb_visual && hw_cursor) {
Kristian Høgsberg4d07a1c2011-05-06 14:03:12 -0400704 if (output->prepare_scanout_surface(output, es) == 0) {
705 /* We're drawing nothing now,
706 * draw the damaged regions later. */
707 pixman_region32_union(&ec->damage_region,
708 &ec->damage_region,
709 &total_damage);
710 return;
711 }
712 }
713
714 if (es->fullscreen_output == output) {
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200715 if (es->width < output->width ||
716 es->height < output->height)
717 glClear(GL_COLOR_BUFFER_BIT);
718 wlsc_surface_draw(es, output, &total_damage);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500719 } else {
720 if (output->background)
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500721 wlsc_surface_draw(output->background,
722 output, &total_damage);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500723 else
Kristian Høgsbergc352ab02011-04-23 15:34:50 -0400724 fade_output(output, 1.0, &total_damage);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500725
Kristian Høgsbergc352ab02011-04-23 15:34:50 -0400726 glUseProgram(ec->texture_shader.program);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500727 wl_list_for_each_reverse(es, &ec->surface_list, link) {
Kristian Høgsberg547cadf2011-04-12 22:23:30 -0400728 if (ec->overlay == es)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500729 continue;
730
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500731 wlsc_surface_draw(es, output, &total_damage);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500732 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500733 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400734
Kristian Høgsberg547cadf2011-04-12 22:23:30 -0400735 if (ec->overlay)
736 wlsc_surface_draw(ec->overlay, output, &total_damage);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500737
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400738 if (ec->focus)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200739 wl_list_for_each(eid, &ec->input_device_list, link) {
740 if (&eid->input_device != ec->input_device ||
Kristian Høgsberge4c40a42011-05-06 14:04:21 -0400741 eid != hw_cursor)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200742 wlsc_surface_draw(eid->sprite, output,
743 &total_damage);
744 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400745
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400746 if (ec->fade.spring.current > 0.001)
747 fade_output(output, ec->fade.spring.current, &total_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500748}
749
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400750static int
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500751repaint(void *data)
752{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500753 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500754 struct wlsc_output *output;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100755 int repainted_all_outputs = 1;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500756
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100757 wl_list_for_each(output, &ec->output_list, link) {
758 if (!output->repaint_needed)
759 continue;
760
761 if (!output->finished) {
762 repainted_all_outputs = 0;
763 continue;
764 }
765
766 wlsc_output_repaint(output);
767 output->finished = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100768 output->repaint_needed = 0;
Kristian Høgsberg11e28282011-04-11 16:47:50 -0400769 output->present(output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500770 }
771
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100772 if (repainted_all_outputs)
773 ec->repaint_on_timeout = 0;
774 else
775 wl_event_source_timer_update(ec->timer_source, 1);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400776
777 return 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400778}
779
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400780WL_EXPORT void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400781wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400782{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100783 struct wlsc_output *output;
784
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400785 if (compositor->state == WLSC_COMPOSITOR_SLEEPING)
786 return;
787
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100788 wl_list_for_each(output, &compositor->output_list, link)
789 output->repaint_needed = 1;
790
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400791 if (compositor->repaint_on_timeout)
792 return;
793
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400794 wl_event_source_timer_update(compositor->timer_source, 1);
795 compositor->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400796}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500797
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400798WL_EXPORT void
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400799wlsc_compositor_fade(struct wlsc_compositor *compositor, float tint)
800{
801 int done;
802
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400803 done = wlsc_spring_done(&compositor->fade.spring);
804 compositor->fade.spring.target = tint;
805 if (wlsc_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400806 return;
807
808 if (done)
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400809 compositor->fade.spring.timestamp =
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400810 wlsc_compositor_get_time();
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400811
812 wlsc_compositor_damage_all(compositor);
813 if (wl_list_empty(&compositor->fade.animation.link))
814 wl_list_insert(compositor->animation_list.prev,
815 &compositor->fade.animation.link);
816}
817
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500818static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500819surface_destroy(struct wl_client *client,
820 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400821{
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200822 wl_resource_destroy(&surface->resource, client,
823 wlsc_compositor_get_time());
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400824}
825
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400826WL_EXPORT void
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100827wlsc_surface_assign_output(struct wlsc_surface *es)
828{
829 struct wlsc_compositor *ec = es->compositor;
830 struct wlsc_output *output;
831
832 struct wlsc_output *tmp = es->output;
833 es->output = NULL;
834
835 wl_list_for_each(output, &ec->output_list, link) {
836 if (output->x < es->x && es->x < output->x + output->width &&
837 output->y < es->y && es->y < output->y + output->height) {
838 if (output != tmp)
839 printf("assiging surface %p to output %p\n",
840 es, output);
841 es->output = output;
842 }
843 }
844
845 if (es->output == NULL) {
846 printf("no output found\n");
847 es->output = container_of(ec->output_list.next,
848 struct wlsc_output, link);
849 }
850}
851
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500852static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500853surface_attach(struct wl_client *client,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500854 struct wl_surface *surface, struct wl_buffer *buffer,
855 int32_t x, int32_t y)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400856{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500857 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400858
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500859 /* FIXME: This damages the entire old surface, but we should
860 * really just damage the part that's no longer covered by the
861 * surface. Anything covered by the new surface will be
862 * damaged by the client. */
863 wlsc_surface_damage(es);
864
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400865 es->x += x;
866 es->y += y;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500867 es->width = buffer->width;
868 es->height = buffer->height;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100869 if (x != 0 || y != 0)
870 wlsc_surface_assign_output(es);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400871 if (es->visual == NULL)
872 wl_list_insert(&es->compositor->surface_list, &es->link);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400873
Kristian Høgsberg2e02d242011-05-13 14:06:29 -0400874 wlsc_buffer_attach(buffer, surface);
875
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400876 es->compositor->shell->attach(es->compositor->shell, es);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400877}
878
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500879static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500880surface_damage(struct wl_client *client,
881 struct wl_surface *surface,
882 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500883{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400884 struct wlsc_surface *es = (struct wlsc_surface *) surface;
885
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500886 wlsc_surface_damage_rectangle(es, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500887}
888
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500889const static struct wl_surface_interface surface_interface = {
890 surface_destroy,
891 surface_attach,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500892 surface_damage
893};
894
895static void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400896wlsc_input_device_attach(struct wlsc_input_device *device,
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200897 int x, int y, int width, int height)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400898{
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500899 wlsc_surface_damage(device->sprite);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400900
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400901 device->hotspot_x = x;
902 device->hotspot_y = y;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400903
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500904 device->sprite->x = device->input_device.x - device->hotspot_x;
905 device->sprite->y = device->input_device.y - device->hotspot_y;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200906 device->sprite->width = width;
907 device->sprite->height = height;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400908
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500909 wlsc_surface_damage(device->sprite);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400910}
911
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200912static void
913wlsc_input_device_attach_buffer(struct wlsc_input_device *device,
914 struct wl_buffer *buffer, int x, int y)
915{
916 wlsc_buffer_attach(buffer, &device->sprite->surface);
917 wlsc_input_device_attach(device, x, y, buffer->width, buffer->height);
918}
919
920static void
921wlsc_input_device_attach_sprite(struct wlsc_input_device *device,
922 struct wlsc_sprite *sprite, int x, int y)
923{
924 wlsc_sprite_attach(sprite, &device->sprite->surface);
925 wlsc_input_device_attach(device, x, y, sprite->width, sprite->height);
926}
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400927
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400928WL_EXPORT void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400929wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
930 enum wlsc_pointer_type type)
931{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500932 struct wlsc_compositor *compositor =
933 (struct wlsc_compositor *) device->input_device.compositor;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400934
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200935 wlsc_input_device_attach_sprite(device,
936 compositor->pointer_sprites[type],
937 pointer_images[type].hotspot_x,
938 pointer_images[type].hotspot_y);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400939}
940
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400941static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500942compositor_create_surface(struct wl_client *client,
943 struct wl_compositor *compositor, uint32_t id)
944{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500945 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400946 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500947
Kristian Høgsbergc5d6be92011-01-14 16:22:37 -0500948 surface = wlsc_surface_create(ec, 0, 0, 0, 0);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400949 if (surface == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -0400950 wl_client_post_no_memory(client);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500951 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400952 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500953
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500954 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -0400955
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500956 surface->surface.resource.object.id = id;
957 surface->surface.resource.object.interface = &wl_surface_interface;
958 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -0400959 (void (**)(void)) &surface_interface;
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500960 surface->surface.client = client;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -0400961
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500962 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500963}
964
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500965const static struct wl_compositor_interface compositor_interface = {
966 compositor_create_surface,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500967};
968
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500969static void
970wlsc_surface_transform(struct wlsc_surface *surface,
971 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
972{
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400973 *sx = x - surface->x;
974 *sy = y - surface->y;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500975}
976
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400977WL_EXPORT struct wlsc_surface *
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500978pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500979{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500980 struct wlsc_compositor *ec =
981 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500982 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500983
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400984 wl_list_for_each(es, &ec->surface_list, link) {
985 wlsc_surface_transform(es, device->x, device->y, sx, sy);
986 if (0 <= *sx && *sx < es->width &&
987 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500988 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400989 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500990
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500991 return NULL;
992}
993
Kristian Høgsberg8321e692010-12-07 17:06:15 -0500994
995static void
996motion_grab_motion(struct wl_grab *grab,
997 uint32_t time, int32_t x, int32_t y)
998{
999 struct wlsc_input_device *device =
1000 (struct wlsc_input_device *) grab->input_device;
1001 struct wlsc_surface *es =
1002 (struct wlsc_surface *) device->input_device.pointer_focus;
1003 int32_t sx, sy;
1004
1005 wlsc_surface_transform(es, x, y, &sx, &sy);
1006 wl_client_post_event(es->surface.client,
1007 &device->input_device.object,
1008 WL_INPUT_DEVICE_MOTION,
1009 time, x, y, sx, sy);
1010}
1011
1012static void
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001013motion_grab_button(struct wl_grab *grab,
1014 uint32_t time, int32_t button, int32_t state)
1015{
1016 wl_client_post_event(grab->input_device->pointer_focus->client,
1017 &grab->input_device->object,
1018 WL_INPUT_DEVICE_BUTTON,
1019 time, button, state);
1020}
1021
1022static void
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001023motion_grab_end(struct wl_grab *grab, uint32_t time)
1024{
1025}
1026
1027static const struct wl_grab_interface motion_grab_interface = {
1028 motion_grab_motion,
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001029 motion_grab_button,
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001030 motion_grab_end
1031};
1032
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001033WL_EXPORT void
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001034wlsc_compositor_wake(struct wlsc_compositor *compositor)
1035{
1036 if (compositor->idle_inhibit)
1037 return;
1038
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001039 wlsc_compositor_fade(compositor, 0.0);
1040 compositor->state = WLSC_COMPOSITOR_ACTIVE;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001041
1042 wl_event_source_timer_update(compositor->idle_source,
1043 option_idle_time * 1000);
1044}
1045
1046static void
1047wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor)
1048{
1049 wlsc_compositor_wake(compositor);
1050 compositor->idle_inhibit++;
1051}
1052
1053static void
1054wlsc_compositor_idle_release(struct wlsc_compositor *compositor)
1055{
1056 compositor->idle_inhibit--;
1057 wlsc_compositor_wake(compositor);
1058}
1059
1060static int
1061idle_handler(void *data)
1062{
1063 struct wlsc_compositor *compositor = data;
1064
1065 if (compositor->idle_inhibit)
1066 return 1;
1067
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001068 wlsc_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001069
1070 return 1;
1071}
1072
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001073WL_EXPORT void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001074notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001075{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001076 struct wlsc_surface *es;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001077 struct wlsc_compositor *ec =
1078 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001079 struct wlsc_output *output;
Kristian Høgsberg6d65d5f2010-12-07 13:30:18 -05001080 const struct wl_grab_interface *interface;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001081 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsbergfc9c28a2010-12-07 13:04:43 -05001082 int32_t sx, sy;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001083 int x_valid = 0, y_valid = 0;
1084 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 -05001085
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001086 wlsc_compositor_wake(ec);
1087
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001088 wl_list_for_each(output, &ec->output_list, link) {
1089 if (output->x <= x && x <= output->x + output->width)
1090 x_valid = 1;
1091
1092 if (output->y <= y && y <= output->y + output->height)
1093 y_valid = 1;
1094
1095 /* FIXME: calculate this only on output addition/deletion */
1096 if (output->x < min_x)
1097 min_x = output->x;
1098 if (output->y < min_y)
1099 min_y = output->y;
1100
1101 if (output->x + output->width > max_x)
1102 max_x = output->x + output->width;
1103 if (output->y + output->height > max_y)
1104 max_y = output->y + output->height;
1105 }
1106
1107 if (!x_valid) {
1108 if (x < min_x)
1109 x = min_x;
1110 else if (x >= max_x)
1111 x = max_x;
1112 }
1113 if (!y_valid) {
1114 if (y < min_y)
1115 y = min_y;
1116 else if (y >= max_y)
1117 y = max_y;
1118 }
Ray Strode90e701d2008-12-18 23:05:43 -05001119
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -05001120 device->x = x;
1121 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001122
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001123 if (device->grab) {
1124 interface = device->grab->interface;
1125 interface->motion(device->grab, time, x, y);
1126 } else {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001127 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001128 wl_input_device_set_pointer_focus(device,
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001129 &es->surface,
Kristian Høgsberg26437072010-12-01 10:17:47 -05001130 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001131 if (es)
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001132 wl_client_post_event(es->surface.client,
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001133 &device->object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001134 WL_INPUT_DEVICE_MOTION,
1135 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001136 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001137
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001138 wlsc_surface_damage(wd->sprite);
1139
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001140 wd->sprite->x = device->x - wd->hotspot_x;
1141 wd->sprite->y = device->y - wd->hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001142
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001143 wlsc_surface_damage(wd->sprite);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001144}
1145
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001146WL_EXPORT void
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001147wlsc_surface_activate(struct wlsc_surface *surface,
1148 struct wlsc_input_device *device, uint32_t time)
1149{
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001150 struct wlsc_shell *shell = surface->compositor->shell;
1151
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001152 wlsc_surface_raise(surface);
1153 if (device->selection)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001154 shell->set_selection_focus(shell,
1155 device->selection,
1156 &surface->surface, time);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001157
1158 wl_input_device_set_keyboard_focus(&device->input_device,
1159 &surface->surface,
1160 time);
1161}
1162
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001163struct wlsc_binding {
1164 uint32_t key;
1165 uint32_t button;
1166 uint32_t modifier;
1167 wlsc_binding_handler_t handler;
1168 void *data;
1169 struct wl_list link;
1170};
1171
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001172WL_EXPORT void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001173notify_button(struct wl_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001174 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001175{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001176 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001177 struct wlsc_compositor *compositor =
1178 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001179 struct wlsc_binding *b;
1180 struct wlsc_surface *surface =
1181 (struct wlsc_surface *) device->pointer_focus;
Kristian Høgsbergea081152010-12-07 08:59:51 -05001182
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001183 if (state)
1184 wlsc_compositor_idle_inhibit(compositor);
1185 else
1186 wlsc_compositor_idle_release(compositor);
1187
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -05001188 if (state && surface && device->grab == NULL) {
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001189 wlsc_surface_activate(surface, wd, time);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001190 wl_input_device_start_grab(device,
1191 &device->motion_grab,
1192 button, time);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001193 }
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -05001194
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001195 wl_list_for_each(b, &compositor->binding_list, link) {
1196 if (b->button == button &&
1197 b->modifier == wd->modifier_state && state) {
1198 b->handler(&wd->input_device,
1199 time, 0, button, state, b->data);
1200 break;
1201 }
Benjamin Franzke5a2218a2011-02-01 16:30:31 +01001202 }
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001203
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -05001204 if (device->grab)
1205 device->grab->interface->button(device->grab, time,
1206 button, state);
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -05001207
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001208 if (!state && device->grab && device->grab_button == button)
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001209 wl_input_device_end_grab(device, time);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001210}
1211
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001212static void
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001213terminate_binding(struct wl_input_device *device, uint32_t time,
1214 uint32_t key, uint32_t button, uint32_t state, void *data)
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001215{
1216 struct wlsc_compositor *compositor = data;
1217
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001218 if (state)
1219 wl_display_terminate(compositor->wl_display);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001220}
1221
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001222WL_EXPORT struct wlsc_binding *
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001223wlsc_compositor_add_binding(struct wlsc_compositor *compositor,
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001224 uint32_t key, uint32_t button, uint32_t modifier,
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001225 wlsc_binding_handler_t handler, void *data)
1226{
1227 struct wlsc_binding *binding;
1228
1229 binding = malloc(sizeof *binding);
1230 if (binding == NULL)
1231 return NULL;
1232
1233 binding->key = key;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001234 binding->button = button;
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001235 binding->modifier = modifier;
1236 binding->handler = handler;
1237 binding->data = data;
1238 wl_list_insert(compositor->binding_list.prev, &binding->link);
1239
1240 return binding;
1241}
1242
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001243WL_EXPORT void
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001244wlsc_binding_destroy(struct wlsc_binding *binding)
1245{
1246 wl_list_remove(&binding->link);
1247 free(binding);
1248}
1249
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001250static void
1251update_modifier_state(struct wlsc_input_device *device,
1252 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001253{
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001254 uint32_t modifier;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001255
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001256 switch (key) {
1257 case KEY_LEFTCTRL:
1258 case KEY_RIGHTCTRL:
1259 modifier = MODIFIER_CTRL;
1260 break;
1261
1262 case KEY_LEFTALT:
1263 case KEY_RIGHTALT:
1264 modifier = MODIFIER_ALT;
1265 break;
1266
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -04001267 case KEY_LEFTMETA:
1268 case KEY_RIGHTMETA:
1269 modifier = MODIFIER_SUPER;
1270 break;
1271
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001272 default:
1273 modifier = 0;
1274 break;
1275 }
1276
1277 if (state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001278 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001279 else
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001280 device->modifier_state &= ~modifier;
1281}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001282
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001283WL_EXPORT void
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001284notify_key(struct wl_input_device *device,
1285 uint32_t time, uint32_t key, uint32_t state)
1286{
1287 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1288 struct wlsc_compositor *compositor =
1289 (struct wlsc_compositor *) device->compositor;
1290 uint32_t *k, *end;
1291 struct wlsc_binding *b;
1292
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001293 if (state)
1294 wlsc_compositor_idle_inhibit(compositor);
1295 else
1296 wlsc_compositor_idle_release(compositor);
1297
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001298 wl_list_for_each(b, &compositor->binding_list, link) {
1299 if (b->key == key &&
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001300 b->modifier == wd->modifier_state) {
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001301 b->handler(&wd->input_device,
1302 time, key, 0, state, b->data);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001303 break;
1304 }
1305 }
1306
1307 update_modifier_state(wd, key, state);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001308 end = device->keys.data + device->keys.size;
1309 for (k = device->keys.data; k < end; k++) {
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001310 if (*k == key)
1311 *k = *--end;
1312 }
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001313 device->keys.size = (void *) end - device->keys.data;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001314 if (state) {
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001315 k = wl_array_add(&device->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001316 *k = key;
1317 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001318
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001319 if (device->keyboard_focus != NULL)
1320 wl_client_post_event(device->keyboard_focus->client,
1321 &device->object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001322 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001323}
1324
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001325WL_EXPORT void
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001326notify_pointer_focus(struct wl_input_device *device,
1327 uint32_t time, struct wlsc_output *output,
1328 int32_t x, int32_t y)
1329{
1330 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1331 struct wlsc_compositor *compositor =
1332 (struct wlsc_compositor *) device->compositor;
1333 struct wlsc_surface *es;
1334 int32_t sx, sy;
1335
1336 if (output) {
1337 device->x = x;
1338 device->y = y;
1339 es = pick_surface(device, &sx, &sy);
1340 wl_input_device_set_pointer_focus(device,
1341 &es->surface,
1342 time, x, y, sx, sy);
1343
1344 compositor->focus = 1;
1345
1346 wd->sprite->x = device->x - wd->hotspot_x;
1347 wd->sprite->y = device->y - wd->hotspot_y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001348 } else {
1349 wl_input_device_set_pointer_focus(device, NULL,
1350 time, 0, 0, 0, 0);
1351 compositor->focus = 0;
1352 }
1353
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001354 wlsc_surface_damage(wd->sprite);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001355}
1356
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001357WL_EXPORT void
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001358notify_keyboard_focus(struct wl_input_device *device,
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001359 uint32_t time, struct wlsc_output *output,
1360 struct wl_array *keys)
1361{
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001362 struct wlsc_input_device *wd =
1363 (struct wlsc_input_device *) device;
1364 struct wlsc_compositor *compositor =
1365 (struct wlsc_compositor *) device->compositor;
1366 struct wlsc_surface *es;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001367 uint32_t *k, *end;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001368
1369 if (!wl_list_empty(&compositor->surface_list))
1370 es = container_of(compositor->surface_list.next,
1371 struct wlsc_surface, link);
1372 else
1373 es = NULL;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001374
1375 if (output) {
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001376 wl_array_copy(&wd->input_device.keys, keys);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001377 wd->modifier_state = 0;
1378 end = device->keys.data + device->keys.size;
1379 for (k = device->keys.data; k < end; k++) {
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001380 wlsc_compositor_idle_inhibit(compositor);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001381 update_modifier_state(wd, *k, 1);
1382 }
1383
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001384 wl_input_device_set_keyboard_focus(&wd->input_device,
1385 &es->surface, time);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001386 } else {
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001387 end = device->keys.data + device->keys.size;
1388 for (k = device->keys.data; k < end; k++)
1389 wlsc_compositor_idle_release(compositor);
1390
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001391 wd->modifier_state = 0;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001392 wl_input_device_set_keyboard_focus(&wd->input_device,
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001393 NULL, time);
1394 }
1395}
1396
1397
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001398static void
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001399input_device_attach(struct wl_client *client,
1400 struct wl_input_device *device_base,
1401 uint32_t time,
1402 struct wl_buffer *buffer, int32_t x, int32_t y)
1403{
1404 struct wlsc_input_device *device =
1405 (struct wlsc_input_device *) device_base;
1406
1407 if (time < device->input_device.pointer_focus_time)
1408 return;
1409 if (device->input_device.pointer_focus == NULL)
1410 return;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001411 if (device->input_device.pointer_focus->client != client)
1412 return;
1413
1414 if (buffer == NULL) {
1415 wlsc_input_device_set_pointer_image(device,
1416 WLSC_POINTER_LEFT_PTR);
1417 return;
1418 }
1419
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001420 wlsc_input_device_attach_buffer(device, buffer, x, y);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001421}
1422
1423const static struct wl_input_device_interface input_device_interface = {
1424 input_device_attach,
1425};
1426
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001427WL_EXPORT void
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001428wlsc_input_device_init(struct wlsc_input_device *device,
1429 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001430{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001431 wl_input_device_init(&device->input_device, &ec->compositor);
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05001432
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001433 device->input_device.object.interface = &wl_input_device_interface;
1434 device->input_device.object.implementation =
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001435 (void (**)(void)) &input_device_interface;
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001436 wl_display_add_object(ec->wl_display, &device->input_device.object);
1437 wl_display_add_global(ec->wl_display, &device->input_device.object, NULL);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001438
Kristian Høgsbergc5d6be92011-01-14 16:22:37 -05001439 device->sprite = wlsc_surface_create(ec,
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001440 device->input_device.x,
1441 device->input_device.y, 32, 32);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001442 device->hotspot_x = 16;
1443 device->hotspot_y = 16;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001444 device->modifier_state = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001445
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001446 device->input_device.motion_grab.interface = &motion_grab_interface;
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001447
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001448 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001449
1450 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001451}
1452
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001453static void
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001454wlsc_output_post_geometry(struct wl_client *client,
1455 struct wl_object *global, uint32_t version)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001456{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001457 struct wlsc_output *output =
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001458 container_of(global, struct wlsc_output, object);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001459
1460 wl_client_post_event(client, global,
1461 WL_OUTPUT_GEOMETRY,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001462 output->x, output->y,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001463 output->width, output->height);
1464}
1465
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001466static const char vertex_shader[] =
1467 "uniform mat4 proj;\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001468 "attribute vec2 position;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001469 "attribute vec2 texcoord;\n"
1470 "varying vec2 v_texcoord;\n"
1471 "void main()\n"
1472 "{\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001473 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001474 " v_texcoord = texcoord;\n"
1475 "}\n";
1476
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001477static const char texture_fragment_shader[] =
Kristian Høgsbergdfce71d2010-12-07 20:19:10 -05001478 "precision mediump float;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001479 "varying vec2 v_texcoord;\n"
1480 "uniform sampler2D tex;\n"
1481 "void main()\n"
1482 "{\n"
1483 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1484 "}\n";
1485
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001486static const char solid_fragment_shader[] =
1487 "precision mediump float;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001488 "uniform vec4 color;\n"
1489 "void main()\n"
1490 "{\n"
1491 " gl_FragColor = color\n;"
1492 "}\n";
1493
Kristian Høgsberga9468212010-06-14 21:03:11 -04001494static int
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001495compile_shader(GLenum type, const char *source)
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001496{
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001497 GLuint s;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001498 char msg[512];
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001499 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001500
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001501 s = glCreateShader(type);
1502 glShaderSource(s, 1, &source, NULL);
1503 glCompileShader(s);
1504 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001505 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001506 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1507 fprintf(stderr, "shader info: %s\n", msg);
1508 return GL_NONE;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001509 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001510
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001511 return s;
1512}
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001513
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001514static int
1515wlsc_shader_init(struct wlsc_shader *shader,
1516 const char *vertex_source, const char *fragment_source)
1517{
1518 char msg[512];
1519 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001520
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001521 shader->vertex_shader =
1522 compile_shader(GL_VERTEX_SHADER, vertex_source);
1523 shader->fragment_shader =
1524 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1525
1526 shader->program = glCreateProgram();
1527 glAttachShader(shader->program, shader->vertex_shader);
1528 glAttachShader(shader->program, shader->fragment_shader);
1529 glBindAttribLocation(shader->program, 0, "position");
1530 glBindAttribLocation(shader->program, 1, "texcoord");
1531
1532 glLinkProgram(shader->program);
1533 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001534 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001535 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001536 fprintf(stderr, "link info: %s\n", msg);
1537 return -1;
1538 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001539
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001540 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1541 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001542
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001543 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001544}
1545
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001546static int
1547init_solid_shader(struct wlsc_shader *shader,
1548 GLuint vertex_shader, const char *fragment_source)
1549{
1550 GLint status;
1551 char msg[512];
1552
1553 shader->vertex_shader = vertex_shader;
1554 shader->fragment_shader =
1555 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1556
1557 shader->program = glCreateProgram();
1558 glAttachShader(shader->program, shader->vertex_shader);
1559 glAttachShader(shader->program, shader->fragment_shader);
1560 glBindAttribLocation(shader->program, 0, "position");
1561 glBindAttribLocation(shader->program, 1, "texcoord");
1562
1563 glLinkProgram(shader->program);
1564 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1565 if (!status) {
1566 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1567 fprintf(stderr, "link info: %s\n", msg);
1568 return -1;
1569 }
1570
1571 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1572 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1573
1574 return 0;
1575}
1576
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001577WL_EXPORT void
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001578wlsc_output_destroy(struct wlsc_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001579{
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001580 destroy_surface(&output->background->surface.resource, NULL);
1581}
1582
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001583WL_EXPORT void
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001584wlsc_output_move(struct wlsc_output *output, int x, int y)
1585{
1586 struct wlsc_compositor *c = output->compositor;
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01001587 int flip;
1588
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001589 output->x = x;
1590 output->y = y;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001591
Benjamin Franzke264b3f92011-03-16 13:48:42 +01001592 if (output->background) {
1593 output->background->x = x;
1594 output->background->y = y;
Benjamin Franzke264b3f92011-03-16 13:48:42 +01001595 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001596
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001597 pixman_region32_init(&output->previous_damage_region);
1598
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001599 wlsc_matrix_init(&output->matrix);
1600 wlsc_matrix_translate(&output->matrix,
1601 -output->x - output->width / 2.0,
1602 -output->y - output->height / 2.0, 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01001603
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001604 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001605 wlsc_matrix_scale(&output->matrix,
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01001606 2.0 / output->width,
1607 flip * 2.0 / output->height, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001608
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001609 pixman_region32_union_rect(&c->damage_region,
1610 &c->damage_region,
1611 x, y, output->width, output->height);
1612}
1613
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001614WL_EXPORT void
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001615wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1616 int x, int y, int width, int height, uint32_t flags)
1617{
1618 output->compositor = c;
1619 output->x = x;
1620 output->y = y;
1621 output->width = width;
1622 output->height = height;
1623
1624 output->background =
1625 background_create(output, option_background);
1626
1627 output->flags = flags;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001628 output->finished = 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001629 wlsc_output_move(output, x, y);
1630
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001631 output->object.interface = &wl_output_interface;
1632 wl_display_add_object(c->wl_display, &output->object);
1633 wl_display_add_global(c->wl_display, &output->object,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001634 wlsc_output_post_geometry);
1635}
1636
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001637static void
1638shm_buffer_created(struct wl_buffer *buffer)
1639{
1640 struct wl_list *surfaces_attached_to;
1641
1642 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1643 if (!surfaces_attached_to) {
1644 buffer->user_data = NULL;
1645 return;
1646 }
1647
1648 wl_list_init(surfaces_attached_to);
1649
1650 buffer->user_data = surfaces_attached_to;
1651}
1652
1653static void
1654shm_buffer_damaged(struct wl_buffer *buffer,
1655 int32_t x, int32_t y, int32_t width, int32_t height)
1656{
1657 struct wl_list *surfaces_attached_to = buffer->user_data;
1658 struct wlsc_surface *es;
Benjamin Franzkefab5ec12011-04-25 19:44:47 +02001659 GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001660
1661 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1662 glBindTexture(GL_TEXTURE_2D, es->texture);
1663 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Benjamin Franzkefab5ec12011-04-25 19:44:47 +02001664 tex_width, buffer->height, 0,
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001665 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1666 wl_shm_buffer_get_data(buffer));
1667 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
1668 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
1669 }
1670}
1671
1672static void
1673shm_buffer_destroyed(struct wl_buffer *buffer)
1674{
1675 struct wl_list *surfaces_attached_to = buffer->user_data;
1676 struct wlsc_surface *es, *next;
1677
1678 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001679 wl_list_remove(&es->buffer_link);
Kristian Høgsbergfac11d22011-05-02 13:35:17 -04001680 wl_list_init(&es->buffer_link);
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001681 }
1682
1683 free(surfaces_attached_to);
1684}
1685
1686const static struct wl_shm_callbacks shm_callbacks = {
1687 shm_buffer_created,
1688 shm_buffer_damaged,
1689 shm_buffer_destroyed
1690};
1691
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001692WL_EXPORT int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001693wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1694{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001695 struct wl_event_loop *loop;
Kristian Høgsbergd711d0c2011-01-14 17:28:21 -05001696 const char *extensions;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001697
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001698 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001699
Kristian Høgsbergc5c510e2010-12-08 15:12:58 -05001700 wl_compositor_init(&ec->compositor, &compositor_interface, display);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001701
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001702 ec->shm = wl_shm_init(display, &shm_callbacks);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04001703
1704 ec->image_target_texture_2d =
1705 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1706 ec->image_target_renderbuffer_storage = (void *)
1707 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1708 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1709 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1710 ec->bind_display =
1711 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1712 ec->unbind_display =
1713 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1714
1715 extensions = (const char *) glGetString(GL_EXTENSIONS);
1716 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1717 fprintf(stderr,
1718 "GL_EXT_texture_format_BGRA8888 not available\n");
1719 return -1;
1720 }
1721
1722 extensions =
1723 (const char *) eglQueryString(ec->display, EGL_EXTENSIONS);
1724 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1725 ec->has_bind_display = 1;
1726 if (ec->has_bind_display)
1727 ec->bind_display(ec->display, ec->wl_display);
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04001728
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001729 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001730 wl_list_init(&ec->input_device_list);
1731 wl_list_init(&ec->output_list);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001732 wl_list_init(&ec->binding_list);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001733 wl_list_init(&ec->animation_list);
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001734 wlsc_spring_init(&ec->fade.spring, 0.8, 0.0, 0.0);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001735 ec->fade.animation.frame = fade_frame;
1736 wl_list_init(&ec->fade.animation.link);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001737
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001738 wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0,
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001739 MODIFIER_CTRL | MODIFIER_ALT,
1740 terminate_binding, ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001741
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001742 create_pointer_images(ec);
1743
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001744 screenshooter_create(ec);
1745
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001746 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001747
1748 if (wlsc_shader_init(&ec->texture_shader,
1749 vertex_shader, texture_fragment_shader) < 0)
Kristian Høgsberga9468212010-06-14 21:03:11 -04001750 return -1;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001751 if (init_solid_shader(&ec->solid_shader,
1752 ec->texture_shader.vertex_shader,
1753 solid_fragment_shader) < 0)
1754 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001755
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001756 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001757 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
1758 wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000);
1759
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001760 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001761 pixman_region32_init(&ec->damage_region);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001762 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001763
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001764 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001765}
1766
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001767static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001768{
1769 struct wlsc_compositor *ec = data;
1770
1771 wl_display_terminate(ec->wl_display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001772
1773 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001774}
1775
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001776static void *
1777load_module(const char *name, const char *entrypoint, void **handle)
1778{
1779 char path[PATH_MAX];
1780 void *module, *init;
1781
1782 if (name[0] != '/')
1783 snprintf(path, sizeof path, MODULEDIR "/%s", name);
1784 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02001785 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001786
1787 module = dlopen(path, RTLD_LAZY);
1788 if (!module) {
1789 fprintf(stderr,
1790 "failed to load module: %s\n", dlerror());
1791 return NULL;
1792 }
1793
1794 init = dlsym(module, entrypoint);
1795 if (!init) {
1796 fprintf(stderr,
1797 "failed to lookup init function: %s\n", dlerror());
1798 return NULL;
1799 }
1800
1801 return init;
1802}
1803
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001804int main(int argc, char *argv[])
1805{
1806 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001807 struct wlsc_compositor *ec;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001808 struct wl_event_loop *loop;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001809 int o;
1810 void *shell_module, *backend_module;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001811 int (*shell_init)(struct wlsc_compositor *ec);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001812 struct wlsc_compositor
1813 *(*backend_init)(struct wl_display *display, char *options);
1814 char *backend = NULL;
1815 char *backend_options = "";
1816 char *shell = NULL;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04001817 char *p;
1818
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001819 static const char opts[] = "B:b:o:S:i:s:";
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04001820 static const struct option longopts[ ] = {
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001821 { "backend", 1, NULL, 'B' },
1822 { "backend-options", 1, NULL, 'o' },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04001823 { "background", 1, NULL, 'b' },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04001824 { "socket", 1, NULL, 'S' },
1825 { "idle-time", 1, NULL, 'i' },
1826 { "shell", 1, NULL, 's' },
1827 { NULL, }
1828 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001829
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04001830 while (o = getopt_long(argc, argv, opts, longopts, &o), o > 0) {
1831 switch (o) {
1832 case 'b':
1833 option_background = optarg;
1834 break;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001835 case 'B':
1836 backend = optarg;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04001837 break;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001838 case 'o':
1839 backend_options = optarg;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04001840 break;
1841 case 'S':
1842 option_socket_name = optarg;
1843 break;
1844 case 'i':
1845 option_idle_time = strtol(optarg, &p, 0);
1846 if (*p != '\0') {
1847 fprintf(stderr,
1848 "invalid idle time option: %s\n",
1849 optarg);
1850 exit(EXIT_FAILURE);
1851 }
1852 break;
1853 case 's':
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001854 shell = optarg;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04001855 break;
1856 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001857 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001858
1859 display = wl_display_create();
1860
Kristian Høgsberga9410222011-01-14 17:22:35 -05001861 ec = NULL;
1862
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001863 if (!backend) {
1864 if (getenv("WAYLAND_DISPLAY"))
1865 backend = "wayland-backend.so";
1866 else if (getenv("DISPLAY"))
1867 backend = "x11-backend.so";
1868 else if (getenv("OPENWFD"))
1869 backend = "openwfd-backend.so";
1870 else
1871 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001872 }
1873
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001874 if (!shell)
1875 shell = "desktop-shell.so";
Kristian Høgsberga9410222011-01-14 17:22:35 -05001876
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001877 backend_init = load_module(backend, "backend_init", &backend_module);
1878 if (!backend_init)
1879 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05001880
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001881 shell_init = load_module(shell, "shell_init", &shell_module);
1882 if (!shell_init)
1883 exit(EXIT_FAILURE);
Benjamin Franzke5d007092011-04-04 00:30:25 +02001884
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001885 ec = backend_init(display, backend_options);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001886 if (ec == NULL) {
1887 fprintf(stderr, "failed to create compositor\n");
1888 exit(EXIT_FAILURE);
1889 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001890
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001891 if (shell_init(ec) < 0)
1892 exit(EXIT_FAILURE);
1893
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001894 if (wl_display_add_socket(display, option_socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001895 fprintf(stderr, "failed to add socket: %m\n");
1896 exit(EXIT_FAILURE);
1897 }
1898
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001899 loop = wl_display_get_event_loop(ec->wl_display);
1900 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
1901 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec);
1902
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001903 wl_display_run(display);
1904
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04001905 if (ec->has_bind_display)
1906 ec->unbind_display(ec->display, display);
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001907 wl_display_destroy(display);
1908
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001909 ec->destroy(ec);
1910
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001911 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001912}