blob: 987267ed921a503da156fab4b1ea49d345231aed [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øgsbergaa5b5be2008-11-21 21:31:54 -050032#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050033#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040034#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040035#include <dlfcn.h>
Kristian Høgsbergd34912c2011-05-02 10:36:04 -040036#include <getopt.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;
Benjamin Franzkebab41fb2011-03-07 18:04:59 +0100184 surface->buffer = NULL;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400185 surface->x = x;
186 surface->y = y;
187 surface->width = width;
188 surface->height = height;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400189 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400190 wlsc_matrix_scale(&surface->matrix, width, height, 1);
191 wlsc_matrix_translate(&surface->matrix, x, y, 0);
192
193 wlsc_matrix_init(&surface->matrix_inv);
194 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
195 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
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øgsbergc551bd22010-12-06 16:43:16 -0500236 struct wl_listener *l, *next;
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400237 struct wlsc_compositor *compositor = surface->compositor;
Kristian Høgsberg4685fa32010-12-06 21:38:50 -0500238 uint32_t time;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400239
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500240 wlsc_surface_damage(surface);
241
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400242 wl_list_remove(&surface->link);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200243 if (surface->saved_texture == 0)
244 glDeleteTextures(1, &surface->texture);
245 else
246 glDeleteTextures(1, &surface->saved_texture);
247
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400248
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200249 if (surface->image != EGL_NO_IMAGE_KHR)
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400250 compositor->destroy_image(compositor->display,
251 surface->image);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200252
Benjamin Franzkebab41fb2011-03-07 18:04:59 +0100253 wl_list_remove(&surface->buffer_link);
254
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400255 time = wlsc_compositor_get_time();
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500256 wl_list_for_each_safe(l, next,
257 &surface->surface.destroy_listener_list, link)
Kristian Høgsberg4685fa32010-12-06 21:38:50 -0500258 l->func(l, &surface->surface, time);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400259
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400260 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500261}
262
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100263static void
264wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
265{
266 struct wlsc_surface *es = (struct wlsc_surface *) surface;
267 struct wlsc_compositor *ec = es->compositor;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100268 struct wl_list *surfaces_attached_to;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100269
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100270 if (es->saved_texture != 0)
271 es->texture = es->saved_texture;
272
273 glBindTexture(GL_TEXTURE_2D, es->texture);
274
275 if (wl_buffer_is_shm(buffer)) {
276 /* Unbind any EGLImage texture that may be bound, so we don't
277 * overwrite it.*/
278 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
279 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200280 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100281 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200282 es->pitch, buffer->height, 0,
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100283 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
284 wl_shm_buffer_get_data(buffer));
285 es->visual = buffer->visual;
286
287 surfaces_attached_to = buffer->user_data;
288
289 if (es->buffer)
290 wl_list_remove(&es->buffer_link);
291 wl_list_insert(surfaces_attached_to, &es->buffer_link);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100292 } else {
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400293 es->image = ec->create_image(ec->display, NULL,
294 EGL_WAYLAND_BUFFER_WL,
295 buffer, NULL);
296
297 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100298 es->visual = buffer->visual;
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200299 es->pitch = es->width;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100300 }
301}
302
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200303static void
304wlsc_sprite_attach(struct wlsc_sprite *sprite, struct wl_surface *surface)
305{
306 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400307 struct wlsc_compositor *ec = es->compositor;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200308
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200309 es->pitch = es->width;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200310 es->image = sprite->image;
311 if (sprite->image != EGL_NO_IMAGE_KHR) {
312 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400313 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200314 } else {
315 if (es->saved_texture == 0)
316 es->saved_texture = es->texture;
317 es->texture = sprite->texture;
318 }
319
320 es->visual = sprite->visual;
321}
322
323enum sprite_usage {
324 SPRITE_USE_CURSOR = (1 << 0),
325};
326
327static struct wlsc_sprite *
328create_sprite_from_png(struct wlsc_compositor *ec,
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400329 const char *filename, uint32_t usage)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500330{
331 uint32_t *pixels;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200332 struct wlsc_sprite *sprite;
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400333 int32_t width, height;
334 uint32_t stride;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500335
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400336 pixels = wlsc_load_image(filename, &width, &height, &stride);
337 if (pixels == NULL)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200338 return NULL;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500339
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200340 sprite = malloc(sizeof *sprite);
341 if (sprite == NULL) {
342 free(pixels);
343 return NULL;
344 }
345
346 sprite->visual = &ec->compositor.premultiplied_argb_visual;
347 sprite->width = width;
348 sprite->height = height;
349 sprite->image = EGL_NO_IMAGE_KHR;
350
351 if (usage & SPRITE_USE_CURSOR && ec->create_cursor_image != NULL)
352 sprite->image = ec->create_cursor_image(ec, width, height);
353
354 glGenTextures(1, &sprite->texture);
355 glBindTexture(GL_TEXTURE_2D, sprite->texture);
356 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
357 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
358 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
359 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
360
361 if (sprite->image != EGL_NO_IMAGE_KHR) {
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400362 ec->image_target_texture_2d(GL_TEXTURE_2D, sprite->image);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200363 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
364 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
365 } else {
366 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, width, height, 0,
367 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
368 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500369
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500370 free(pixels);
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500371
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200372 return sprite;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500373}
374
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400375static const struct {
376 const char *filename;
377 int hotspot_x, hotspot_y;
378} pointer_images[] = {
Kristian Høgsberg4219a402010-08-16 16:43:03 -0400379 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
380 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
381 { DATADIR "/wayland/bottom_side.png", 16, 20 },
382 { DATADIR "/wayland/grabbing.png", 20, 17 },
383 { DATADIR "/wayland/left_ptr.png", 10, 5 },
384 { DATADIR "/wayland/left_side.png", 10, 20 },
385 { DATADIR "/wayland/right_side.png", 30, 19 },
386 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
387 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
388 { DATADIR "/wayland/top_side.png", 18, 8 },
389 { DATADIR "/wayland/xterm.png", 15, 15 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400390};
391
392static void
393create_pointer_images(struct wlsc_compositor *ec)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500394{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400395 int i, count;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400396
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400397 count = ARRAY_LENGTH(pointer_images);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200398 ec->pointer_sprites = malloc(count * sizeof *ec->pointer_sprites);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400399 for (i = 0; i < count; i++) {
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200400 ec->pointer_sprites[i] =
401 create_sprite_from_png(ec,
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500402 pointer_images[i].filename,
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200403 SPRITE_USE_CURSOR);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400404 }
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400405}
406
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500407static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500408background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500409{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500410 struct wlsc_surface *background;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200411 struct wlsc_sprite *sprite;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500412
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400413 background = wlsc_surface_create(output->compositor,
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400414 output->x, output->y,
415 output->width, output->height);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500416 if (background == NULL)
417 return NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500418
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400419 sprite = create_sprite_from_png(output->compositor, filename, 0);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200420 if (sprite == NULL) {
Benjamin Franzked3b023e2011-01-15 12:34:49 +0100421 free(background);
422 return NULL;
423 }
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100424
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200425 wlsc_sprite_attach(sprite, &background->surface);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500426
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500427 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500428}
429
430static void
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500431wlsc_surface_draw(struct wlsc_surface *es,
432 struct wlsc_output *output, pixman_region32_t *clip)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500433{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500434 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500435 GLfloat *v, inv_width, inv_height;
436 unsigned int *p;
437 pixman_region32_t repaint;
438 pixman_box32_t *rectangles;
439 int i, n;
440
441 pixman_region32_init_rect(&repaint,
442 es->x, es->y, es->width, es->height);
443 pixman_region32_intersect(&repaint, &repaint, clip);
444 if (!pixman_region32_not_empty(&repaint))
445 return;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500446
Kristian Høgsbergc5c510e2010-12-08 15:12:58 -0500447 if (es->visual == &ec->compositor.argb_visual) {
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500448 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
449 glEnable(GL_BLEND);
Kristian Høgsbergc5c510e2010-12-08 15:12:58 -0500450 } else if (es->visual == &ec->compositor.premultiplied_argb_visual) {
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500451 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
452 glEnable(GL_BLEND);
453 } else {
454 glDisable(GL_BLEND);
455 }
456
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500457 rectangles = pixman_region32_rectangles(&repaint, &n);
458 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
459 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200460 inv_width = 1.0 / es->pitch;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500461 inv_height = 1.0 / es->height;
462 for (i = 0; i < n; i++, v += 16, p += 6) {
463 v[ 0] = rectangles[i].x1;
464 v[ 1] = rectangles[i].y1;
465 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
466 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500467
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500468 v[ 4] = rectangles[i].x1;
469 v[ 5] = rectangles[i].y2;
470 v[ 6] = v[ 2];
471 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500472
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500473 v[ 8] = rectangles[i].x2;
474 v[ 9] = rectangles[i].y1;
475 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
476 v[11] = v[ 3];
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500477
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500478 v[12] = rectangles[i].x2;
479 v[13] = rectangles[i].y2;
480 v[14] = v[10];
481 v[15] = v[ 7];
482
483 p[0] = i * 4 + 0;
484 p[1] = i * 4 + 1;
485 p[2] = i * 4 + 2;
486 p[3] = i * 4 + 2;
487 p[4] = i * 4 + 1;
488 p[5] = i * 4 + 3;
489 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500490
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500491 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500492 v = ec->vertices.data;
493 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
494 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400495 glEnableVertexAttribArray(0);
496 glEnableVertexAttribArray(1);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500497 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
498
499 ec->vertices.size = 0;
500 ec->indices.size = 0;
501 pixman_region32_fini(&repaint);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500502}
503
504static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400505wlsc_surface_raise(struct wlsc_surface *surface)
506{
507 struct wlsc_compositor *compositor = surface->compositor;
508
509 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400510 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400511}
512
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400513WL_EXPORT void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400514wlsc_surface_update_matrix(struct wlsc_surface *es)
515{
516 wlsc_matrix_init(&es->matrix);
517 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
518 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
519
520 wlsc_matrix_init(&es->matrix_inv);
521 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
522 wlsc_matrix_scale(&es->matrix_inv,
523 1.0 / es->width, 1.0 / es->height, 1);
524}
525
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400526WL_EXPORT void
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400527wlsc_compositor_damage_all(struct wlsc_compositor *compositor)
528{
529 struct wlsc_output *output;
530
531 wl_list_for_each(output, &compositor->output_list, link)
532 wlsc_output_damage(output);
533}
534
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400535WL_EXPORT void
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100536wlsc_output_finish_frame(struct wlsc_output *output, int msecs)
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400537{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100538 struct wlsc_compositor *compositor = output->compositor;
539 struct wlsc_surface *es;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400540 struct wlsc_animation *animation, *next;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100541
542 wl_list_for_each(es, &compositor->surface_list, link) {
543 if (es->output == output) {
544 wl_display_post_frame(compositor->wl_display,
545 &es->surface, msecs);
546 }
547 }
548
549 output->finished = 1;
550
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400551 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsbergf30c67e2011-02-06 12:58:44 -0500552 compositor->repaint_on_timeout = 1;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400553
554 wl_list_for_each_safe(animation, next,
555 &compositor->animation_list, link)
556 animation->frame(animation, output, msecs);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400557}
558
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400559WL_EXPORT void
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400560wlsc_output_damage(struct wlsc_output *output)
561{
562 struct wlsc_compositor *compositor = output->compositor;
563
564 pixman_region32_union_rect(&compositor->damage_region,
565 &compositor->damage_region,
566 output->x, output->y,
567 output->width, output->height);
568 wlsc_compositor_schedule_repaint(compositor);
569}
570
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400571static void
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400572fade_frame(struct wlsc_animation *animation,
573 struct wlsc_output *output, uint32_t msecs)
574{
575 struct wlsc_compositor *compositor =
576 container_of(animation,
577 struct wlsc_compositor, fade.animation);
578
579 wlsc_tweener_update(&compositor->fade.tweener, msecs);
580 if (wlsc_tweener_done(&compositor->fade.tweener)) {
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400581 if (compositor->fade.tweener.current > 0.999) {
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400582 compositor->state = WLSC_COMPOSITOR_SLEEPING;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400583 compositor->shell->lock(compositor->shell);
584 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400585 compositor->fade.tweener.current =
586 compositor->fade.tweener.target;
587 wl_list_remove(&animation->link);
588 wl_list_init(&animation->link);
589 }
590
591 wlsc_output_damage(output);
592}
593
594static void
595fade_output(struct wlsc_output *output,
596 GLfloat tint, pixman_region32_t *region)
597{
598 struct wlsc_compositor *compositor = output->compositor;
599 struct wlsc_surface surface;
600 GLfloat color[4] = { 0.0, 0.0, 0.0, tint };
601
602 surface.compositor = compositor;
603 surface.x = output->x;
604 surface.y = output->y;
605 surface.width = output->width;
606 surface.height = output->height;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400607 surface.texture = GL_NONE;
Kristian Høgsbergc352ab02011-04-23 15:34:50 -0400608
609 if (tint <= 1.0)
610 surface.visual =
611 &compositor->compositor.premultiplied_argb_visual;
612 else
613 surface.visual = &compositor->compositor.rgb_visual;
614
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400615 glUseProgram(compositor->solid_shader.program);
616 glUniformMatrix4fv(compositor->solid_shader.proj_uniform,
617 1, GL_FALSE, output->matrix.d);
618 glUniform4fv(compositor->solid_shader.color_uniform, 1, color);
619 wlsc_surface_draw(&surface, output, region);
620}
621
622static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400623wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400624{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500625 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500626 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500627 struct wlsc_input_device *eid;
Kristian Høgsbergcfc6d272011-04-11 13:34:24 -0400628 pixman_region32_t new_damage, total_damage, repaint;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200629 int using_hardware_cursor = 1;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500630
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100631 output->prepare_render(output);
632
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500633 glViewport(0, 0, output->width, output->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500634
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -0400635 glUseProgram(ec->texture_shader.program);
636 glUniformMatrix4fv(ec->texture_shader.proj_uniform,
637 1, GL_FALSE, output->matrix.d);
638 glUniform1i(ec->texture_shader.tex_uniform, 0);
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500639
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500640 pixman_region32_init(&new_damage);
641 pixman_region32_init(&total_damage);
642 pixman_region32_intersect_rect(&new_damage,
643 &ec->damage_region,
644 output->x, output->y,
645 output->width, output->height);
646 pixman_region32_subtract(&ec->damage_region,
647 &ec->damage_region, &new_damage);
648 pixman_region32_union(&total_damage, &new_damage,
649 &output->previous_damage_region);
650 pixman_region32_copy(&output->previous_damage_region, &new_damage);
651
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200652 if (ec->focus)
653 if (output->set_hardware_cursor(output, ec->input_device) < 0)
654 using_hardware_cursor = 0;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400655 if (ec->fade.tweener.current > 0.001)
656 using_hardware_cursor = 0;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200657
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500658 es = container_of(ec->surface_list.next, struct wlsc_surface, link);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400659 if (es->fullscreen_output == output) {
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200660 if (es->visual == &ec->compositor.rgb_visual &&
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200661 using_hardware_cursor) {
662 if (output->prepare_scanout_surface(output, es) == 0) {
663 /* We're drawing nothing now,
664 * draw the damaged regions later. */
665 pixman_region32_union(&ec->damage_region,
666 &ec->damage_region,
667 &total_damage);
668 return;
669 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200670 }
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200671
672 if (es->width < output->width ||
673 es->height < output->height)
674 glClear(GL_COLOR_BUFFER_BIT);
675 wlsc_surface_draw(es, output, &total_damage);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500676 } else {
Kristian Høgsbergcfc6d272011-04-11 13:34:24 -0400677 wl_list_for_each(es, &ec->surface_list, link) {
678 if (es->visual != &ec->compositor.rgb_visual)
679 continue;
680
681 pixman_region32_init_rect(&repaint,
682 es->x, es->y,
683 es->width, es->height);
684 wlsc_surface_draw(es, output, &total_damage);
685 pixman_region32_subtract(&total_damage,
686 &total_damage, &repaint);
687 }
688
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500689 if (output->background)
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500690 wlsc_surface_draw(output->background,
691 output, &total_damage);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500692 else
Kristian Høgsbergc352ab02011-04-23 15:34:50 -0400693 fade_output(output, 1.0, &total_damage);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500694
Kristian Høgsbergc352ab02011-04-23 15:34:50 -0400695 glUseProgram(ec->texture_shader.program);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500696 wl_list_for_each_reverse(es, &ec->surface_list, link) {
Kristian Høgsberg547cadf2011-04-12 22:23:30 -0400697 if (ec->overlay == es)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500698 continue;
699
Kristian Høgsbergcfc6d272011-04-11 13:34:24 -0400700 if (es->visual == &ec->compositor.rgb_visual) {
701 pixman_region32_union_rect(&total_damage,
702 &total_damage,
703 es->x, es->y,
704 es->width, es->height);
705 continue;
706 }
707
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500708 wlsc_surface_draw(es, output, &total_damage);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500709 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500710 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400711
Kristian Høgsberg547cadf2011-04-12 22:23:30 -0400712 if (ec->overlay)
713 wlsc_surface_draw(ec->overlay, output, &total_damage);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500714
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400715 if (ec->focus)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200716 wl_list_for_each(eid, &ec->input_device_list, link) {
717 if (&eid->input_device != ec->input_device ||
718 !using_hardware_cursor)
719 wlsc_surface_draw(eid->sprite, output,
720 &total_damage);
721 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400722
723 if (ec->fade.tweener.current > 0.001)
724 fade_output(output, ec->fade.tweener.current, &total_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500725}
726
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400727static int
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500728repaint(void *data)
729{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500730 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500731 struct wlsc_output *output;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100732 int repainted_all_outputs = 1;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500733
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100734 wl_list_for_each(output, &ec->output_list, link) {
735 if (!output->repaint_needed)
736 continue;
737
738 if (!output->finished) {
739 repainted_all_outputs = 0;
740 continue;
741 }
742
743 wlsc_output_repaint(output);
744 output->finished = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100745 output->repaint_needed = 0;
Kristian Høgsberg11e28282011-04-11 16:47:50 -0400746 output->present(output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500747 }
748
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100749 if (repainted_all_outputs)
750 ec->repaint_on_timeout = 0;
751 else
752 wl_event_source_timer_update(ec->timer_source, 1);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400753
754 return 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400755}
756
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400757WL_EXPORT void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400758wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400759{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100760 struct wlsc_output *output;
761
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400762 if (compositor->state == WLSC_COMPOSITOR_SLEEPING)
763 return;
764
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100765 wl_list_for_each(output, &compositor->output_list, link)
766 output->repaint_needed = 1;
767
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400768 if (compositor->repaint_on_timeout)
769 return;
770
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400771 wl_event_source_timer_update(compositor->timer_source, 1);
772 compositor->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400773}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500774
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400775WL_EXPORT void
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400776wlsc_compositor_fade(struct wlsc_compositor *compositor, float tint)
777{
778 int done;
779
780 done = wlsc_tweener_done(&compositor->fade.tweener);
781 compositor->fade.tweener.target = tint;
782 if (wlsc_tweener_done(&compositor->fade.tweener))
783 return;
784
785 if (done)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400786 compositor->fade.tweener.timestamp =
787 wlsc_compositor_get_time();
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400788
789 wlsc_compositor_damage_all(compositor);
790 if (wl_list_empty(&compositor->fade.animation.link))
791 wl_list_insert(compositor->animation_list.prev,
792 &compositor->fade.animation.link);
793}
794
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500795static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500796surface_destroy(struct wl_client *client,
797 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400798{
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500799 wl_resource_destroy(&surface->resource, client);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400800}
801
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400802WL_EXPORT void
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100803wlsc_surface_assign_output(struct wlsc_surface *es)
804{
805 struct wlsc_compositor *ec = es->compositor;
806 struct wlsc_output *output;
807
808 struct wlsc_output *tmp = es->output;
809 es->output = NULL;
810
811 wl_list_for_each(output, &ec->output_list, link) {
812 if (output->x < es->x && es->x < output->x + output->width &&
813 output->y < es->y && es->y < output->y + output->height) {
814 if (output != tmp)
815 printf("assiging surface %p to output %p\n",
816 es, output);
817 es->output = output;
818 }
819 }
820
821 if (es->output == NULL) {
822 printf("no output found\n");
823 es->output = container_of(ec->output_list.next,
824 struct wlsc_output, link);
825 }
826}
827
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500828static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500829surface_attach(struct wl_client *client,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500830 struct wl_surface *surface, struct wl_buffer *buffer,
831 int32_t x, int32_t y)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400832{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500833 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400834
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500835 /* FIXME: This damages the entire old surface, but we should
836 * really just damage the part that's no longer covered by the
837 * surface. Anything covered by the new surface will be
838 * damaged by the client. */
839 wlsc_surface_damage(es);
840
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100841 wlsc_buffer_attach(buffer, surface);
842
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400843 es->buffer = buffer;
Benjamin Franzke0b5116f2011-04-26 15:36:26 +0200844 switch (es->map_type) {
845 case WLSC_SURFACE_MAP_FULLSCREEN:
846 es->x = (es->fullscreen_output->width - es->width) / 2;
847 es->y = (es->fullscreen_output->height - es->height) / 2;
848 break;
849 default:
850 es->x += x;
851 es->y += y;
852 break;
853 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500854 es->width = buffer->width;
855 es->height = buffer->height;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100856 if (x != 0 || y != 0)
857 wlsc_surface_assign_output(es);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500858 wlsc_surface_update_matrix(es);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400859
860 es->compositor->shell->attach(es->compositor->shell, es);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400861}
862
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500863static void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500864surface_map_toplevel(struct wl_client *client,
865 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400866{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500867 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100868 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400869
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500870 switch (es->map_type) {
871 case WLSC_SURFACE_MAP_UNMAPPED:
872 es->x = 10 + random() % 400;
873 es->y = 10 + random() % 400;
874 wlsc_surface_update_matrix(es);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100875 /* assign to first output */
876 es->output = container_of(ec->output_list.next,
877 struct wlsc_output, link);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500878 wl_list_insert(&es->compositor->surface_list, &es->link);
879 break;
880 case WLSC_SURFACE_MAP_TOPLEVEL:
Kristian Høgsberg8f66a572011-01-07 08:38:56 -0500881 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500882 case WLSC_SURFACE_MAP_FULLSCREEN:
883 es->fullscreen_output = NULL;
884 es->x = es->saved_x;
885 es->y = es->saved_y;
886 wlsc_surface_update_matrix(es);
887 break;
888 default:
889 break;
890 }
Kristian Høgsberg8f66a572011-01-07 08:38:56 -0500891
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500892 wlsc_surface_damage(es);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500893 es->map_type = WLSC_SURFACE_MAP_TOPLEVEL;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400894}
895
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500896static void
Kristian Høgsberg8dc378f2011-01-21 18:02:24 -0500897surface_map_transient(struct wl_client *client,
898 struct wl_surface *surface, struct wl_surface *parent,
899 int x, int y, uint32_t flags)
900{
901 struct wlsc_surface *es = (struct wlsc_surface *) surface;
902 struct wlsc_surface *pes = (struct wlsc_surface *) parent;
903
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500904 switch (es->map_type) {
905 case WLSC_SURFACE_MAP_UNMAPPED:
906 wl_list_insert(&es->compositor->surface_list, &es->link);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100907 /* assign to parents output */
908 es->output = pes->output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500909 break;
910 case WLSC_SURFACE_MAP_FULLSCREEN:
911 es->fullscreen_output = NULL;
912 break;
913 default:
914 break;
915 }
Kristian Høgsberg8dc378f2011-01-21 18:02:24 -0500916
917 es->x = pes->x + x;
918 es->y = pes->y + y;
919
920 wlsc_surface_update_matrix(es);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500921 wlsc_surface_damage(es);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500922 es->map_type = WLSC_SURFACE_MAP_TRANSIENT;
923}
924
925static void
926surface_map_fullscreen(struct wl_client *client, struct wl_surface *surface)
927{
928 struct wlsc_surface *es = (struct wlsc_surface *) surface;
929 struct wlsc_output *output;
930
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100931 /* FIXME: Fullscreen on first output */
932 /* FIXME: Handle output going away */
933 output = container_of(es->compositor->output_list.next,
934 struct wlsc_output, link);
935
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500936 switch (es->map_type) {
937 case WLSC_SURFACE_MAP_UNMAPPED:
938 es->x = 10 + random() % 400;
939 es->y = 10 + random() % 400;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100940 /* assign to first output */
941 es->output = output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500942 wl_list_insert(&es->compositor->surface_list, &es->link);
943 break;
944 case WLSC_SURFACE_MAP_FULLSCREEN:
945 return;
946 default:
947 break;
948 }
949
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500950 es->saved_x = es->x;
951 es->saved_y = es->y;
952 es->x = (output->width - es->width) / 2;
953 es->y = (output->height - es->height) / 2;
954 es->fullscreen_output = output;
955 wlsc_surface_update_matrix(es);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500956 wlsc_surface_damage(es);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500957 es->map_type = WLSC_SURFACE_MAP_FULLSCREEN;
Kristian Høgsberg8dc378f2011-01-21 18:02:24 -0500958}
959
960static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500961surface_damage(struct wl_client *client,
962 struct wl_surface *surface,
963 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500964{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400965 struct wlsc_surface *es = (struct wlsc_surface *) surface;
966
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500967 wlsc_surface_damage_rectangle(es, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500968}
969
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500970const static struct wl_surface_interface surface_interface = {
971 surface_destroy,
972 surface_attach,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500973 surface_map_toplevel,
Kristian Høgsberg8dc378f2011-01-21 18:02:24 -0500974 surface_map_transient,
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500975 surface_map_fullscreen,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500976 surface_damage
977};
978
979static void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400980wlsc_input_device_attach(struct wlsc_input_device *device,
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200981 int x, int y, int width, int height)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400982{
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500983 wlsc_surface_damage(device->sprite);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400984
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400985 device->hotspot_x = x;
986 device->hotspot_y = y;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400987
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500988 device->sprite->x = device->input_device.x - device->hotspot_x;
989 device->sprite->y = device->input_device.y - device->hotspot_y;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200990 device->sprite->width = width;
991 device->sprite->height = height;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400992 wlsc_surface_update_matrix(device->sprite);
993
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500994 wlsc_surface_damage(device->sprite);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400995}
996
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200997static void
998wlsc_input_device_attach_buffer(struct wlsc_input_device *device,
999 struct wl_buffer *buffer, int x, int y)
1000{
1001 wlsc_buffer_attach(buffer, &device->sprite->surface);
1002 wlsc_input_device_attach(device, x, y, buffer->width, buffer->height);
1003}
1004
1005static void
1006wlsc_input_device_attach_sprite(struct wlsc_input_device *device,
1007 struct wlsc_sprite *sprite, int x, int y)
1008{
1009 wlsc_sprite_attach(sprite, &device->sprite->surface);
1010 wlsc_input_device_attach(device, x, y, sprite->width, sprite->height);
1011}
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001012
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001013void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001014wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
1015 enum wlsc_pointer_type type)
1016{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001017 struct wlsc_compositor *compositor =
1018 (struct wlsc_compositor *) device->input_device.compositor;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001019
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001020 wlsc_input_device_attach_sprite(device,
1021 compositor->pointer_sprites[type],
1022 pointer_images[type].hotspot_x,
1023 pointer_images[type].hotspot_y);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001024}
1025
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001026static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001027compositor_create_surface(struct wl_client *client,
1028 struct wl_compositor *compositor, uint32_t id)
1029{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001030 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001031 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001032
Kristian Høgsbergc5d6be92011-01-14 16:22:37 -05001033 surface = wlsc_surface_create(ec, 0, 0, 0, 0);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001034 if (surface == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -04001035 wl_client_post_no_memory(client);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001036 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001037 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001038
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001039 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001040
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001041 surface->surface.resource.object.id = id;
1042 surface->surface.resource.object.interface = &wl_surface_interface;
1043 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001044 (void (**)(void)) &surface_interface;
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001045 surface->surface.client = client;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001046
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001047 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001048}
1049
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001050const static struct wl_compositor_interface compositor_interface = {
1051 compositor_create_surface,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001052};
1053
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001054static void
1055wlsc_surface_transform(struct wlsc_surface *surface,
1056 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1057{
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001058 struct wlsc_vector v = { { x, y, 0, 1 } };
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001059
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001060 wlsc_matrix_transform(&surface->matrix_inv, &v);
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -04001061 *sx = v.f[0] * surface->width;
1062 *sy = v.f[1] * surface->height;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001063}
1064
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001065struct wlsc_surface *
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001066pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001067{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001068 struct wlsc_compositor *ec =
1069 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001070 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001071
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001072 wl_list_for_each(es, &ec->surface_list, link) {
1073 wlsc_surface_transform(es, device->x, device->y, sx, sy);
1074 if (0 <= *sx && *sx < es->width &&
1075 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001076 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001077 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001078
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001079 return NULL;
1080}
1081
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001082
1083static void
1084motion_grab_motion(struct wl_grab *grab,
1085 uint32_t time, int32_t x, int32_t y)
1086{
1087 struct wlsc_input_device *device =
1088 (struct wlsc_input_device *) grab->input_device;
1089 struct wlsc_surface *es =
1090 (struct wlsc_surface *) device->input_device.pointer_focus;
1091 int32_t sx, sy;
1092
1093 wlsc_surface_transform(es, x, y, &sx, &sy);
1094 wl_client_post_event(es->surface.client,
1095 &device->input_device.object,
1096 WL_INPUT_DEVICE_MOTION,
1097 time, x, y, sx, sy);
1098}
1099
1100static void
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001101motion_grab_button(struct wl_grab *grab,
1102 uint32_t time, int32_t button, int32_t state)
1103{
1104 wl_client_post_event(grab->input_device->pointer_focus->client,
1105 &grab->input_device->object,
1106 WL_INPUT_DEVICE_BUTTON,
1107 time, button, state);
1108}
1109
1110static void
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001111motion_grab_end(struct wl_grab *grab, uint32_t time)
1112{
1113}
1114
1115static const struct wl_grab_interface motion_grab_interface = {
1116 motion_grab_motion,
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001117 motion_grab_button,
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001118 motion_grab_end
1119};
1120
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001121WL_EXPORT void
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001122wlsc_compositor_wake(struct wlsc_compositor *compositor)
1123{
1124 if (compositor->idle_inhibit)
1125 return;
1126
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001127 wlsc_compositor_fade(compositor, 0.0);
1128 compositor->state = WLSC_COMPOSITOR_ACTIVE;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001129
1130 wl_event_source_timer_update(compositor->idle_source,
1131 option_idle_time * 1000);
1132}
1133
1134static void
1135wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor)
1136{
1137 wlsc_compositor_wake(compositor);
1138 compositor->idle_inhibit++;
1139}
1140
1141static void
1142wlsc_compositor_idle_release(struct wlsc_compositor *compositor)
1143{
1144 compositor->idle_inhibit--;
1145 wlsc_compositor_wake(compositor);
1146}
1147
1148static int
1149idle_handler(void *data)
1150{
1151 struct wlsc_compositor *compositor = data;
1152
1153 if (compositor->idle_inhibit)
1154 return 1;
1155
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001156 wlsc_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001157
1158 return 1;
1159}
1160
1161void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001162notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001163{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001164 struct wlsc_surface *es;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001165 struct wlsc_compositor *ec =
1166 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001167 struct wlsc_output *output;
Kristian Høgsberg6d65d5f2010-12-07 13:30:18 -05001168 const struct wl_grab_interface *interface;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001169 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsbergfc9c28a2010-12-07 13:04:43 -05001170 int32_t sx, sy;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001171 int x_valid = 0, y_valid = 0;
1172 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 -05001173
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001174 wlsc_compositor_wake(ec);
1175
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001176 wl_list_for_each(output, &ec->output_list, link) {
1177 if (output->x <= x && x <= output->x + output->width)
1178 x_valid = 1;
1179
1180 if (output->y <= y && y <= output->y + output->height)
1181 y_valid = 1;
1182
1183 /* FIXME: calculate this only on output addition/deletion */
1184 if (output->x < min_x)
1185 min_x = output->x;
1186 if (output->y < min_y)
1187 min_y = output->y;
1188
1189 if (output->x + output->width > max_x)
1190 max_x = output->x + output->width;
1191 if (output->y + output->height > max_y)
1192 max_y = output->y + output->height;
1193 }
1194
1195 if (!x_valid) {
1196 if (x < min_x)
1197 x = min_x;
1198 else if (x >= max_x)
1199 x = max_x;
1200 }
1201 if (!y_valid) {
1202 if (y < min_y)
1203 y = min_y;
1204 else if (y >= max_y)
1205 y = max_y;
1206 }
Ray Strode90e701d2008-12-18 23:05:43 -05001207
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -05001208 device->x = x;
1209 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001210
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001211 if (device->grab) {
1212 interface = device->grab->interface;
1213 interface->motion(device->grab, time, x, y);
1214 } else {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001215 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001216 wl_input_device_set_pointer_focus(device,
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001217 &es->surface,
Kristian Høgsberg26437072010-12-01 10:17:47 -05001218 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001219 if (es)
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001220 wl_client_post_event(es->surface.client,
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001221 &device->object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001222 WL_INPUT_DEVICE_MOTION,
1223 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001224 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001225
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001226 wlsc_surface_damage(wd->sprite);
1227
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001228 wd->sprite->x = device->x - wd->hotspot_x;
1229 wd->sprite->y = device->y - wd->hotspot_y;
1230 wlsc_surface_update_matrix(wd->sprite);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001231
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001232 wlsc_surface_damage(wd->sprite);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001233}
1234
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001235WL_EXPORT void
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001236wlsc_surface_activate(struct wlsc_surface *surface,
1237 struct wlsc_input_device *device, uint32_t time)
1238{
1239 wlsc_surface_raise(surface);
1240 if (device->selection)
1241 wlsc_selection_set_focus(device->selection,
1242 &surface->surface, time);
1243
1244 wl_input_device_set_keyboard_focus(&device->input_device,
1245 &surface->surface,
1246 time);
1247}
1248
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001249struct wlsc_binding {
1250 uint32_t key;
1251 uint32_t button;
1252 uint32_t modifier;
1253 wlsc_binding_handler_t handler;
1254 void *data;
1255 struct wl_list link;
1256};
1257
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001258void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001259notify_button(struct wl_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001260 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001261{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001262 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001263 struct wlsc_compositor *compositor =
1264 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001265 struct wlsc_binding *b;
1266 struct wlsc_surface *surface =
1267 (struct wlsc_surface *) device->pointer_focus;
Kristian Høgsbergea081152010-12-07 08:59:51 -05001268
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001269 if (state)
1270 wlsc_compositor_idle_inhibit(compositor);
1271 else
1272 wlsc_compositor_idle_release(compositor);
1273
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -05001274 if (state && surface && device->grab == NULL) {
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001275 wlsc_surface_activate(surface, wd, time);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001276 wl_input_device_start_grab(device,
1277 &device->motion_grab,
1278 button, time);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001279 }
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -05001280
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001281 wl_list_for_each(b, &compositor->binding_list, link) {
1282 if (b->button == button &&
1283 b->modifier == wd->modifier_state && state) {
1284 b->handler(&wd->input_device,
1285 time, 0, button, state, b->data);
1286 break;
1287 }
Benjamin Franzke5a2218a2011-02-01 16:30:31 +01001288 }
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001289
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -05001290 if (device->grab)
1291 device->grab->interface->button(device->grab, time,
1292 button, state);
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -05001293
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001294 if (!state && device->grab && device->grab_button == button)
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001295 wl_input_device_end_grab(device, time);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001296}
1297
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001298static void
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001299terminate_binding(struct wl_input_device *device, uint32_t time,
1300 uint32_t key, uint32_t button, uint32_t state, void *data)
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001301{
1302 struct wlsc_compositor *compositor = data;
1303
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001304 if (state)
1305 wl_display_terminate(compositor->wl_display);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001306}
1307
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001308WL_EXPORT struct wlsc_binding *
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001309wlsc_compositor_add_binding(struct wlsc_compositor *compositor,
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001310 uint32_t key, uint32_t button, uint32_t modifier,
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001311 wlsc_binding_handler_t handler, void *data)
1312{
1313 struct wlsc_binding *binding;
1314
1315 binding = malloc(sizeof *binding);
1316 if (binding == NULL)
1317 return NULL;
1318
1319 binding->key = key;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001320 binding->button = button;
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001321 binding->modifier = modifier;
1322 binding->handler = handler;
1323 binding->data = data;
1324 wl_list_insert(compositor->binding_list.prev, &binding->link);
1325
1326 return binding;
1327}
1328
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001329WL_EXPORT void
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001330wlsc_binding_destroy(struct wlsc_binding *binding)
1331{
1332 wl_list_remove(&binding->link);
1333 free(binding);
1334}
1335
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001336static void
1337update_modifier_state(struct wlsc_input_device *device,
1338 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001339{
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001340 uint32_t modifier;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001341
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001342 switch (key) {
1343 case KEY_LEFTCTRL:
1344 case KEY_RIGHTCTRL:
1345 modifier = MODIFIER_CTRL;
1346 break;
1347
1348 case KEY_LEFTALT:
1349 case KEY_RIGHTALT:
1350 modifier = MODIFIER_ALT;
1351 break;
1352
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -04001353 case KEY_LEFTMETA:
1354 case KEY_RIGHTMETA:
1355 modifier = MODIFIER_SUPER;
1356 break;
1357
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001358 default:
1359 modifier = 0;
1360 break;
1361 }
1362
1363 if (state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001364 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001365 else
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001366 device->modifier_state &= ~modifier;
1367}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001368
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001369void
1370notify_key(struct wl_input_device *device,
1371 uint32_t time, uint32_t key, uint32_t state)
1372{
1373 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1374 struct wlsc_compositor *compositor =
1375 (struct wlsc_compositor *) device->compositor;
1376 uint32_t *k, *end;
1377 struct wlsc_binding *b;
1378
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001379 if (state)
1380 wlsc_compositor_idle_inhibit(compositor);
1381 else
1382 wlsc_compositor_idle_release(compositor);
1383
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001384 wl_list_for_each(b, &compositor->binding_list, link) {
1385 if (b->key == key &&
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001386 b->modifier == wd->modifier_state) {
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001387 b->handler(&wd->input_device,
1388 time, key, 0, state, b->data);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001389 break;
1390 }
1391 }
1392
1393 update_modifier_state(wd, key, state);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001394 end = device->keys.data + device->keys.size;
1395 for (k = device->keys.data; k < end; k++) {
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001396 if (*k == key)
1397 *k = *--end;
1398 }
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001399 device->keys.size = (void *) end - device->keys.data;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001400 if (state) {
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001401 k = wl_array_add(&device->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001402 *k = key;
1403 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001404
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001405 if (device->keyboard_focus != NULL)
1406 wl_client_post_event(device->keyboard_focus->client,
1407 &device->object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001408 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001409}
1410
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001411void
1412notify_pointer_focus(struct wl_input_device *device,
1413 uint32_t time, struct wlsc_output *output,
1414 int32_t x, int32_t y)
1415{
1416 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1417 struct wlsc_compositor *compositor =
1418 (struct wlsc_compositor *) device->compositor;
1419 struct wlsc_surface *es;
1420 int32_t sx, sy;
1421
1422 if (output) {
1423 device->x = x;
1424 device->y = y;
1425 es = pick_surface(device, &sx, &sy);
1426 wl_input_device_set_pointer_focus(device,
1427 &es->surface,
1428 time, x, y, sx, sy);
1429
1430 compositor->focus = 1;
1431
1432 wd->sprite->x = device->x - wd->hotspot_x;
1433 wd->sprite->y = device->y - wd->hotspot_y;
1434 wlsc_surface_update_matrix(wd->sprite);
1435 } else {
1436 wl_input_device_set_pointer_focus(device, NULL,
1437 time, 0, 0, 0, 0);
1438 compositor->focus = 0;
1439 }
1440
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001441 wlsc_surface_damage(wd->sprite);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001442}
1443
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001444void
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001445notify_keyboard_focus(struct wl_input_device *device,
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001446 uint32_t time, struct wlsc_output *output,
1447 struct wl_array *keys)
1448{
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001449 struct wlsc_input_device *wd =
1450 (struct wlsc_input_device *) device;
1451 struct wlsc_compositor *compositor =
1452 (struct wlsc_compositor *) device->compositor;
1453 struct wlsc_surface *es;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001454 uint32_t *k, *end;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001455
1456 if (!wl_list_empty(&compositor->surface_list))
1457 es = container_of(compositor->surface_list.next,
1458 struct wlsc_surface, link);
1459 else
1460 es = NULL;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001461
1462 if (output) {
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001463 wl_array_copy(&wd->input_device.keys, keys);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001464 wd->modifier_state = 0;
1465 end = device->keys.data + device->keys.size;
1466 for (k = device->keys.data; k < end; k++) {
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001467 wlsc_compositor_idle_inhibit(compositor);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001468 update_modifier_state(wd, *k, 1);
1469 }
1470
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001471 wl_input_device_set_keyboard_focus(&wd->input_device,
1472 &es->surface, time);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001473 } else {
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001474 end = device->keys.data + device->keys.size;
1475 for (k = device->keys.data; k < end; k++)
1476 wlsc_compositor_idle_release(compositor);
1477
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001478 wd->modifier_state = 0;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001479 wl_input_device_set_keyboard_focus(&wd->input_device,
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001480 NULL, time);
1481 }
1482}
1483
1484
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001485static void
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001486input_device_attach(struct wl_client *client,
1487 struct wl_input_device *device_base,
1488 uint32_t time,
1489 struct wl_buffer *buffer, int32_t x, int32_t y)
1490{
1491 struct wlsc_input_device *device =
1492 (struct wlsc_input_device *) device_base;
1493
1494 if (time < device->input_device.pointer_focus_time)
1495 return;
1496 if (device->input_device.pointer_focus == NULL)
1497 return;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001498 if (device->input_device.pointer_focus->client != client)
1499 return;
1500
1501 if (buffer == NULL) {
1502 wlsc_input_device_set_pointer_image(device,
1503 WLSC_POINTER_LEFT_PTR);
1504 return;
1505 }
1506
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001507 wlsc_input_device_attach_buffer(device, buffer, x, y);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001508}
1509
1510const static struct wl_input_device_interface input_device_interface = {
1511 input_device_attach,
1512};
1513
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001514void
1515wlsc_input_device_init(struct wlsc_input_device *device,
1516 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001517{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001518 wl_input_device_init(&device->input_device, &ec->compositor);
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05001519
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001520 device->input_device.object.interface = &wl_input_device_interface;
1521 device->input_device.object.implementation =
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001522 (void (**)(void)) &input_device_interface;
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001523 wl_display_add_object(ec->wl_display, &device->input_device.object);
1524 wl_display_add_global(ec->wl_display, &device->input_device.object, NULL);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001525
Kristian Høgsbergc5d6be92011-01-14 16:22:37 -05001526 device->sprite = wlsc_surface_create(ec,
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001527 device->input_device.x,
1528 device->input_device.y, 32, 32);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001529 device->hotspot_x = 16;
1530 device->hotspot_y = 16;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001531 device->modifier_state = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001532
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001533 device->input_device.motion_grab.interface = &motion_grab_interface;
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001534
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001535 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001536
1537 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001538}
1539
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001540static void
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001541wlsc_output_post_geometry(struct wl_client *client,
1542 struct wl_object *global, uint32_t version)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001543{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001544 struct wlsc_output *output =
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001545 container_of(global, struct wlsc_output, object);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001546
1547 wl_client_post_event(client, global,
1548 WL_OUTPUT_GEOMETRY,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001549 output->x, output->y,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001550 output->width, output->height);
1551}
1552
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001553static const char vertex_shader[] =
1554 "uniform mat4 proj;\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001555 "attribute vec2 position;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001556 "attribute vec2 texcoord;\n"
1557 "varying vec2 v_texcoord;\n"
1558 "void main()\n"
1559 "{\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001560 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001561 " v_texcoord = texcoord;\n"
1562 "}\n";
1563
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001564static const char texture_fragment_shader[] =
Kristian Høgsbergdfce71d2010-12-07 20:19:10 -05001565 "precision mediump float;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001566 "varying vec2 v_texcoord;\n"
1567 "uniform sampler2D tex;\n"
1568 "void main()\n"
1569 "{\n"
1570 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1571 "}\n";
1572
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001573static const char solid_fragment_shader[] =
1574 "precision mediump float;\n"
1575 "varying vec2 v_texcoord;\n"
1576 "uniform vec4 color;\n"
1577 "void main()\n"
1578 "{\n"
1579 " gl_FragColor = color\n;"
1580 "}\n";
1581
Kristian Høgsberga9468212010-06-14 21:03:11 -04001582static int
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001583compile_shader(GLenum type, const char *source)
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001584{
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001585 GLuint s;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001586 char msg[512];
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001587 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001588
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001589 s = glCreateShader(type);
1590 glShaderSource(s, 1, &source, NULL);
1591 glCompileShader(s);
1592 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001593 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001594 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1595 fprintf(stderr, "shader info: %s\n", msg);
1596 return GL_NONE;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001597 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001598
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001599 return s;
1600}
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001601
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001602static int
1603wlsc_shader_init(struct wlsc_shader *shader,
1604 const char *vertex_source, const char *fragment_source)
1605{
1606 char msg[512];
1607 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001608
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001609 shader->vertex_shader =
1610 compile_shader(GL_VERTEX_SHADER, vertex_source);
1611 shader->fragment_shader =
1612 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1613
1614 shader->program = glCreateProgram();
1615 glAttachShader(shader->program, shader->vertex_shader);
1616 glAttachShader(shader->program, shader->fragment_shader);
1617 glBindAttribLocation(shader->program, 0, "position");
1618 glBindAttribLocation(shader->program, 1, "texcoord");
1619
1620 glLinkProgram(shader->program);
1621 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001622 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001623 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001624 fprintf(stderr, "link info: %s\n", msg);
1625 return -1;
1626 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001627
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001628 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1629 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001630
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001631 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001632}
1633
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001634static int
1635init_solid_shader(struct wlsc_shader *shader,
1636 GLuint vertex_shader, const char *fragment_source)
1637{
1638 GLint status;
1639 char msg[512];
1640
1641 shader->vertex_shader = vertex_shader;
1642 shader->fragment_shader =
1643 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1644
1645 shader->program = glCreateProgram();
1646 glAttachShader(shader->program, shader->vertex_shader);
1647 glAttachShader(shader->program, shader->fragment_shader);
1648 glBindAttribLocation(shader->program, 0, "position");
1649 glBindAttribLocation(shader->program, 1, "texcoord");
1650
1651 glLinkProgram(shader->program);
1652 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1653 if (!status) {
1654 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1655 fprintf(stderr, "link info: %s\n", msg);
1656 return -1;
1657 }
1658
1659 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1660 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1661
1662 return 0;
1663}
1664
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001665void
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001666wlsc_output_destroy(struct wlsc_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001667{
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001668 destroy_surface(&output->background->surface.resource, NULL);
1669}
1670
1671void
1672wlsc_output_move(struct wlsc_output *output, int x, int y)
1673{
1674 struct wlsc_compositor *c = output->compositor;
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01001675 int flip;
1676
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001677 output->x = x;
1678 output->y = y;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001679
Benjamin Franzke264b3f92011-03-16 13:48:42 +01001680 if (output->background) {
1681 output->background->x = x;
1682 output->background->y = y;
1683 wlsc_surface_update_matrix(output->background);
1684 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001685
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001686 pixman_region32_init(&output->previous_damage_region);
1687
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001688 wlsc_matrix_init(&output->matrix);
1689 wlsc_matrix_translate(&output->matrix,
1690 -output->x - output->width / 2.0,
1691 -output->y - output->height / 2.0, 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01001692
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001693 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001694 wlsc_matrix_scale(&output->matrix,
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01001695 2.0 / output->width,
1696 flip * 2.0 / output->height, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001697
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001698 pixman_region32_union_rect(&c->damage_region,
1699 &c->damage_region,
1700 x, y, output->width, output->height);
1701}
1702
1703void
1704wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1705 int x, int y, int width, int height, uint32_t flags)
1706{
1707 output->compositor = c;
1708 output->x = x;
1709 output->y = y;
1710 output->width = width;
1711 output->height = height;
1712
1713 output->background =
1714 background_create(output, option_background);
1715
1716 output->flags = flags;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001717 output->finished = 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001718 wlsc_output_move(output, x, y);
1719
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001720 output->object.interface = &wl_output_interface;
1721 wl_display_add_object(c->wl_display, &output->object);
1722 wl_display_add_global(c->wl_display, &output->object,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001723 wlsc_output_post_geometry);
1724}
1725
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001726static void
1727shm_buffer_created(struct wl_buffer *buffer)
1728{
1729 struct wl_list *surfaces_attached_to;
1730
1731 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1732 if (!surfaces_attached_to) {
1733 buffer->user_data = NULL;
1734 return;
1735 }
1736
1737 wl_list_init(surfaces_attached_to);
1738
1739 buffer->user_data = surfaces_attached_to;
1740}
1741
1742static void
1743shm_buffer_damaged(struct wl_buffer *buffer,
1744 int32_t x, int32_t y, int32_t width, int32_t height)
1745{
1746 struct wl_list *surfaces_attached_to = buffer->user_data;
1747 struct wlsc_surface *es;
Benjamin Franzkefab5ec12011-04-25 19:44:47 +02001748 GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001749
1750 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1751 glBindTexture(GL_TEXTURE_2D, es->texture);
1752 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Benjamin Franzkefab5ec12011-04-25 19:44:47 +02001753 tex_width, buffer->height, 0,
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001754 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1755 wl_shm_buffer_get_data(buffer));
1756 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
1757 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
1758 }
1759}
1760
1761static void
1762shm_buffer_destroyed(struct wl_buffer *buffer)
1763{
1764 struct wl_list *surfaces_attached_to = buffer->user_data;
1765 struct wlsc_surface *es, *next;
1766
1767 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
1768 es->buffer = NULL;
1769 wl_list_remove(&es->buffer_link);
1770 }
1771
1772 free(surfaces_attached_to);
1773}
1774
1775const static struct wl_shm_callbacks shm_callbacks = {
1776 shm_buffer_created,
1777 shm_buffer_damaged,
1778 shm_buffer_destroyed
1779};
1780
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001781int
1782wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1783{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001784 struct wl_event_loop *loop;
Kristian Høgsbergd711d0c2011-01-14 17:28:21 -05001785 const char *extensions;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001786
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001787 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001788
Kristian Høgsbergc5c510e2010-12-08 15:12:58 -05001789 wl_compositor_init(&ec->compositor, &compositor_interface, display);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001790
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001791 ec->shm = wl_shm_init(display, &shm_callbacks);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04001792
1793 ec->image_target_texture_2d =
1794 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1795 ec->image_target_renderbuffer_storage = (void *)
1796 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1797 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1798 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1799 ec->bind_display =
1800 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1801 ec->unbind_display =
1802 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1803
1804 extensions = (const char *) glGetString(GL_EXTENSIONS);
1805 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1806 fprintf(stderr,
1807 "GL_EXT_texture_format_BGRA8888 not available\n");
1808 return -1;
1809 }
1810
1811 extensions =
1812 (const char *) eglQueryString(ec->display, EGL_EXTENSIONS);
1813 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1814 ec->has_bind_display = 1;
1815 if (ec->has_bind_display)
1816 ec->bind_display(ec->display, ec->wl_display);
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04001817
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001818 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001819 wl_list_init(&ec->input_device_list);
1820 wl_list_init(&ec->output_list);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001821 wl_list_init(&ec->binding_list);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001822 wl_list_init(&ec->animation_list);
1823 wlsc_tweener_init(&ec->fade.tweener, 0.8, 0.0, 0.0);
1824 ec->fade.animation.frame = fade_frame;
1825 wl_list_init(&ec->fade.animation.link);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001826
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001827 wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0,
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001828 MODIFIER_CTRL | MODIFIER_ALT,
1829 terminate_binding, ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001830
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001831 create_pointer_images(ec);
1832
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001833 screenshooter_create(ec);
1834
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001835 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001836
1837 if (wlsc_shader_init(&ec->texture_shader,
1838 vertex_shader, texture_fragment_shader) < 0)
Kristian Høgsberga9468212010-06-14 21:03:11 -04001839 return -1;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001840 if (init_solid_shader(&ec->solid_shader,
1841 ec->texture_shader.vertex_shader,
1842 solid_fragment_shader) < 0)
1843 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001844
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001845 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001846 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
1847 wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000);
1848
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001849 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001850 pixman_region32_init(&ec->damage_region);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001851 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001852
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001853 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001854}
1855
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001856static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001857{
1858 struct wlsc_compositor *ec = data;
1859
1860 wl_display_terminate(ec->wl_display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001861
1862 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001863}
1864
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001865int main(int argc, char *argv[])
1866{
1867 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001868 struct wlsc_compositor *ec;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001869 struct wl_event_loop *loop;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04001870 int width, height, o;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001871 void *shell_module;
1872 int (*shell_init)(struct wlsc_compositor *ec);
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04001873 char *p;
1874
1875 static const char opts[] = "b:c:g:S:i:s:";
1876 static const struct option longopts[ ] = {
1877 { "background", 1, NULL, 'b' },
1878 { "connector", 1, NULL, 'c' },
1879 { "geometry", 1, NULL, 'g' },
1880 { "socket", 1, NULL, 'S' },
1881 { "idle-time", 1, NULL, 'i' },
1882 { "shell", 1, NULL, 's' },
1883 { NULL, }
1884 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001885
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001886 g_type_init(); /* GdkPixbuf needs this, it seems. */
1887
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04001888 width = 1024;
1889 height = 640;
1890
1891 while (o = getopt_long(argc, argv, opts, longopts, &o), o > 0) {
1892 switch (o) {
1893 case 'b':
1894 option_background = optarg;
1895 break;
1896 case 'c':
1897 option_connector = strtol(optarg, &p, 0);
1898 if (*p != '\0') {
1899 fprintf(stderr,
1900 "invalid connection option: %s\n",
1901 optarg);
1902 exit(EXIT_FAILURE);
1903 }
1904 break;
1905 case 'g':
1906 if (sscanf(optarg, "%dx%d", &width, &height) != 2) {
1907 fprintf(stderr,
1908 "invalid geometry option: %s\n",
1909 optarg);
1910 exit(EXIT_FAILURE);
1911 }
1912 break;
1913 case 'S':
1914 option_socket_name = optarg;
1915 break;
1916 case 'i':
1917 option_idle_time = strtol(optarg, &p, 0);
1918 if (*p != '\0') {
1919 fprintf(stderr,
1920 "invalid idle time option: %s\n",
1921 optarg);
1922 exit(EXIT_FAILURE);
1923 }
1924 break;
1925 case 's':
1926 option_shell = optarg;
1927 break;
1928 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001929 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001930
1931 display = wl_display_create();
1932
Kristian Høgsberga9410222011-01-14 17:22:35 -05001933 ec = NULL;
1934
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001935 shell_init = desktop_shell_init;
1936 if (option_shell) {
1937 shell_module = dlopen(option_shell, RTLD_LAZY);
1938 if (!shell_module) {
1939 fprintf(stderr, "failed to load shell module: %m\n");
1940 exit(EXIT_FAILURE);
1941 }
1942 shell_init = dlsym(shell_module, "shell_init");
1943 if (!shell_init) {
1944 fprintf(stderr,
1945 "failed to lookup shell init function: %m\n");
1946 exit(EXIT_FAILURE);
1947 }
1948 }
1949
Kristian Høgsberga9410222011-01-14 17:22:35 -05001950#if BUILD_WAYLAND_COMPOSITOR
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001951 if (getenv("WAYLAND_DISPLAY"))
1952 ec = wayland_compositor_create(display, width, height);
Kristian Høgsberga9410222011-01-14 17:22:35 -05001953#endif
1954
1955#if BUILD_X11_COMPOSITOR
1956 if (ec == NULL && getenv("DISPLAY"))
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001957 ec = x11_compositor_create(display, width, height);
Kristian Høgsberga9410222011-01-14 17:22:35 -05001958#endif
1959
Benjamin Franzke5d007092011-04-04 00:30:25 +02001960#if BUILD_OPENWFD_COMPOSITOR
1961 if (ec == NULL && getenv("OPENWFD"))
1962 ec = wfd_compositor_create(display, option_connector);
1963#endif
1964
Kristian Høgsberga9410222011-01-14 17:22:35 -05001965#if BUILD_DRM_COMPOSITOR
1966 if (ec == NULL)
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001967 ec = drm_compositor_create(display, option_connector);
Kristian Høgsberga9410222011-01-14 17:22:35 -05001968#endif
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001969
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001970 if (ec == NULL) {
1971 fprintf(stderr, "failed to create compositor\n");
1972 exit(EXIT_FAILURE);
1973 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001974
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001975 if (shell_init(ec) < 0)
1976 exit(EXIT_FAILURE);
1977
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001978 if (wl_display_add_socket(display, option_socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001979 fprintf(stderr, "failed to add socket: %m\n");
1980 exit(EXIT_FAILURE);
1981 }
1982
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001983 loop = wl_display_get_event_loop(ec->wl_display);
1984 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
1985 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec);
1986
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001987 wl_display_run(display);
1988
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04001989 if (ec->has_bind_display)
1990 ec->unbind_display(ec->display, display);
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001991 wl_display_destroy(display);
1992
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001993 ec->destroy(ec);
1994
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001995 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001996}