blob: 6288337edb4b77283ec7140a09ee7f615198e472 [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øgsberg02ec0a52011-04-23 13:04:11 -040046static const char *option_shell = NULL;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -040047static int option_idle_time = 300;
Kristian Høgsberg61a82512010-10-26 11:26:44 -040048static int option_connector = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050049
Kristian Høgsbergd34912c2011-05-02 10:36:04 -040050WL_EXPORT void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050051wlsc_matrix_init(struct wlsc_matrix *matrix)
52{
53 static const struct wlsc_matrix identity = {
54 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
55 };
56
57 memcpy(matrix, &identity, sizeof identity);
58}
59
60static void
61wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
62{
63 struct wlsc_matrix tmp;
Kristian Høgsberg27803c62010-06-06 22:23:21 -040064 const GLfloat *row, *column;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050065 div_t d;
66 int i, j;
67
68 for (i = 0; i < 16; i++) {
69 tmp.d[i] = 0;
70 d = div(i, 4);
71 row = m->d + d.quot * 4;
72 column = n->d + d.rem;
73 for (j = 0; j < 4; j++)
74 tmp.d[i] += row[j] * column[j * 4];
75 }
76 memcpy(m, &tmp, sizeof tmp);
77}
78
79static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040080wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050081{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -050082 struct wlsc_matrix translate = {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050083 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
84 };
85
86 wlsc_matrix_multiply(matrix, &translate);
87}
88
89static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040090wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050091{
92 struct wlsc_matrix scale = {
93 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
94 };
95
96 wlsc_matrix_multiply(matrix, &scale);
97}
98
99static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400100wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
101{
102 int i, j;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400103 struct wlsc_vector t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400104
105 for (i = 0; i < 4; i++) {
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400106 t.f[i] = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400107 for (j = 0; j < 4; j++)
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400108 t.f[i] += v->f[j] * matrix->d[i + j * 4];
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400109 }
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500110
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400111 *v = t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400112}
113
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400114WL_EXPORT void
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400115wlsc_tweener_init(struct wlsc_tweener *tweener,
116 double k, double current, double target)
117{
118 tweener->k = k;
119 tweener->current = current;
120 tweener->previous = current;
121 tweener->target = target;
122}
123
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400124WL_EXPORT void
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400125wlsc_tweener_update(struct wlsc_tweener *tweener, uint32_t msec)
126{
127 double force, current, step;
128
129 step = (msec - tweener->timestamp) / 100.0;
130 tweener->timestamp = msec;
131
132 current = tweener->current;
133 force = tweener->k * (tweener->target - current) / 10.0 +
134 (tweener->previous - current);
135
136 tweener->current =
137 current + (current - tweener->previous) + force * step * step;
138 tweener->previous = current;
139
140 if (tweener->current >= 1.0) {
141 tweener->current = 1.0;
142 tweener->previous = 1.0;
143 }
144
145 if (tweener->current <= 0.0) {
146 tweener->current = 0.0;
147 tweener->previous = 0.0;
148 }
149}
150
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400151WL_EXPORT int
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400152wlsc_tweener_done(struct wlsc_tweener *tweener)
153{
154 return fabs(tweener->previous - tweener->target) < 0.0002 &&
155 fabs(tweener->current - tweener->target) < 0.0002;
156}
157
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400158WL_EXPORT struct wlsc_surface *
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400159wlsc_surface_create(struct wlsc_compositor *compositor,
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400160 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500161{
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400162 struct wlsc_surface *surface;
163
164 surface = malloc(sizeof *surface);
165 if (surface == NULL)
166 return NULL;
167
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500168 wl_list_init(&surface->surface.destroy_listener_list);
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500169 wl_list_init(&surface->link);
Benjamin Franzkebab41fb2011-03-07 18:04:59 +0100170 wl_list_init(&surface->buffer_link);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500171 surface->map_type = WLSC_SURFACE_MAP_UNMAPPED;
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500172
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500173 glGenTextures(1, &surface->texture);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400174 glBindTexture(GL_TEXTURE_2D, surface->texture);
175 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
176 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
177 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
178 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
179
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500180 surface->compositor = compositor;
Kristian Høgsbergc5d6be92011-01-14 16:22:37 -0500181 surface->visual = NULL;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200182 surface->image = EGL_NO_IMAGE_KHR;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200183 surface->saved_texture = 0;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400184 surface->x = x;
185 surface->y = y;
186 surface->width = width;
187 surface->height = height;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400188 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400189 wlsc_matrix_scale(&surface->matrix, width, height, 1);
190 wlsc_matrix_translate(&surface->matrix, x, y, 0);
191
192 wlsc_matrix_init(&surface->matrix_inv);
193 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
194 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500195
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400196 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500197}
198
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400199WL_EXPORT void
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500200wlsc_surface_damage_rectangle(struct wlsc_surface *surface,
201 int32_t x, int32_t y,
202 int32_t width, int32_t height)
203{
204 struct wlsc_compositor *compositor = surface->compositor;
205
206 pixman_region32_union_rect(&compositor->damage_region,
207 &compositor->damage_region,
208 surface->x + x, surface->y + y,
209 width, height);
210 wlsc_compositor_schedule_repaint(compositor);
211}
212
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400213WL_EXPORT void
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500214wlsc_surface_damage(struct wlsc_surface *surface)
215{
216 wlsc_surface_damage_rectangle(surface, 0, 0,
217 surface->width, surface->height);
218}
219
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400220WL_EXPORT uint32_t
221wlsc_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500222{
223 struct timeval tv;
224
225 gettimeofday(&tv, NULL);
226
227 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
228}
229
Kristian Høgsberg54879822008-11-23 17:07:32 -0500230static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400231destroy_surface(struct wl_resource *resource, struct wl_client *client)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500232{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400233 struct wlsc_surface *surface =
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500234 container_of(resource, struct wlsc_surface, surface.resource);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500235 struct wl_listener *l, *next;
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400236 struct wlsc_compositor *compositor = surface->compositor;
Kristian Høgsberg4685fa32010-12-06 21:38:50 -0500237 uint32_t time;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400238
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500239 wlsc_surface_damage(surface);
240
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400241 wl_list_remove(&surface->link);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200242 if (surface->saved_texture == 0)
243 glDeleteTextures(1, &surface->texture);
244 else
245 glDeleteTextures(1, &surface->saved_texture);
246
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400247
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200248 if (surface->image != EGL_NO_IMAGE_KHR)
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400249 compositor->destroy_image(compositor->display,
250 surface->image);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200251
Benjamin Franzkebab41fb2011-03-07 18:04:59 +0100252 wl_list_remove(&surface->buffer_link);
253
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400254 time = wlsc_compositor_get_time();
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500255 wl_list_for_each_safe(l, next,
256 &surface->surface.destroy_listener_list, link)
Kristian Høgsberg4685fa32010-12-06 21:38:50 -0500257 l->func(l, &surface->surface, time);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400258
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400259 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500260}
261
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100262static void
263wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
264{
265 struct wlsc_surface *es = (struct wlsc_surface *) surface;
266 struct wlsc_compositor *ec = es->compositor;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100267 struct wl_list *surfaces_attached_to;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100268
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100269 if (es->saved_texture != 0)
270 es->texture = es->saved_texture;
271
272 glBindTexture(GL_TEXTURE_2D, es->texture);
273
274 if (wl_buffer_is_shm(buffer)) {
275 /* Unbind any EGLImage texture that may be bound, so we don't
276 * overwrite it.*/
277 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
278 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200279 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100280 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200281 es->pitch, buffer->height, 0,
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100282 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
283 wl_shm_buffer_get_data(buffer));
284 es->visual = buffer->visual;
285
286 surfaces_attached_to = buffer->user_data;
287
Kristian Høgsbergfac11d22011-05-02 13:35:17 -0400288 wl_list_remove(&es->buffer_link);
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100289 wl_list_insert(surfaces_attached_to, &es->buffer_link);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100290 } else {
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400291 es->image = ec->create_image(ec->display, NULL,
292 EGL_WAYLAND_BUFFER_WL,
293 buffer, NULL);
294
295 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100296 es->visual = buffer->visual;
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200297 es->pitch = es->width;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100298 }
299}
300
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200301static void
302wlsc_sprite_attach(struct wlsc_sprite *sprite, struct wl_surface *surface)
303{
304 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400305 struct wlsc_compositor *ec = es->compositor;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200306
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200307 es->pitch = es->width;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200308 es->image = sprite->image;
309 if (sprite->image != EGL_NO_IMAGE_KHR) {
310 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400311 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200312 } else {
313 if (es->saved_texture == 0)
314 es->saved_texture = es->texture;
315 es->texture = sprite->texture;
316 }
317
318 es->visual = sprite->visual;
319}
320
321enum sprite_usage {
322 SPRITE_USE_CURSOR = (1 << 0),
323};
324
325static struct wlsc_sprite *
326create_sprite_from_png(struct wlsc_compositor *ec,
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400327 const char *filename, uint32_t usage)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500328{
329 uint32_t *pixels;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200330 struct wlsc_sprite *sprite;
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400331 int32_t width, height;
332 uint32_t stride;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500333
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400334 pixels = wlsc_load_image(filename, &width, &height, &stride);
335 if (pixels == NULL)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200336 return NULL;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500337
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200338 sprite = malloc(sizeof *sprite);
339 if (sprite == NULL) {
340 free(pixels);
341 return NULL;
342 }
343
344 sprite->visual = &ec->compositor.premultiplied_argb_visual;
345 sprite->width = width;
346 sprite->height = height;
347 sprite->image = EGL_NO_IMAGE_KHR;
348
349 if (usage & SPRITE_USE_CURSOR && ec->create_cursor_image != NULL)
350 sprite->image = ec->create_cursor_image(ec, width, height);
351
352 glGenTextures(1, &sprite->texture);
353 glBindTexture(GL_TEXTURE_2D, sprite->texture);
354 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
355 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
356 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
357 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
358
359 if (sprite->image != EGL_NO_IMAGE_KHR) {
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400360 ec->image_target_texture_2d(GL_TEXTURE_2D, sprite->image);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200361 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
362 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
363 } else {
364 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, width, height, 0,
365 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
366 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500367
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500368 free(pixels);
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500369
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200370 return sprite;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500371}
372
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400373static const struct {
374 const char *filename;
375 int hotspot_x, hotspot_y;
376} pointer_images[] = {
Kristian Høgsberg4219a402010-08-16 16:43:03 -0400377 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
378 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
379 { DATADIR "/wayland/bottom_side.png", 16, 20 },
380 { DATADIR "/wayland/grabbing.png", 20, 17 },
381 { DATADIR "/wayland/left_ptr.png", 10, 5 },
382 { DATADIR "/wayland/left_side.png", 10, 20 },
383 { DATADIR "/wayland/right_side.png", 30, 19 },
384 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
385 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
386 { DATADIR "/wayland/top_side.png", 18, 8 },
387 { DATADIR "/wayland/xterm.png", 15, 15 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400388};
389
390static void
391create_pointer_images(struct wlsc_compositor *ec)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500392{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400393 int i, count;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400394
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400395 count = ARRAY_LENGTH(pointer_images);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200396 ec->pointer_sprites = malloc(count * sizeof *ec->pointer_sprites);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400397 for (i = 0; i < count; i++) {
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200398 ec->pointer_sprites[i] =
399 create_sprite_from_png(ec,
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500400 pointer_images[i].filename,
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200401 SPRITE_USE_CURSOR);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400402 }
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400403}
404
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500405static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500406background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500407{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500408 struct wlsc_surface *background;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200409 struct wlsc_sprite *sprite;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500410
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400411 background = wlsc_surface_create(output->compositor,
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400412 output->x, output->y,
413 output->width, output->height);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500414 if (background == NULL)
415 return NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500416
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400417 sprite = create_sprite_from_png(output->compositor, filename, 0);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200418 if (sprite == NULL) {
Benjamin Franzked3b023e2011-01-15 12:34:49 +0100419 free(background);
420 return NULL;
421 }
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100422
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200423 wlsc_sprite_attach(sprite, &background->surface);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500424
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500425 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500426}
427
428static void
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500429wlsc_surface_draw(struct wlsc_surface *es,
430 struct wlsc_output *output, pixman_region32_t *clip)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500431{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500432 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500433 GLfloat *v, inv_width, inv_height;
434 unsigned int *p;
435 pixman_region32_t repaint;
436 pixman_box32_t *rectangles;
437 int i, n;
438
439 pixman_region32_init_rect(&repaint,
440 es->x, es->y, es->width, es->height);
441 pixman_region32_intersect(&repaint, &repaint, clip);
442 if (!pixman_region32_not_empty(&repaint))
443 return;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500444
Kristian Høgsbergc5c510e2010-12-08 15:12:58 -0500445 if (es->visual == &ec->compositor.argb_visual) {
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500446 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
447 glEnable(GL_BLEND);
Kristian Høgsbergc5c510e2010-12-08 15:12:58 -0500448 } else if (es->visual == &ec->compositor.premultiplied_argb_visual) {
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500449 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
450 glEnable(GL_BLEND);
451 } else {
452 glDisable(GL_BLEND);
453 }
454
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500455 rectangles = pixman_region32_rectangles(&repaint, &n);
456 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
457 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200458 inv_width = 1.0 / es->pitch;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500459 inv_height = 1.0 / es->height;
460 for (i = 0; i < n; i++, v += 16, p += 6) {
461 v[ 0] = rectangles[i].x1;
462 v[ 1] = rectangles[i].y1;
463 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
464 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500465
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500466 v[ 4] = rectangles[i].x1;
467 v[ 5] = rectangles[i].y2;
468 v[ 6] = v[ 2];
469 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500470
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500471 v[ 8] = rectangles[i].x2;
472 v[ 9] = rectangles[i].y1;
473 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
474 v[11] = v[ 3];
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500475
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500476 v[12] = rectangles[i].x2;
477 v[13] = rectangles[i].y2;
478 v[14] = v[10];
479 v[15] = v[ 7];
480
481 p[0] = i * 4 + 0;
482 p[1] = i * 4 + 1;
483 p[2] = i * 4 + 2;
484 p[3] = i * 4 + 2;
485 p[4] = i * 4 + 1;
486 p[5] = i * 4 + 3;
487 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500488
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500489 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500490 v = ec->vertices.data;
491 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
492 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400493 glEnableVertexAttribArray(0);
494 glEnableVertexAttribArray(1);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500495 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
496
497 ec->vertices.size = 0;
498 ec->indices.size = 0;
499 pixman_region32_fini(&repaint);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500500}
501
502static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400503wlsc_surface_raise(struct wlsc_surface *surface)
504{
505 struct wlsc_compositor *compositor = surface->compositor;
506
507 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400508 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400509}
510
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400511WL_EXPORT void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400512wlsc_surface_update_matrix(struct wlsc_surface *es)
513{
514 wlsc_matrix_init(&es->matrix);
515 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
516 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
517
518 wlsc_matrix_init(&es->matrix_inv);
519 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
520 wlsc_matrix_scale(&es->matrix_inv,
521 1.0 / es->width, 1.0 / es->height, 1);
522}
523
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400524WL_EXPORT void
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400525wlsc_compositor_damage_all(struct wlsc_compositor *compositor)
526{
527 struct wlsc_output *output;
528
529 wl_list_for_each(output, &compositor->output_list, link)
530 wlsc_output_damage(output);
531}
532
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400533WL_EXPORT void
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100534wlsc_output_finish_frame(struct wlsc_output *output, int msecs)
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400535{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100536 struct wlsc_compositor *compositor = output->compositor;
537 struct wlsc_surface *es;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400538 struct wlsc_animation *animation, *next;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100539
540 wl_list_for_each(es, &compositor->surface_list, link) {
541 if (es->output == output) {
542 wl_display_post_frame(compositor->wl_display,
543 &es->surface, msecs);
544 }
545 }
546
547 output->finished = 1;
548
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400549 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsbergf30c67e2011-02-06 12:58:44 -0500550 compositor->repaint_on_timeout = 1;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400551
552 wl_list_for_each_safe(animation, next,
553 &compositor->animation_list, link)
554 animation->frame(animation, output, msecs);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400555}
556
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400557WL_EXPORT void
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400558wlsc_output_damage(struct wlsc_output *output)
559{
560 struct wlsc_compositor *compositor = output->compositor;
561
562 pixman_region32_union_rect(&compositor->damage_region,
563 &compositor->damage_region,
564 output->x, output->y,
565 output->width, output->height);
566 wlsc_compositor_schedule_repaint(compositor);
567}
568
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400569static void
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400570fade_frame(struct wlsc_animation *animation,
571 struct wlsc_output *output, uint32_t msecs)
572{
573 struct wlsc_compositor *compositor =
574 container_of(animation,
575 struct wlsc_compositor, fade.animation);
576
577 wlsc_tweener_update(&compositor->fade.tweener, msecs);
578 if (wlsc_tweener_done(&compositor->fade.tweener)) {
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400579 if (compositor->fade.tweener.current > 0.999) {
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400580 compositor->state = WLSC_COMPOSITOR_SLEEPING;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400581 compositor->shell->lock(compositor->shell);
582 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400583 compositor->fade.tweener.current =
584 compositor->fade.tweener.target;
585 wl_list_remove(&animation->link);
586 wl_list_init(&animation->link);
587 }
588
589 wlsc_output_damage(output);
590}
591
592static void
593fade_output(struct wlsc_output *output,
594 GLfloat tint, pixman_region32_t *region)
595{
596 struct wlsc_compositor *compositor = output->compositor;
597 struct wlsc_surface surface;
598 GLfloat color[4] = { 0.0, 0.0, 0.0, tint };
599
600 surface.compositor = compositor;
601 surface.x = output->x;
602 surface.y = output->y;
603 surface.width = output->width;
604 surface.height = output->height;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400605 surface.texture = GL_NONE;
Kristian Høgsbergc352ab02011-04-23 15:34:50 -0400606
607 if (tint <= 1.0)
608 surface.visual =
609 &compositor->compositor.premultiplied_argb_visual;
610 else
611 surface.visual = &compositor->compositor.rgb_visual;
612
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400613 glUseProgram(compositor->solid_shader.program);
614 glUniformMatrix4fv(compositor->solid_shader.proj_uniform,
615 1, GL_FALSE, output->matrix.d);
616 glUniform4fv(compositor->solid_shader.color_uniform, 1, color);
617 wlsc_surface_draw(&surface, output, region);
618}
619
620static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400621wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400622{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500623 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500624 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500625 struct wlsc_input_device *eid;
Kristian Høgsbergcfc6d272011-04-11 13:34:24 -0400626 pixman_region32_t new_damage, total_damage, repaint;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200627 int using_hardware_cursor = 1;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500628
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100629 output->prepare_render(output);
630
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500631 glViewport(0, 0, output->width, output->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500632
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -0400633 glUseProgram(ec->texture_shader.program);
634 glUniformMatrix4fv(ec->texture_shader.proj_uniform,
635 1, GL_FALSE, output->matrix.d);
636 glUniform1i(ec->texture_shader.tex_uniform, 0);
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500637
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500638 pixman_region32_init(&new_damage);
639 pixman_region32_init(&total_damage);
640 pixman_region32_intersect_rect(&new_damage,
641 &ec->damage_region,
642 output->x, output->y,
643 output->width, output->height);
644 pixman_region32_subtract(&ec->damage_region,
645 &ec->damage_region, &new_damage);
646 pixman_region32_union(&total_damage, &new_damage,
647 &output->previous_damage_region);
648 pixman_region32_copy(&output->previous_damage_region, &new_damage);
649
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200650 if (ec->focus)
651 if (output->set_hardware_cursor(output, ec->input_device) < 0)
652 using_hardware_cursor = 0;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400653 if (ec->fade.tweener.current > 0.001)
654 using_hardware_cursor = 0;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200655
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500656 es = container_of(ec->surface_list.next, struct wlsc_surface, link);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400657 if (es->fullscreen_output == output) {
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200658 if (es->visual == &ec->compositor.rgb_visual &&
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200659 using_hardware_cursor) {
660 if (output->prepare_scanout_surface(output, es) == 0) {
661 /* We're drawing nothing now,
662 * draw the damaged regions later. */
663 pixman_region32_union(&ec->damage_region,
664 &ec->damage_region,
665 &total_damage);
666 return;
667 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200668 }
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200669
670 if (es->width < output->width ||
671 es->height < output->height)
672 glClear(GL_COLOR_BUFFER_BIT);
673 wlsc_surface_draw(es, output, &total_damage);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500674 } else {
Kristian Høgsbergcfc6d272011-04-11 13:34:24 -0400675 wl_list_for_each(es, &ec->surface_list, link) {
676 if (es->visual != &ec->compositor.rgb_visual)
677 continue;
678
679 pixman_region32_init_rect(&repaint,
680 es->x, es->y,
681 es->width, es->height);
682 wlsc_surface_draw(es, output, &total_damage);
683 pixman_region32_subtract(&total_damage,
684 &total_damage, &repaint);
685 }
686
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500687 if (output->background)
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500688 wlsc_surface_draw(output->background,
689 output, &total_damage);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500690 else
Kristian Høgsbergc352ab02011-04-23 15:34:50 -0400691 fade_output(output, 1.0, &total_damage);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500692
Kristian Høgsbergc352ab02011-04-23 15:34:50 -0400693 glUseProgram(ec->texture_shader.program);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500694 wl_list_for_each_reverse(es, &ec->surface_list, link) {
Kristian Høgsberg547cadf2011-04-12 22:23:30 -0400695 if (ec->overlay == es)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500696 continue;
697
Kristian Høgsbergcfc6d272011-04-11 13:34:24 -0400698 if (es->visual == &ec->compositor.rgb_visual) {
699 pixman_region32_union_rect(&total_damage,
700 &total_damage,
701 es->x, es->y,
702 es->width, es->height);
703 continue;
704 }
705
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500706 wlsc_surface_draw(es, output, &total_damage);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500707 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500708 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400709
Kristian Høgsberg547cadf2011-04-12 22:23:30 -0400710 if (ec->overlay)
711 wlsc_surface_draw(ec->overlay, output, &total_damage);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500712
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400713 if (ec->focus)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200714 wl_list_for_each(eid, &ec->input_device_list, link) {
715 if (&eid->input_device != ec->input_device ||
716 !using_hardware_cursor)
717 wlsc_surface_draw(eid->sprite, output,
718 &total_damage);
719 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400720
721 if (ec->fade.tweener.current > 0.001)
722 fade_output(output, ec->fade.tweener.current, &total_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500723}
724
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400725static int
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500726repaint(void *data)
727{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500728 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500729 struct wlsc_output *output;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100730 int repainted_all_outputs = 1;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500731
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100732 wl_list_for_each(output, &ec->output_list, link) {
733 if (!output->repaint_needed)
734 continue;
735
736 if (!output->finished) {
737 repainted_all_outputs = 0;
738 continue;
739 }
740
741 wlsc_output_repaint(output);
742 output->finished = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100743 output->repaint_needed = 0;
Kristian Høgsberg11e28282011-04-11 16:47:50 -0400744 output->present(output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500745 }
746
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100747 if (repainted_all_outputs)
748 ec->repaint_on_timeout = 0;
749 else
750 wl_event_source_timer_update(ec->timer_source, 1);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400751
752 return 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400753}
754
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400755WL_EXPORT void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400756wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400757{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100758 struct wlsc_output *output;
759
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400760 if (compositor->state == WLSC_COMPOSITOR_SLEEPING)
761 return;
762
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100763 wl_list_for_each(output, &compositor->output_list, link)
764 output->repaint_needed = 1;
765
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400766 if (compositor->repaint_on_timeout)
767 return;
768
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400769 wl_event_source_timer_update(compositor->timer_source, 1);
770 compositor->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400771}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500772
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400773WL_EXPORT void
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400774wlsc_compositor_fade(struct wlsc_compositor *compositor, float tint)
775{
776 int done;
777
778 done = wlsc_tweener_done(&compositor->fade.tweener);
779 compositor->fade.tweener.target = tint;
780 if (wlsc_tweener_done(&compositor->fade.tweener))
781 return;
782
783 if (done)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400784 compositor->fade.tweener.timestamp =
785 wlsc_compositor_get_time();
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400786
787 wlsc_compositor_damage_all(compositor);
788 if (wl_list_empty(&compositor->fade.animation.link))
789 wl_list_insert(compositor->animation_list.prev,
790 &compositor->fade.animation.link);
791}
792
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500793static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500794surface_destroy(struct wl_client *client,
795 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400796{
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500797 wl_resource_destroy(&surface->resource, client);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400798}
799
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400800WL_EXPORT void
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100801wlsc_surface_assign_output(struct wlsc_surface *es)
802{
803 struct wlsc_compositor *ec = es->compositor;
804 struct wlsc_output *output;
805
806 struct wlsc_output *tmp = es->output;
807 es->output = NULL;
808
809 wl_list_for_each(output, &ec->output_list, link) {
810 if (output->x < es->x && es->x < output->x + output->width &&
811 output->y < es->y && es->y < output->y + output->height) {
812 if (output != tmp)
813 printf("assiging surface %p to output %p\n",
814 es, output);
815 es->output = output;
816 }
817 }
818
819 if (es->output == NULL) {
820 printf("no output found\n");
821 es->output = container_of(ec->output_list.next,
822 struct wlsc_output, link);
823 }
824}
825
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500826static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500827surface_attach(struct wl_client *client,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500828 struct wl_surface *surface, struct wl_buffer *buffer,
829 int32_t x, int32_t y)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400830{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500831 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400832
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500833 /* FIXME: This damages the entire old surface, but we should
834 * really just damage the part that's no longer covered by the
835 * surface. Anything covered by the new surface will be
836 * damaged by the client. */
837 wlsc_surface_damage(es);
838
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100839 wlsc_buffer_attach(buffer, surface);
840
Benjamin Franzke0b5116f2011-04-26 15:36:26 +0200841 switch (es->map_type) {
842 case WLSC_SURFACE_MAP_FULLSCREEN:
843 es->x = (es->fullscreen_output->width - es->width) / 2;
844 es->y = (es->fullscreen_output->height - es->height) / 2;
845 break;
846 default:
847 es->x += x;
848 es->y += y;
849 break;
850 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500851 es->width = buffer->width;
852 es->height = buffer->height;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100853 if (x != 0 || y != 0)
854 wlsc_surface_assign_output(es);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500855 wlsc_surface_update_matrix(es);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400856
857 es->compositor->shell->attach(es->compositor->shell, es);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400858}
859
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500860static void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500861surface_map_toplevel(struct wl_client *client,
862 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400863{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500864 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100865 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400866
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500867 switch (es->map_type) {
868 case WLSC_SURFACE_MAP_UNMAPPED:
869 es->x = 10 + random() % 400;
870 es->y = 10 + random() % 400;
871 wlsc_surface_update_matrix(es);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100872 /* assign to first output */
873 es->output = container_of(ec->output_list.next,
874 struct wlsc_output, link);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500875 wl_list_insert(&es->compositor->surface_list, &es->link);
876 break;
877 case WLSC_SURFACE_MAP_TOPLEVEL:
Kristian Høgsberg8f66a572011-01-07 08:38:56 -0500878 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500879 case WLSC_SURFACE_MAP_FULLSCREEN:
880 es->fullscreen_output = NULL;
881 es->x = es->saved_x;
882 es->y = es->saved_y;
883 wlsc_surface_update_matrix(es);
884 break;
885 default:
886 break;
887 }
Kristian Høgsberg8f66a572011-01-07 08:38:56 -0500888
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500889 wlsc_surface_damage(es);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500890 es->map_type = WLSC_SURFACE_MAP_TOPLEVEL;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400891}
892
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500893static void
Kristian Høgsberg8dc378f2011-01-21 18:02:24 -0500894surface_map_transient(struct wl_client *client,
895 struct wl_surface *surface, struct wl_surface *parent,
896 int x, int y, uint32_t flags)
897{
898 struct wlsc_surface *es = (struct wlsc_surface *) surface;
899 struct wlsc_surface *pes = (struct wlsc_surface *) parent;
900
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500901 switch (es->map_type) {
902 case WLSC_SURFACE_MAP_UNMAPPED:
903 wl_list_insert(&es->compositor->surface_list, &es->link);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100904 /* assign to parents output */
905 es->output = pes->output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500906 break;
907 case WLSC_SURFACE_MAP_FULLSCREEN:
908 es->fullscreen_output = NULL;
909 break;
910 default:
911 break;
912 }
Kristian Høgsberg8dc378f2011-01-21 18:02:24 -0500913
914 es->x = pes->x + x;
915 es->y = pes->y + y;
916
917 wlsc_surface_update_matrix(es);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500918 wlsc_surface_damage(es);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500919 es->map_type = WLSC_SURFACE_MAP_TRANSIENT;
920}
921
922static void
923surface_map_fullscreen(struct wl_client *client, struct wl_surface *surface)
924{
925 struct wlsc_surface *es = (struct wlsc_surface *) surface;
926 struct wlsc_output *output;
927
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100928 /* FIXME: Fullscreen on first output */
929 /* FIXME: Handle output going away */
930 output = container_of(es->compositor->output_list.next,
931 struct wlsc_output, link);
932
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500933 switch (es->map_type) {
934 case WLSC_SURFACE_MAP_UNMAPPED:
935 es->x = 10 + random() % 400;
936 es->y = 10 + random() % 400;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100937 /* assign to first output */
938 es->output = output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500939 wl_list_insert(&es->compositor->surface_list, &es->link);
940 break;
941 case WLSC_SURFACE_MAP_FULLSCREEN:
942 return;
943 default:
944 break;
945 }
946
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500947 es->saved_x = es->x;
948 es->saved_y = es->y;
949 es->x = (output->width - es->width) / 2;
950 es->y = (output->height - es->height) / 2;
951 es->fullscreen_output = output;
952 wlsc_surface_update_matrix(es);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500953 wlsc_surface_damage(es);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500954 es->map_type = WLSC_SURFACE_MAP_FULLSCREEN;
Kristian Høgsberg8dc378f2011-01-21 18:02:24 -0500955}
956
957static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500958surface_damage(struct wl_client *client,
959 struct wl_surface *surface,
960 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500961{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400962 struct wlsc_surface *es = (struct wlsc_surface *) surface;
963
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500964 wlsc_surface_damage_rectangle(es, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500965}
966
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500967const static struct wl_surface_interface surface_interface = {
968 surface_destroy,
969 surface_attach,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500970 surface_map_toplevel,
Kristian Høgsberg8dc378f2011-01-21 18:02:24 -0500971 surface_map_transient,
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500972 surface_map_fullscreen,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500973 surface_damage
974};
975
976static void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400977wlsc_input_device_attach(struct wlsc_input_device *device,
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200978 int x, int y, int width, int height)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400979{
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500980 wlsc_surface_damage(device->sprite);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400981
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400982 device->hotspot_x = x;
983 device->hotspot_y = y;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400984
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500985 device->sprite->x = device->input_device.x - device->hotspot_x;
986 device->sprite->y = device->input_device.y - device->hotspot_y;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200987 device->sprite->width = width;
988 device->sprite->height = height;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400989 wlsc_surface_update_matrix(device->sprite);
990
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500991 wlsc_surface_damage(device->sprite);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400992}
993
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200994static void
995wlsc_input_device_attach_buffer(struct wlsc_input_device *device,
996 struct wl_buffer *buffer, int x, int y)
997{
998 wlsc_buffer_attach(buffer, &device->sprite->surface);
999 wlsc_input_device_attach(device, x, y, buffer->width, buffer->height);
1000}
1001
1002static void
1003wlsc_input_device_attach_sprite(struct wlsc_input_device *device,
1004 struct wlsc_sprite *sprite, int x, int y)
1005{
1006 wlsc_sprite_attach(sprite, &device->sprite->surface);
1007 wlsc_input_device_attach(device, x, y, sprite->width, sprite->height);
1008}
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001009
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001010void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001011wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
1012 enum wlsc_pointer_type type)
1013{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001014 struct wlsc_compositor *compositor =
1015 (struct wlsc_compositor *) device->input_device.compositor;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001016
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001017 wlsc_input_device_attach_sprite(device,
1018 compositor->pointer_sprites[type],
1019 pointer_images[type].hotspot_x,
1020 pointer_images[type].hotspot_y);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001021}
1022
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001023static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001024compositor_create_surface(struct wl_client *client,
1025 struct wl_compositor *compositor, uint32_t id)
1026{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001027 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001028 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001029
Kristian Høgsbergc5d6be92011-01-14 16:22:37 -05001030 surface = wlsc_surface_create(ec, 0, 0, 0, 0);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001031 if (surface == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -04001032 wl_client_post_no_memory(client);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001033 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001034 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001035
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001036 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001037
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001038 surface->surface.resource.object.id = id;
1039 surface->surface.resource.object.interface = &wl_surface_interface;
1040 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001041 (void (**)(void)) &surface_interface;
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001042 surface->surface.client = client;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001043
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001044 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001045}
1046
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001047const static struct wl_compositor_interface compositor_interface = {
1048 compositor_create_surface,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001049};
1050
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001051static void
1052wlsc_surface_transform(struct wlsc_surface *surface,
1053 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1054{
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001055 struct wlsc_vector v = { { x, y, 0, 1 } };
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001056
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001057 wlsc_matrix_transform(&surface->matrix_inv, &v);
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -04001058 *sx = v.f[0] * surface->width;
1059 *sy = v.f[1] * surface->height;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001060}
1061
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001062struct wlsc_surface *
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001063pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001064{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001065 struct wlsc_compositor *ec =
1066 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001067 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001068
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001069 wl_list_for_each(es, &ec->surface_list, link) {
1070 wlsc_surface_transform(es, device->x, device->y, sx, sy);
1071 if (0 <= *sx && *sx < es->width &&
1072 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001073 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001074 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001075
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001076 return NULL;
1077}
1078
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001079
1080static void
1081motion_grab_motion(struct wl_grab *grab,
1082 uint32_t time, int32_t x, int32_t y)
1083{
1084 struct wlsc_input_device *device =
1085 (struct wlsc_input_device *) grab->input_device;
1086 struct wlsc_surface *es =
1087 (struct wlsc_surface *) device->input_device.pointer_focus;
1088 int32_t sx, sy;
1089
1090 wlsc_surface_transform(es, x, y, &sx, &sy);
1091 wl_client_post_event(es->surface.client,
1092 &device->input_device.object,
1093 WL_INPUT_DEVICE_MOTION,
1094 time, x, y, sx, sy);
1095}
1096
1097static void
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001098motion_grab_button(struct wl_grab *grab,
1099 uint32_t time, int32_t button, int32_t state)
1100{
1101 wl_client_post_event(grab->input_device->pointer_focus->client,
1102 &grab->input_device->object,
1103 WL_INPUT_DEVICE_BUTTON,
1104 time, button, state);
1105}
1106
1107static void
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001108motion_grab_end(struct wl_grab *grab, uint32_t time)
1109{
1110}
1111
1112static const struct wl_grab_interface motion_grab_interface = {
1113 motion_grab_motion,
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001114 motion_grab_button,
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001115 motion_grab_end
1116};
1117
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001118WL_EXPORT void
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001119wlsc_compositor_wake(struct wlsc_compositor *compositor)
1120{
1121 if (compositor->idle_inhibit)
1122 return;
1123
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001124 wlsc_compositor_fade(compositor, 0.0);
1125 compositor->state = WLSC_COMPOSITOR_ACTIVE;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001126
1127 wl_event_source_timer_update(compositor->idle_source,
1128 option_idle_time * 1000);
1129}
1130
1131static void
1132wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor)
1133{
1134 wlsc_compositor_wake(compositor);
1135 compositor->idle_inhibit++;
1136}
1137
1138static void
1139wlsc_compositor_idle_release(struct wlsc_compositor *compositor)
1140{
1141 compositor->idle_inhibit--;
1142 wlsc_compositor_wake(compositor);
1143}
1144
1145static int
1146idle_handler(void *data)
1147{
1148 struct wlsc_compositor *compositor = data;
1149
1150 if (compositor->idle_inhibit)
1151 return 1;
1152
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001153 wlsc_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001154
1155 return 1;
1156}
1157
1158void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001159notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001160{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001161 struct wlsc_surface *es;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001162 struct wlsc_compositor *ec =
1163 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001164 struct wlsc_output *output;
Kristian Høgsberg6d65d5f2010-12-07 13:30:18 -05001165 const struct wl_grab_interface *interface;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001166 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsbergfc9c28a2010-12-07 13:04:43 -05001167 int32_t sx, sy;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001168 int x_valid = 0, y_valid = 0;
1169 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 -05001170
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001171 wlsc_compositor_wake(ec);
1172
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001173 wl_list_for_each(output, &ec->output_list, link) {
1174 if (output->x <= x && x <= output->x + output->width)
1175 x_valid = 1;
1176
1177 if (output->y <= y && y <= output->y + output->height)
1178 y_valid = 1;
1179
1180 /* FIXME: calculate this only on output addition/deletion */
1181 if (output->x < min_x)
1182 min_x = output->x;
1183 if (output->y < min_y)
1184 min_y = output->y;
1185
1186 if (output->x + output->width > max_x)
1187 max_x = output->x + output->width;
1188 if (output->y + output->height > max_y)
1189 max_y = output->y + output->height;
1190 }
1191
1192 if (!x_valid) {
1193 if (x < min_x)
1194 x = min_x;
1195 else if (x >= max_x)
1196 x = max_x;
1197 }
1198 if (!y_valid) {
1199 if (y < min_y)
1200 y = min_y;
1201 else if (y >= max_y)
1202 y = max_y;
1203 }
Ray Strode90e701d2008-12-18 23:05:43 -05001204
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -05001205 device->x = x;
1206 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001207
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001208 if (device->grab) {
1209 interface = device->grab->interface;
1210 interface->motion(device->grab, time, x, y);
1211 } else {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001212 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001213 wl_input_device_set_pointer_focus(device,
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001214 &es->surface,
Kristian Høgsberg26437072010-12-01 10:17:47 -05001215 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001216 if (es)
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001217 wl_client_post_event(es->surface.client,
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001218 &device->object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001219 WL_INPUT_DEVICE_MOTION,
1220 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001221 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001222
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001223 wlsc_surface_damage(wd->sprite);
1224
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001225 wd->sprite->x = device->x - wd->hotspot_x;
1226 wd->sprite->y = device->y - wd->hotspot_y;
1227 wlsc_surface_update_matrix(wd->sprite);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001228
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001229 wlsc_surface_damage(wd->sprite);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001230}
1231
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001232WL_EXPORT void
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001233wlsc_surface_activate(struct wlsc_surface *surface,
1234 struct wlsc_input_device *device, uint32_t time)
1235{
1236 wlsc_surface_raise(surface);
1237 if (device->selection)
1238 wlsc_selection_set_focus(device->selection,
1239 &surface->surface, time);
1240
1241 wl_input_device_set_keyboard_focus(&device->input_device,
1242 &surface->surface,
1243 time);
1244}
1245
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001246struct wlsc_binding {
1247 uint32_t key;
1248 uint32_t button;
1249 uint32_t modifier;
1250 wlsc_binding_handler_t handler;
1251 void *data;
1252 struct wl_list link;
1253};
1254
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001255void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001256notify_button(struct wl_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001257 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001258{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001259 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001260 struct wlsc_compositor *compositor =
1261 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001262 struct wlsc_binding *b;
1263 struct wlsc_surface *surface =
1264 (struct wlsc_surface *) device->pointer_focus;
Kristian Høgsbergea081152010-12-07 08:59:51 -05001265
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001266 if (state)
1267 wlsc_compositor_idle_inhibit(compositor);
1268 else
1269 wlsc_compositor_idle_release(compositor);
1270
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -05001271 if (state && surface && device->grab == NULL) {
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001272 wlsc_surface_activate(surface, wd, time);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001273 wl_input_device_start_grab(device,
1274 &device->motion_grab,
1275 button, time);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001276 }
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -05001277
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001278 wl_list_for_each(b, &compositor->binding_list, link) {
1279 if (b->button == button &&
1280 b->modifier == wd->modifier_state && state) {
1281 b->handler(&wd->input_device,
1282 time, 0, button, state, b->data);
1283 break;
1284 }
Benjamin Franzke5a2218a2011-02-01 16:30:31 +01001285 }
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001286
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -05001287 if (device->grab)
1288 device->grab->interface->button(device->grab, time,
1289 button, state);
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -05001290
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001291 if (!state && device->grab && device->grab_button == button)
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001292 wl_input_device_end_grab(device, time);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001293}
1294
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001295static void
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001296terminate_binding(struct wl_input_device *device, uint32_t time,
1297 uint32_t key, uint32_t button, uint32_t state, void *data)
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001298{
1299 struct wlsc_compositor *compositor = data;
1300
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001301 if (state)
1302 wl_display_terminate(compositor->wl_display);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001303}
1304
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001305WL_EXPORT struct wlsc_binding *
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001306wlsc_compositor_add_binding(struct wlsc_compositor *compositor,
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001307 uint32_t key, uint32_t button, uint32_t modifier,
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001308 wlsc_binding_handler_t handler, void *data)
1309{
1310 struct wlsc_binding *binding;
1311
1312 binding = malloc(sizeof *binding);
1313 if (binding == NULL)
1314 return NULL;
1315
1316 binding->key = key;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001317 binding->button = button;
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001318 binding->modifier = modifier;
1319 binding->handler = handler;
1320 binding->data = data;
1321 wl_list_insert(compositor->binding_list.prev, &binding->link);
1322
1323 return binding;
1324}
1325
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001326WL_EXPORT void
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001327wlsc_binding_destroy(struct wlsc_binding *binding)
1328{
1329 wl_list_remove(&binding->link);
1330 free(binding);
1331}
1332
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001333static void
1334update_modifier_state(struct wlsc_input_device *device,
1335 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001336{
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001337 uint32_t modifier;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001338
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001339 switch (key) {
1340 case KEY_LEFTCTRL:
1341 case KEY_RIGHTCTRL:
1342 modifier = MODIFIER_CTRL;
1343 break;
1344
1345 case KEY_LEFTALT:
1346 case KEY_RIGHTALT:
1347 modifier = MODIFIER_ALT;
1348 break;
1349
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -04001350 case KEY_LEFTMETA:
1351 case KEY_RIGHTMETA:
1352 modifier = MODIFIER_SUPER;
1353 break;
1354
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001355 default:
1356 modifier = 0;
1357 break;
1358 }
1359
1360 if (state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001361 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001362 else
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001363 device->modifier_state &= ~modifier;
1364}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001365
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001366void
1367notify_key(struct wl_input_device *device,
1368 uint32_t time, uint32_t key, uint32_t state)
1369{
1370 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1371 struct wlsc_compositor *compositor =
1372 (struct wlsc_compositor *) device->compositor;
1373 uint32_t *k, *end;
1374 struct wlsc_binding *b;
1375
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001376 if (state)
1377 wlsc_compositor_idle_inhibit(compositor);
1378 else
1379 wlsc_compositor_idle_release(compositor);
1380
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001381 wl_list_for_each(b, &compositor->binding_list, link) {
1382 if (b->key == key &&
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001383 b->modifier == wd->modifier_state) {
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001384 b->handler(&wd->input_device,
1385 time, key, 0, state, b->data);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001386 break;
1387 }
1388 }
1389
1390 update_modifier_state(wd, key, state);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001391 end = device->keys.data + device->keys.size;
1392 for (k = device->keys.data; k < end; k++) {
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001393 if (*k == key)
1394 *k = *--end;
1395 }
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001396 device->keys.size = (void *) end - device->keys.data;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001397 if (state) {
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001398 k = wl_array_add(&device->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001399 *k = key;
1400 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001401
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001402 if (device->keyboard_focus != NULL)
1403 wl_client_post_event(device->keyboard_focus->client,
1404 &device->object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001405 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001406}
1407
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001408void
1409notify_pointer_focus(struct wl_input_device *device,
1410 uint32_t time, struct wlsc_output *output,
1411 int32_t x, int32_t y)
1412{
1413 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1414 struct wlsc_compositor *compositor =
1415 (struct wlsc_compositor *) device->compositor;
1416 struct wlsc_surface *es;
1417 int32_t sx, sy;
1418
1419 if (output) {
1420 device->x = x;
1421 device->y = y;
1422 es = pick_surface(device, &sx, &sy);
1423 wl_input_device_set_pointer_focus(device,
1424 &es->surface,
1425 time, x, y, sx, sy);
1426
1427 compositor->focus = 1;
1428
1429 wd->sprite->x = device->x - wd->hotspot_x;
1430 wd->sprite->y = device->y - wd->hotspot_y;
1431 wlsc_surface_update_matrix(wd->sprite);
1432 } else {
1433 wl_input_device_set_pointer_focus(device, NULL,
1434 time, 0, 0, 0, 0);
1435 compositor->focus = 0;
1436 }
1437
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001438 wlsc_surface_damage(wd->sprite);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001439}
1440
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001441void
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001442notify_keyboard_focus(struct wl_input_device *device,
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001443 uint32_t time, struct wlsc_output *output,
1444 struct wl_array *keys)
1445{
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001446 struct wlsc_input_device *wd =
1447 (struct wlsc_input_device *) device;
1448 struct wlsc_compositor *compositor =
1449 (struct wlsc_compositor *) device->compositor;
1450 struct wlsc_surface *es;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001451 uint32_t *k, *end;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001452
1453 if (!wl_list_empty(&compositor->surface_list))
1454 es = container_of(compositor->surface_list.next,
1455 struct wlsc_surface, link);
1456 else
1457 es = NULL;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001458
1459 if (output) {
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001460 wl_array_copy(&wd->input_device.keys, keys);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001461 wd->modifier_state = 0;
1462 end = device->keys.data + device->keys.size;
1463 for (k = device->keys.data; k < end; k++) {
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001464 wlsc_compositor_idle_inhibit(compositor);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001465 update_modifier_state(wd, *k, 1);
1466 }
1467
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001468 wl_input_device_set_keyboard_focus(&wd->input_device,
1469 &es->surface, time);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001470 } else {
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001471 end = device->keys.data + device->keys.size;
1472 for (k = device->keys.data; k < end; k++)
1473 wlsc_compositor_idle_release(compositor);
1474
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001475 wd->modifier_state = 0;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001476 wl_input_device_set_keyboard_focus(&wd->input_device,
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001477 NULL, time);
1478 }
1479}
1480
1481
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001482static void
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001483input_device_attach(struct wl_client *client,
1484 struct wl_input_device *device_base,
1485 uint32_t time,
1486 struct wl_buffer *buffer, int32_t x, int32_t y)
1487{
1488 struct wlsc_input_device *device =
1489 (struct wlsc_input_device *) device_base;
1490
1491 if (time < device->input_device.pointer_focus_time)
1492 return;
1493 if (device->input_device.pointer_focus == NULL)
1494 return;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001495 if (device->input_device.pointer_focus->client != client)
1496 return;
1497
1498 if (buffer == NULL) {
1499 wlsc_input_device_set_pointer_image(device,
1500 WLSC_POINTER_LEFT_PTR);
1501 return;
1502 }
1503
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001504 wlsc_input_device_attach_buffer(device, buffer, x, y);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001505}
1506
1507const static struct wl_input_device_interface input_device_interface = {
1508 input_device_attach,
1509};
1510
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001511void
1512wlsc_input_device_init(struct wlsc_input_device *device,
1513 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001514{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001515 wl_input_device_init(&device->input_device, &ec->compositor);
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05001516
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001517 device->input_device.object.interface = &wl_input_device_interface;
1518 device->input_device.object.implementation =
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001519 (void (**)(void)) &input_device_interface;
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001520 wl_display_add_object(ec->wl_display, &device->input_device.object);
1521 wl_display_add_global(ec->wl_display, &device->input_device.object, NULL);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001522
Kristian Høgsbergc5d6be92011-01-14 16:22:37 -05001523 device->sprite = wlsc_surface_create(ec,
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001524 device->input_device.x,
1525 device->input_device.y, 32, 32);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001526 device->hotspot_x = 16;
1527 device->hotspot_y = 16;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001528 device->modifier_state = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001529
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001530 device->input_device.motion_grab.interface = &motion_grab_interface;
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001531
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001532 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001533
1534 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001535}
1536
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001537static void
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001538wlsc_output_post_geometry(struct wl_client *client,
1539 struct wl_object *global, uint32_t version)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001540{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001541 struct wlsc_output *output =
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001542 container_of(global, struct wlsc_output, object);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001543
1544 wl_client_post_event(client, global,
1545 WL_OUTPUT_GEOMETRY,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001546 output->x, output->y,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001547 output->width, output->height);
1548}
1549
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001550static const char vertex_shader[] =
1551 "uniform mat4 proj;\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001552 "attribute vec2 position;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001553 "attribute vec2 texcoord;\n"
1554 "varying vec2 v_texcoord;\n"
1555 "void main()\n"
1556 "{\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001557 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001558 " v_texcoord = texcoord;\n"
1559 "}\n";
1560
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001561static const char texture_fragment_shader[] =
Kristian Høgsbergdfce71d2010-12-07 20:19:10 -05001562 "precision mediump float;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001563 "varying vec2 v_texcoord;\n"
1564 "uniform sampler2D tex;\n"
1565 "void main()\n"
1566 "{\n"
1567 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1568 "}\n";
1569
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001570static const char solid_fragment_shader[] =
1571 "precision mediump float;\n"
1572 "varying vec2 v_texcoord;\n"
1573 "uniform vec4 color;\n"
1574 "void main()\n"
1575 "{\n"
1576 " gl_FragColor = color\n;"
1577 "}\n";
1578
Kristian Høgsberga9468212010-06-14 21:03:11 -04001579static int
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001580compile_shader(GLenum type, const char *source)
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001581{
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001582 GLuint s;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001583 char msg[512];
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001584 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001585
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001586 s = glCreateShader(type);
1587 glShaderSource(s, 1, &source, NULL);
1588 glCompileShader(s);
1589 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001590 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001591 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1592 fprintf(stderr, "shader info: %s\n", msg);
1593 return GL_NONE;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001594 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001595
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001596 return s;
1597}
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001598
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001599static int
1600wlsc_shader_init(struct wlsc_shader *shader,
1601 const char *vertex_source, const char *fragment_source)
1602{
1603 char msg[512];
1604 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001605
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001606 shader->vertex_shader =
1607 compile_shader(GL_VERTEX_SHADER, vertex_source);
1608 shader->fragment_shader =
1609 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1610
1611 shader->program = glCreateProgram();
1612 glAttachShader(shader->program, shader->vertex_shader);
1613 glAttachShader(shader->program, shader->fragment_shader);
1614 glBindAttribLocation(shader->program, 0, "position");
1615 glBindAttribLocation(shader->program, 1, "texcoord");
1616
1617 glLinkProgram(shader->program);
1618 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001619 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001620 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001621 fprintf(stderr, "link info: %s\n", msg);
1622 return -1;
1623 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001624
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001625 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1626 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001627
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001628 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001629}
1630
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001631static int
1632init_solid_shader(struct wlsc_shader *shader,
1633 GLuint vertex_shader, const char *fragment_source)
1634{
1635 GLint status;
1636 char msg[512];
1637
1638 shader->vertex_shader = vertex_shader;
1639 shader->fragment_shader =
1640 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1641
1642 shader->program = glCreateProgram();
1643 glAttachShader(shader->program, shader->vertex_shader);
1644 glAttachShader(shader->program, shader->fragment_shader);
1645 glBindAttribLocation(shader->program, 0, "position");
1646 glBindAttribLocation(shader->program, 1, "texcoord");
1647
1648 glLinkProgram(shader->program);
1649 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1650 if (!status) {
1651 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1652 fprintf(stderr, "link info: %s\n", msg);
1653 return -1;
1654 }
1655
1656 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1657 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1658
1659 return 0;
1660}
1661
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001662void
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001663wlsc_output_destroy(struct wlsc_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001664{
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001665 destroy_surface(&output->background->surface.resource, NULL);
1666}
1667
1668void
1669wlsc_output_move(struct wlsc_output *output, int x, int y)
1670{
1671 struct wlsc_compositor *c = output->compositor;
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01001672 int flip;
1673
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001674 output->x = x;
1675 output->y = y;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001676
Benjamin Franzke264b3f92011-03-16 13:48:42 +01001677 if (output->background) {
1678 output->background->x = x;
1679 output->background->y = y;
1680 wlsc_surface_update_matrix(output->background);
1681 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001682
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001683 pixman_region32_init(&output->previous_damage_region);
1684
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001685 wlsc_matrix_init(&output->matrix);
1686 wlsc_matrix_translate(&output->matrix,
1687 -output->x - output->width / 2.0,
1688 -output->y - output->height / 2.0, 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01001689
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001690 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001691 wlsc_matrix_scale(&output->matrix,
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01001692 2.0 / output->width,
1693 flip * 2.0 / output->height, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001694
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001695 pixman_region32_union_rect(&c->damage_region,
1696 &c->damage_region,
1697 x, y, output->width, output->height);
1698}
1699
1700void
1701wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1702 int x, int y, int width, int height, uint32_t flags)
1703{
1704 output->compositor = c;
1705 output->x = x;
1706 output->y = y;
1707 output->width = width;
1708 output->height = height;
1709
1710 output->background =
1711 background_create(output, option_background);
1712
1713 output->flags = flags;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001714 output->finished = 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001715 wlsc_output_move(output, x, y);
1716
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001717 output->object.interface = &wl_output_interface;
1718 wl_display_add_object(c->wl_display, &output->object);
1719 wl_display_add_global(c->wl_display, &output->object,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001720 wlsc_output_post_geometry);
1721}
1722
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001723static void
1724shm_buffer_created(struct wl_buffer *buffer)
1725{
1726 struct wl_list *surfaces_attached_to;
1727
1728 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1729 if (!surfaces_attached_to) {
1730 buffer->user_data = NULL;
1731 return;
1732 }
1733
1734 wl_list_init(surfaces_attached_to);
1735
1736 buffer->user_data = surfaces_attached_to;
1737}
1738
1739static void
1740shm_buffer_damaged(struct wl_buffer *buffer,
1741 int32_t x, int32_t y, int32_t width, int32_t height)
1742{
1743 struct wl_list *surfaces_attached_to = buffer->user_data;
1744 struct wlsc_surface *es;
Benjamin Franzkefab5ec12011-04-25 19:44:47 +02001745 GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001746
1747 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1748 glBindTexture(GL_TEXTURE_2D, es->texture);
1749 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Benjamin Franzkefab5ec12011-04-25 19:44:47 +02001750 tex_width, buffer->height, 0,
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001751 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1752 wl_shm_buffer_get_data(buffer));
1753 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
1754 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
1755 }
1756}
1757
1758static void
1759shm_buffer_destroyed(struct wl_buffer *buffer)
1760{
1761 struct wl_list *surfaces_attached_to = buffer->user_data;
1762 struct wlsc_surface *es, *next;
1763
1764 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001765 wl_list_remove(&es->buffer_link);
Kristian Høgsbergfac11d22011-05-02 13:35:17 -04001766 wl_list_init(&es->buffer_link);
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001767 }
1768
1769 free(surfaces_attached_to);
1770}
1771
1772const static struct wl_shm_callbacks shm_callbacks = {
1773 shm_buffer_created,
1774 shm_buffer_damaged,
1775 shm_buffer_destroyed
1776};
1777
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001778int
1779wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1780{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001781 struct wl_event_loop *loop;
Kristian Høgsbergd711d0c2011-01-14 17:28:21 -05001782 const char *extensions;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001783
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001784 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001785
Kristian Høgsbergc5c510e2010-12-08 15:12:58 -05001786 wl_compositor_init(&ec->compositor, &compositor_interface, display);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001787
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001788 ec->shm = wl_shm_init(display, &shm_callbacks);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04001789
1790 ec->image_target_texture_2d =
1791 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1792 ec->image_target_renderbuffer_storage = (void *)
1793 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1794 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1795 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1796 ec->bind_display =
1797 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1798 ec->unbind_display =
1799 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1800
1801 extensions = (const char *) glGetString(GL_EXTENSIONS);
1802 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1803 fprintf(stderr,
1804 "GL_EXT_texture_format_BGRA8888 not available\n");
1805 return -1;
1806 }
1807
1808 extensions =
1809 (const char *) eglQueryString(ec->display, EGL_EXTENSIONS);
1810 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1811 ec->has_bind_display = 1;
1812 if (ec->has_bind_display)
1813 ec->bind_display(ec->display, ec->wl_display);
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04001814
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001815 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001816 wl_list_init(&ec->input_device_list);
1817 wl_list_init(&ec->output_list);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001818 wl_list_init(&ec->binding_list);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001819 wl_list_init(&ec->animation_list);
1820 wlsc_tweener_init(&ec->fade.tweener, 0.8, 0.0, 0.0);
1821 ec->fade.animation.frame = fade_frame;
1822 wl_list_init(&ec->fade.animation.link);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001823
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001824 wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0,
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001825 MODIFIER_CTRL | MODIFIER_ALT,
1826 terminate_binding, ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001827
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001828 create_pointer_images(ec);
1829
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001830 screenshooter_create(ec);
1831
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001832 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001833
1834 if (wlsc_shader_init(&ec->texture_shader,
1835 vertex_shader, texture_fragment_shader) < 0)
Kristian Høgsberga9468212010-06-14 21:03:11 -04001836 return -1;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001837 if (init_solid_shader(&ec->solid_shader,
1838 ec->texture_shader.vertex_shader,
1839 solid_fragment_shader) < 0)
1840 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001841
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001842 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001843 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
1844 wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000);
1845
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001846 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001847 pixman_region32_init(&ec->damage_region);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001848 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001849
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001850 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001851}
1852
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001853static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001854{
1855 struct wlsc_compositor *ec = data;
1856
1857 wl_display_terminate(ec->wl_display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001858
1859 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001860}
1861
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001862int main(int argc, char *argv[])
1863{
1864 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001865 struct wlsc_compositor *ec;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001866 struct wl_event_loop *loop;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04001867 int width, height, o;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001868 void *shell_module;
1869 int (*shell_init)(struct wlsc_compositor *ec);
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04001870 char *p;
1871
1872 static const char opts[] = "b:c:g:S:i:s:";
1873 static const struct option longopts[ ] = {
1874 { "background", 1, NULL, 'b' },
1875 { "connector", 1, NULL, 'c' },
1876 { "geometry", 1, NULL, 'g' },
1877 { "socket", 1, NULL, 'S' },
1878 { "idle-time", 1, NULL, 'i' },
1879 { "shell", 1, NULL, 's' },
1880 { NULL, }
1881 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001882
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04001883 width = 1024;
1884 height = 640;
1885
1886 while (o = getopt_long(argc, argv, opts, longopts, &o), o > 0) {
1887 switch (o) {
1888 case 'b':
1889 option_background = optarg;
1890 break;
1891 case 'c':
1892 option_connector = strtol(optarg, &p, 0);
1893 if (*p != '\0') {
1894 fprintf(stderr,
1895 "invalid connection option: %s\n",
1896 optarg);
1897 exit(EXIT_FAILURE);
1898 }
1899 break;
1900 case 'g':
1901 if (sscanf(optarg, "%dx%d", &width, &height) != 2) {
1902 fprintf(stderr,
1903 "invalid geometry option: %s\n",
1904 optarg);
1905 exit(EXIT_FAILURE);
1906 }
1907 break;
1908 case 'S':
1909 option_socket_name = optarg;
1910 break;
1911 case 'i':
1912 option_idle_time = strtol(optarg, &p, 0);
1913 if (*p != '\0') {
1914 fprintf(stderr,
1915 "invalid idle time option: %s\n",
1916 optarg);
1917 exit(EXIT_FAILURE);
1918 }
1919 break;
1920 case 's':
1921 option_shell = optarg;
1922 break;
1923 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001924 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001925
1926 display = wl_display_create();
1927
Kristian Høgsberga9410222011-01-14 17:22:35 -05001928 ec = NULL;
1929
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001930 shell_init = desktop_shell_init;
1931 if (option_shell) {
1932 shell_module = dlopen(option_shell, RTLD_LAZY);
1933 if (!shell_module) {
1934 fprintf(stderr, "failed to load shell module: %m\n");
1935 exit(EXIT_FAILURE);
1936 }
1937 shell_init = dlsym(shell_module, "shell_init");
1938 if (!shell_init) {
1939 fprintf(stderr,
1940 "failed to lookup shell init function: %m\n");
1941 exit(EXIT_FAILURE);
1942 }
1943 }
1944
Kristian Høgsberga9410222011-01-14 17:22:35 -05001945#if BUILD_WAYLAND_COMPOSITOR
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001946 if (getenv("WAYLAND_DISPLAY"))
1947 ec = wayland_compositor_create(display, width, height);
Kristian Høgsberga9410222011-01-14 17:22:35 -05001948#endif
1949
1950#if BUILD_X11_COMPOSITOR
1951 if (ec == NULL && getenv("DISPLAY"))
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001952 ec = x11_compositor_create(display, width, height);
Kristian Høgsberga9410222011-01-14 17:22:35 -05001953#endif
1954
Benjamin Franzke5d007092011-04-04 00:30:25 +02001955#if BUILD_OPENWFD_COMPOSITOR
1956 if (ec == NULL && getenv("OPENWFD"))
1957 ec = wfd_compositor_create(display, option_connector);
1958#endif
1959
Kristian Høgsberga9410222011-01-14 17:22:35 -05001960#if BUILD_DRM_COMPOSITOR
1961 if (ec == NULL)
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001962 ec = drm_compositor_create(display, option_connector);
Kristian Høgsberga9410222011-01-14 17:22:35 -05001963#endif
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001964
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001965 if (ec == NULL) {
1966 fprintf(stderr, "failed to create compositor\n");
1967 exit(EXIT_FAILURE);
1968 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001969
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001970 if (shell_init(ec) < 0)
1971 exit(EXIT_FAILURE);
1972
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001973 if (wl_display_add_socket(display, option_socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001974 fprintf(stderr, "failed to add socket: %m\n");
1975 exit(EXIT_FAILURE);
1976 }
1977
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001978 loop = wl_display_get_event_loop(ec->wl_display);
1979 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
1980 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec);
1981
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001982 wl_display_run(display);
1983
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04001984 if (ec->has_bind_display)
1985 ec->unbind_display(ec->display, display);
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001986 wl_display_destroy(display);
1987
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001988 ec->destroy(ec);
1989
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001990 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001991}