blob: 9be57dfd659fd9a319b5a6ca19f02d6d821c6de2 [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øgsberg890bc052008-12-30 14:31:33 -050035
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036#include "wayland-server.h"
Kristian Høgsberg82863022010-06-04 21:52:02 -040037#include "compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050038
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010039/* The plan here is to generate a random anonymous socket name and
40 * advertise that through a service on the session dbus.
41 */
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -050042static const char *option_socket_name = NULL;
Kristian Høgsberg61a82512010-10-26 11:26:44 -040043static const char *option_background = "background.jpg";
44static const char *option_geometry = "1024x640";
Kristian Høgsberge10a5d92011-04-22 14:01:18 -040045static int option_idle_time = 5;
Kristian Høgsberg61a82512010-10-26 11:26:44 -040046static int option_connector = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050047
48static const GOptionEntry option_entries[] = {
49 { "background", 'b', 0, G_OPTION_ARG_STRING,
50 &option_background, "Background image" },
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -040051 { "connector", 'c', 0, G_OPTION_ARG_INT,
52 &option_connector, "KMS connector" },
Kristian Høgsberg61a82512010-10-26 11:26:44 -040053 { "geometry", 'g', 0, G_OPTION_ARG_STRING,
54 &option_geometry, "Geometry" },
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010055 { "socket", 's', 0, G_OPTION_ARG_STRING,
56 &option_socket_name, "Socket Name" },
Kristian Høgsberge10a5d92011-04-22 14:01:18 -040057 { "idle-time", 'i', 0, G_OPTION_ARG_INT,
58 &option_idle_time, "Screensaver idle time" },
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050059 { NULL }
60};
61
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050062static void
63wlsc_matrix_init(struct wlsc_matrix *matrix)
64{
65 static const struct wlsc_matrix identity = {
66 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
67 };
68
69 memcpy(matrix, &identity, sizeof identity);
70}
71
72static void
73wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
74{
75 struct wlsc_matrix tmp;
Kristian Høgsberg27803c62010-06-06 22:23:21 -040076 const GLfloat *row, *column;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050077 div_t d;
78 int i, j;
79
80 for (i = 0; i < 16; i++) {
81 tmp.d[i] = 0;
82 d = div(i, 4);
83 row = m->d + d.quot * 4;
84 column = n->d + d.rem;
85 for (j = 0; j < 4; j++)
86 tmp.d[i] += row[j] * column[j * 4];
87 }
88 memcpy(m, &tmp, sizeof tmp);
89}
90
91static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040092wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050093{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -050094 struct wlsc_matrix translate = {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050095 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
96 };
97
98 wlsc_matrix_multiply(matrix, &translate);
99}
100
101static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400102wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500103{
104 struct wlsc_matrix scale = {
105 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
106 };
107
108 wlsc_matrix_multiply(matrix, &scale);
109}
110
111static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400112wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
113{
114 int i, j;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400115 struct wlsc_vector t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400116
117 for (i = 0; i < 4; i++) {
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400118 t.f[i] = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400119 for (j = 0; j < 4; j++)
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400120 t.f[i] += v->f[j] * matrix->d[i + j * 4];
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400121 }
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500122
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400123 *v = t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400124}
125
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400126void
127wlsc_tweener_init(struct wlsc_tweener *tweener,
128 double k, double current, double target)
129{
130 tweener->k = k;
131 tweener->current = current;
132 tweener->previous = current;
133 tweener->target = target;
134}
135
136void
137wlsc_tweener_update(struct wlsc_tweener *tweener, uint32_t msec)
138{
139 double force, current, step;
140
141 step = (msec - tweener->timestamp) / 100.0;
142 tweener->timestamp = msec;
143
144 current = tweener->current;
145 force = tweener->k * (tweener->target - current) / 10.0 +
146 (tweener->previous - current);
147
148 tweener->current =
149 current + (current - tweener->previous) + force * step * step;
150 tweener->previous = current;
151
152 if (tweener->current >= 1.0) {
153 tweener->current = 1.0;
154 tweener->previous = 1.0;
155 }
156
157 if (tweener->current <= 0.0) {
158 tweener->current = 0.0;
159 tweener->previous = 0.0;
160 }
161}
162
163int
164wlsc_tweener_done(struct wlsc_tweener *tweener)
165{
166 return fabs(tweener->previous - tweener->target) < 0.0002 &&
167 fabs(tweener->current - tweener->target) < 0.0002;
168}
169
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200170struct wlsc_surface *
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400171wlsc_surface_create(struct wlsc_compositor *compositor,
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400172 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500173{
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400174 struct wlsc_surface *surface;
175
176 surface = malloc(sizeof *surface);
177 if (surface == NULL)
178 return NULL;
179
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500180 wl_list_init(&surface->surface.destroy_listener_list);
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500181 wl_list_init(&surface->link);
Benjamin Franzkebab41fb2011-03-07 18:04:59 +0100182 wl_list_init(&surface->buffer_link);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500183 surface->map_type = WLSC_SURFACE_MAP_UNMAPPED;
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500184
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500185 glGenTextures(1, &surface->texture);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400186 glBindTexture(GL_TEXTURE_2D, surface->texture);
187 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
188 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
189 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
190 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
191
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500192 surface->compositor = compositor;
Kristian Høgsbergc5d6be92011-01-14 16:22:37 -0500193 surface->visual = NULL;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200194 surface->image = EGL_NO_IMAGE_KHR;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200195 surface->saved_texture = 0;
Benjamin Franzkebab41fb2011-03-07 18:04:59 +0100196 surface->buffer = NULL;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400197 surface->x = x;
198 surface->y = y;
199 surface->width = width;
200 surface->height = height;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400201 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400202 wlsc_matrix_scale(&surface->matrix, width, height, 1);
203 wlsc_matrix_translate(&surface->matrix, x, y, 0);
204
205 wlsc_matrix_init(&surface->matrix_inv);
206 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
207 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500208
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400209 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500210}
211
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500212void
213wlsc_surface_damage_rectangle(struct wlsc_surface *surface,
214 int32_t x, int32_t y,
215 int32_t width, int32_t height)
216{
217 struct wlsc_compositor *compositor = surface->compositor;
218
219 pixman_region32_union_rect(&compositor->damage_region,
220 &compositor->damage_region,
221 surface->x + x, surface->y + y,
222 width, height);
223 wlsc_compositor_schedule_repaint(compositor);
224}
225
226void
227wlsc_surface_damage(struct wlsc_surface *surface)
228{
229 wlsc_surface_damage_rectangle(surface, 0, 0,
230 surface->width, surface->height);
231}
232
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500233uint32_t
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500234get_time(void)
235{
236 struct timeval tv;
237
238 gettimeofday(&tv, NULL);
239
240 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
241}
242
Kristian Høgsberg54879822008-11-23 17:07:32 -0500243static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400244destroy_surface(struct wl_resource *resource, struct wl_client *client)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500245{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400246 struct wlsc_surface *surface =
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500247 container_of(resource, struct wlsc_surface, surface.resource);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500248 struct wl_listener *l, *next;
Kristian Høgsberg4685fa32010-12-06 21:38:50 -0500249 uint32_t time;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400250
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500251 wlsc_surface_damage(surface);
252
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400253 wl_list_remove(&surface->link);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200254 if (surface->saved_texture == 0)
255 glDeleteTextures(1, &surface->texture);
256 else
257 glDeleteTextures(1, &surface->saved_texture);
258
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400259
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200260 if (surface->image != EGL_NO_IMAGE_KHR)
261 eglDestroyImageKHR(surface->compositor->display,
262 surface->image);
263
Benjamin Franzkebab41fb2011-03-07 18:04:59 +0100264 wl_list_remove(&surface->buffer_link);
265
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500266 time = get_time();
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500267 wl_list_for_each_safe(l, next,
268 &surface->surface.destroy_listener_list, link)
Kristian Høgsberg4685fa32010-12-06 21:38:50 -0500269 l->func(l, &surface->surface, time);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400270
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400271 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500272}
273
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500274uint32_t *
275wlsc_load_image(const char *filename, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500276{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400277 GdkPixbuf *pixbuf;
278 GError *error = NULL;
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500279 int stride, i, n_channels;
280 unsigned char *pixels, *end, *argb_pixels, *s, *d;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500281
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400282 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
283 width, height,
284 FALSE, &error);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500285 if (error != NULL) {
286 fprintf(stderr, "failed to load image: %s\n", error->message);
287 g_error_free(error);
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500288 return NULL;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500289 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400290
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500291 stride = gdk_pixbuf_get_rowstride(pixbuf);
292 pixels = gdk_pixbuf_get_pixels(pixbuf);
293 n_channels = gdk_pixbuf_get_n_channels(pixbuf);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400294
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500295 argb_pixels = malloc (height * width * 4);
296 if (argb_pixels == NULL) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500297 g_object_unref(pixbuf);
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500298 return NULL;
299 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400300
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500301 if (n_channels == 4) {
302 for (i = 0; i < height; i++) {
303 s = pixels + i * stride;
304 end = s + width * 4;
305 d = argb_pixels + i * width * 4;
306 while (s < end) {
307 unsigned int t;
308
309#define MULT(_d,c,a,t) \
310 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
311
312 MULT(d[0], s[2], s[3], t);
313 MULT(d[1], s[1], s[3], t);
314 MULT(d[2], s[0], s[3], t);
315 d[3] = s[3];
316 s += 4;
317 d += 4;
318 }
319 }
320 } else if (n_channels == 3) {
321 for (i = 0; i < height; i++) {
322 s = pixels + i * stride;
323 end = s + width * 3;
324 d = argb_pixels + i * width * 4;
325 while (s < end) {
326 d[0] = s[2];
327 d[1] = s[1];
328 d[2] = s[0];
329 d[3] = 0xff;
330 s += 3;
331 d += 4;
332 }
333 }
334 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400335
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500336 g_object_unref(pixbuf);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400337
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500338 return (uint32_t *) argb_pixels;
339}
340
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100341static void
342wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
343{
344 struct wlsc_surface *es = (struct wlsc_surface *) surface;
345 struct wlsc_compositor *ec = es->compositor;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100346 struct wl_list *surfaces_attached_to;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100347
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100348 if (es->saved_texture != 0)
349 es->texture = es->saved_texture;
350
351 glBindTexture(GL_TEXTURE_2D, es->texture);
352
353 if (wl_buffer_is_shm(buffer)) {
354 /* Unbind any EGLImage texture that may be bound, so we don't
355 * overwrite it.*/
356 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
357 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
358 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
359 buffer->width, buffer->height, 0,
360 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
361 wl_shm_buffer_get_data(buffer));
362 es->visual = buffer->visual;
363
364 surfaces_attached_to = buffer->user_data;
365
366 if (es->buffer)
367 wl_list_remove(&es->buffer_link);
368 wl_list_insert(surfaces_attached_to, &es->buffer_link);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100369 } else {
Kristian Høgsbergdf2f1972011-04-21 23:48:13 -0400370 es->image = eglCreateImageKHR(ec->display, NULL,
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200371 EGL_WAYLAND_BUFFER_WL,
372 buffer, NULL);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100373
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200374 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, es->image);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100375 es->visual = buffer->visual;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100376 }
377}
378
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200379static void
380wlsc_sprite_attach(struct wlsc_sprite *sprite, struct wl_surface *surface)
381{
382 struct wlsc_surface *es = (struct wlsc_surface *) surface;
383
384 es->image = sprite->image;
385 if (sprite->image != EGL_NO_IMAGE_KHR) {
386 glBindTexture(GL_TEXTURE_2D, es->texture);
387 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, es->image);
388 } else {
389 if (es->saved_texture == 0)
390 es->saved_texture = es->texture;
391 es->texture = sprite->texture;
392 }
393
394 es->visual = sprite->visual;
395}
396
397enum sprite_usage {
398 SPRITE_USE_CURSOR = (1 << 0),
399};
400
401static struct wlsc_sprite *
402create_sprite_from_png(struct wlsc_compositor *ec,
403 const char *filename, int width, int height,
404 uint32_t usage)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500405{
406 uint32_t *pixels;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200407 struct wlsc_sprite *sprite;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500408
409 pixels = wlsc_load_image(filename, width, height);
Tim Wiederhakeac5c5e72011-01-27 01:32:36 +0100410 if(pixels == NULL)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200411 return NULL;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500412
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200413 sprite = malloc(sizeof *sprite);
414 if (sprite == NULL) {
415 free(pixels);
416 return NULL;
417 }
418
419 sprite->visual = &ec->compositor.premultiplied_argb_visual;
420 sprite->width = width;
421 sprite->height = height;
422 sprite->image = EGL_NO_IMAGE_KHR;
423
424 if (usage & SPRITE_USE_CURSOR && ec->create_cursor_image != NULL)
425 sprite->image = ec->create_cursor_image(ec, width, height);
426
427 glGenTextures(1, &sprite->texture);
428 glBindTexture(GL_TEXTURE_2D, sprite->texture);
429 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
430 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
431 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
432 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
433
434 if (sprite->image != EGL_NO_IMAGE_KHR) {
435 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, sprite->image);
436 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
437 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
438 } else {
439 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, width, height, 0,
440 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
441 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500442
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500443 free(pixels);
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500444
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200445 return sprite;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500446}
447
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400448static const struct {
449 const char *filename;
450 int hotspot_x, hotspot_y;
451} pointer_images[] = {
Kristian Høgsberg4219a402010-08-16 16:43:03 -0400452 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
453 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
454 { DATADIR "/wayland/bottom_side.png", 16, 20 },
455 { DATADIR "/wayland/grabbing.png", 20, 17 },
456 { DATADIR "/wayland/left_ptr.png", 10, 5 },
457 { DATADIR "/wayland/left_side.png", 10, 20 },
458 { DATADIR "/wayland/right_side.png", 30, 19 },
459 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
460 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
461 { DATADIR "/wayland/top_side.png", 18, 8 },
462 { DATADIR "/wayland/xterm.png", 15, 15 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400463};
464
465static void
466create_pointer_images(struct wlsc_compositor *ec)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500467{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400468 int i, count;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400469 const int width = 32, height = 32;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400470
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400471 count = ARRAY_LENGTH(pointer_images);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200472 ec->pointer_sprites = malloc(count * sizeof *ec->pointer_sprites);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400473 for (i = 0; i < count; i++) {
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200474 ec->pointer_sprites[i] =
475 create_sprite_from_png(ec,
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500476 pointer_images[i].filename,
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200477 width, height,
478 SPRITE_USE_CURSOR);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400479 }
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400480}
481
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500482static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500483background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500484{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500485 struct wlsc_surface *background;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200486 struct wlsc_sprite *sprite;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500487
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400488 background = wlsc_surface_create(output->compositor,
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400489 output->x, output->y,
490 output->width, output->height);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500491 if (background == NULL)
492 return NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500493
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200494 sprite = create_sprite_from_png(output->compositor, filename,
495 output->width, output->height, 0);
496 if (sprite == NULL) {
Benjamin Franzked3b023e2011-01-15 12:34:49 +0100497 free(background);
498 return NULL;
499 }
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100500
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200501 wlsc_sprite_attach(sprite, &background->surface);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500502
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500503 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500504}
505
506static void
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500507wlsc_surface_draw(struct wlsc_surface *es,
508 struct wlsc_output *output, pixman_region32_t *clip)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500509{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500510 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500511 GLfloat *v, inv_width, inv_height;
512 unsigned int *p;
513 pixman_region32_t repaint;
514 pixman_box32_t *rectangles;
515 int i, n;
516
517 pixman_region32_init_rect(&repaint,
518 es->x, es->y, es->width, es->height);
519 pixman_region32_intersect(&repaint, &repaint, clip);
520 if (!pixman_region32_not_empty(&repaint))
521 return;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500522
Kristian Høgsbergc5c510e2010-12-08 15:12:58 -0500523 if (es->visual == &ec->compositor.argb_visual) {
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500524 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
525 glEnable(GL_BLEND);
Kristian Høgsbergc5c510e2010-12-08 15:12:58 -0500526 } else if (es->visual == &ec->compositor.premultiplied_argb_visual) {
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500527 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
528 glEnable(GL_BLEND);
529 } else {
530 glDisable(GL_BLEND);
531 }
532
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500533 rectangles = pixman_region32_rectangles(&repaint, &n);
534 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
535 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
536 inv_width = 1.0 / es->width;
537 inv_height = 1.0 / es->height;
538 for (i = 0; i < n; i++, v += 16, p += 6) {
539 v[ 0] = rectangles[i].x1;
540 v[ 1] = rectangles[i].y1;
541 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
542 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500543
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500544 v[ 4] = rectangles[i].x1;
545 v[ 5] = rectangles[i].y2;
546 v[ 6] = v[ 2];
547 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500548
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500549 v[ 8] = rectangles[i].x2;
550 v[ 9] = rectangles[i].y1;
551 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
552 v[11] = v[ 3];
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500553
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500554 v[12] = rectangles[i].x2;
555 v[13] = rectangles[i].y2;
556 v[14] = v[10];
557 v[15] = v[ 7];
558
559 p[0] = i * 4 + 0;
560 p[1] = i * 4 + 1;
561 p[2] = i * 4 + 2;
562 p[3] = i * 4 + 2;
563 p[4] = i * 4 + 1;
564 p[5] = i * 4 + 3;
565 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500566
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500567 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500568 v = ec->vertices.data;
569 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
570 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400571 glEnableVertexAttribArray(0);
572 glEnableVertexAttribArray(1);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500573 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
574
575 ec->vertices.size = 0;
576 ec->indices.size = 0;
577 pixman_region32_fini(&repaint);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500578}
579
580static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400581wlsc_surface_raise(struct wlsc_surface *surface)
582{
583 struct wlsc_compositor *compositor = surface->compositor;
584
585 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400586 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400587}
588
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500589void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400590wlsc_surface_update_matrix(struct wlsc_surface *es)
591{
592 wlsc_matrix_init(&es->matrix);
593 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
594 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
595
596 wlsc_matrix_init(&es->matrix_inv);
597 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
598 wlsc_matrix_scale(&es->matrix_inv,
599 1.0 / es->width, 1.0 / es->height, 1);
600}
601
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400602void
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400603wlsc_compositor_damage_all(struct wlsc_compositor *compositor)
604{
605 struct wlsc_output *output;
606
607 wl_list_for_each(output, &compositor->output_list, link)
608 wlsc_output_damage(output);
609}
610
611void
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100612wlsc_output_finish_frame(struct wlsc_output *output, int msecs)
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400613{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100614 struct wlsc_compositor *compositor = output->compositor;
615 struct wlsc_surface *es;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400616 struct wlsc_animation *animation, *next;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100617
618 wl_list_for_each(es, &compositor->surface_list, link) {
619 if (es->output == output) {
620 wl_display_post_frame(compositor->wl_display,
621 &es->surface, msecs);
622 }
623 }
624
625 output->finished = 1;
626
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400627 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsbergf30c67e2011-02-06 12:58:44 -0500628 compositor->repaint_on_timeout = 1;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400629
630 wl_list_for_each_safe(animation, next,
631 &compositor->animation_list, link)
632 animation->frame(animation, output, msecs);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400633}
634
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400635void
636wlsc_output_damage(struct wlsc_output *output)
637{
638 struct wlsc_compositor *compositor = output->compositor;
639
640 pixman_region32_union_rect(&compositor->damage_region,
641 &compositor->damage_region,
642 output->x, output->y,
643 output->width, output->height);
644 wlsc_compositor_schedule_repaint(compositor);
645}
646
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400647static void
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400648fade_frame(struct wlsc_animation *animation,
649 struct wlsc_output *output, uint32_t msecs)
650{
651 struct wlsc_compositor *compositor =
652 container_of(animation,
653 struct wlsc_compositor, fade.animation);
654
655 wlsc_tweener_update(&compositor->fade.tweener, msecs);
656 if (wlsc_tweener_done(&compositor->fade.tweener)) {
657 if (compositor->fade.tweener.current > 0.999)
658 compositor->state = WLSC_COMPOSITOR_SLEEPING;
659 compositor->fade.tweener.current =
660 compositor->fade.tweener.target;
661 wl_list_remove(&animation->link);
662 wl_list_init(&animation->link);
663 }
664
665 wlsc_output_damage(output);
666}
667
668static void
669fade_output(struct wlsc_output *output,
670 GLfloat tint, pixman_region32_t *region)
671{
672 struct wlsc_compositor *compositor = output->compositor;
673 struct wlsc_surface surface;
674 GLfloat color[4] = { 0.0, 0.0, 0.0, tint };
675
676 surface.compositor = compositor;
677 surface.x = output->x;
678 surface.y = output->y;
679 surface.width = output->width;
680 surface.height = output->height;
681 surface.visual = &compositor->compositor.premultiplied_argb_visual;
682 surface.texture = GL_NONE;
683 glUseProgram(compositor->solid_shader.program);
684 glUniformMatrix4fv(compositor->solid_shader.proj_uniform,
685 1, GL_FALSE, output->matrix.d);
686 glUniform4fv(compositor->solid_shader.color_uniform, 1, color);
687 wlsc_surface_draw(&surface, output, region);
688}
689
690static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400691wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400692{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500693 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500694 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500695 struct wlsc_input_device *eid;
Kristian Høgsbergcfc6d272011-04-11 13:34:24 -0400696 pixman_region32_t new_damage, total_damage, repaint;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200697 int using_hardware_cursor = 1;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500698
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100699 output->prepare_render(output);
700
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500701 glViewport(0, 0, output->width, output->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500702
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -0400703 glUseProgram(ec->texture_shader.program);
704 glUniformMatrix4fv(ec->texture_shader.proj_uniform,
705 1, GL_FALSE, output->matrix.d);
706 glUniform1i(ec->texture_shader.tex_uniform, 0);
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500707
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500708 pixman_region32_init(&new_damage);
709 pixman_region32_init(&total_damage);
710 pixman_region32_intersect_rect(&new_damage,
711 &ec->damage_region,
712 output->x, output->y,
713 output->width, output->height);
714 pixman_region32_subtract(&ec->damage_region,
715 &ec->damage_region, &new_damage);
716 pixman_region32_union(&total_damage, &new_damage,
717 &output->previous_damage_region);
718 pixman_region32_copy(&output->previous_damage_region, &new_damage);
719
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400720 if (ec->state == WLSC_COMPOSITOR_SLEEPING) {
721 glClear(GL_COLOR_BUFFER_BIT);
722 return;
723 }
724
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200725 if (ec->focus)
726 if (output->set_hardware_cursor(output, ec->input_device) < 0)
727 using_hardware_cursor = 0;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400728 if (ec->fade.tweener.current > 0.001)
729 using_hardware_cursor = 0;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200730
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500731 es = container_of(ec->surface_list.next, struct wlsc_surface, link);
732 if (es->map_type == WLSC_SURFACE_MAP_FULLSCREEN &&
733 es->fullscreen_output == output) {
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200734 if (es->visual == &ec->compositor.rgb_visual &&
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200735 using_hardware_cursor) {
736 if (output->prepare_scanout_surface(output, es) == 0) {
737 /* We're drawing nothing now,
738 * draw the damaged regions later. */
739 pixman_region32_union(&ec->damage_region,
740 &ec->damage_region,
741 &total_damage);
742 return;
743 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200744 }
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200745
746 if (es->width < output->width ||
747 es->height < output->height)
748 glClear(GL_COLOR_BUFFER_BIT);
749 wlsc_surface_draw(es, output, &total_damage);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500750 } else {
Kristian Høgsbergcfc6d272011-04-11 13:34:24 -0400751 wl_list_for_each(es, &ec->surface_list, link) {
752 if (es->visual != &ec->compositor.rgb_visual)
753 continue;
754
755 pixman_region32_init_rect(&repaint,
756 es->x, es->y,
757 es->width, es->height);
758 wlsc_surface_draw(es, output, &total_damage);
759 pixman_region32_subtract(&total_damage,
760 &total_damage, &repaint);
761 }
762
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500763 if (output->background)
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500764 wlsc_surface_draw(output->background,
765 output, &total_damage);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500766 else
767 glClear(GL_COLOR_BUFFER_BIT);
768
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500769 wl_list_for_each_reverse(es, &ec->surface_list, link) {
Kristian Høgsberg547cadf2011-04-12 22:23:30 -0400770 if (ec->overlay == es)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500771 continue;
772
Kristian Høgsbergcfc6d272011-04-11 13:34:24 -0400773 if (es->visual == &ec->compositor.rgb_visual) {
774 pixman_region32_union_rect(&total_damage,
775 &total_damage,
776 es->x, es->y,
777 es->width, es->height);
778 continue;
779 }
780
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500781 wlsc_surface_draw(es, output, &total_damage);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500782 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500783 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400784
Kristian Høgsberg547cadf2011-04-12 22:23:30 -0400785 if (ec->overlay)
786 wlsc_surface_draw(ec->overlay, output, &total_damage);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500787
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400788 if (ec->focus)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200789 wl_list_for_each(eid, &ec->input_device_list, link) {
790 if (&eid->input_device != ec->input_device ||
791 !using_hardware_cursor)
792 wlsc_surface_draw(eid->sprite, output,
793 &total_damage);
794 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400795
796 if (ec->fade.tweener.current > 0.001)
797 fade_output(output, ec->fade.tweener.current, &total_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500798}
799
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400800static int
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500801repaint(void *data)
802{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500803 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500804 struct wlsc_output *output;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100805 int repainted_all_outputs = 1;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500806
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100807 wl_list_for_each(output, &ec->output_list, link) {
808 if (!output->repaint_needed)
809 continue;
810
811 if (!output->finished) {
812 repainted_all_outputs = 0;
813 continue;
814 }
815
816 wlsc_output_repaint(output);
817 output->finished = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100818 output->repaint_needed = 0;
Kristian Høgsberg11e28282011-04-11 16:47:50 -0400819 output->present(output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500820 }
821
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100822 if (repainted_all_outputs)
823 ec->repaint_on_timeout = 0;
824 else
825 wl_event_source_timer_update(ec->timer_source, 1);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400826
827 return 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400828}
829
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400830void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400831wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400832{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100833 struct wlsc_output *output;
834
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400835 if (compositor->state == WLSC_COMPOSITOR_SLEEPING)
836 return;
837
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100838 wl_list_for_each(output, &compositor->output_list, link)
839 output->repaint_needed = 1;
840
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400841 if (compositor->repaint_on_timeout)
842 return;
843
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400844 wl_event_source_timer_update(compositor->timer_source, 1);
845 compositor->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400846}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500847
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400848void
849wlsc_compositor_fade(struct wlsc_compositor *compositor, float tint)
850{
851 int done;
852
853 done = wlsc_tweener_done(&compositor->fade.tweener);
854 compositor->fade.tweener.target = tint;
855 if (wlsc_tweener_done(&compositor->fade.tweener))
856 return;
857
858 if (done)
859 compositor->fade.tweener.timestamp = get_time();
860
861 wlsc_compositor_damage_all(compositor);
862 if (wl_list_empty(&compositor->fade.animation.link))
863 wl_list_insert(compositor->animation_list.prev,
864 &compositor->fade.animation.link);
865}
866
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500867static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500868surface_destroy(struct wl_client *client,
869 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400870{
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500871 wl_resource_destroy(&surface->resource, client);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400872}
873
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100874void
875wlsc_surface_assign_output(struct wlsc_surface *es)
876{
877 struct wlsc_compositor *ec = es->compositor;
878 struct wlsc_output *output;
879
880 struct wlsc_output *tmp = es->output;
881 es->output = NULL;
882
883 wl_list_for_each(output, &ec->output_list, link) {
884 if (output->x < es->x && es->x < output->x + output->width &&
885 output->y < es->y && es->y < output->y + output->height) {
886 if (output != tmp)
887 printf("assiging surface %p to output %p\n",
888 es, output);
889 es->output = output;
890 }
891 }
892
893 if (es->output == NULL) {
894 printf("no output found\n");
895 es->output = container_of(ec->output_list.next,
896 struct wlsc_output, link);
897 }
898}
899
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500900static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500901surface_attach(struct wl_client *client,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500902 struct wl_surface *surface, struct wl_buffer *buffer,
903 int32_t x, int32_t y)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400904{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500905 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400906
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500907 /* FIXME: This damages the entire old surface, but we should
908 * really just damage the part that's no longer covered by the
909 * surface. Anything covered by the new surface will be
910 * damaged by the client. */
911 wlsc_surface_damage(es);
912
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100913 wlsc_buffer_attach(buffer, surface);
914
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400915 es->buffer = buffer;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500916 es->x += x;
917 es->y += y;
918 es->width = buffer->width;
919 es->height = buffer->height;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100920 if (x != 0 || y != 0)
921 wlsc_surface_assign_output(es);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500922 wlsc_surface_update_matrix(es);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400923}
924
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500925static void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500926surface_map_toplevel(struct wl_client *client,
927 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400928{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500929 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100930 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400931
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500932 switch (es->map_type) {
933 case WLSC_SURFACE_MAP_UNMAPPED:
934 es->x = 10 + random() % 400;
935 es->y = 10 + random() % 400;
936 wlsc_surface_update_matrix(es);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100937 /* assign to first output */
938 es->output = container_of(ec->output_list.next,
939 struct wlsc_output, link);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500940 wl_list_insert(&es->compositor->surface_list, &es->link);
941 break;
942 case WLSC_SURFACE_MAP_TOPLEVEL:
Kristian Høgsberg8f66a572011-01-07 08:38:56 -0500943 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500944 case WLSC_SURFACE_MAP_FULLSCREEN:
945 es->fullscreen_output = NULL;
946 es->x = es->saved_x;
947 es->y = es->saved_y;
948 wlsc_surface_update_matrix(es);
949 break;
950 default:
951 break;
952 }
Kristian Høgsberg8f66a572011-01-07 08:38:56 -0500953
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500954 wlsc_surface_damage(es);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500955 es->map_type = WLSC_SURFACE_MAP_TOPLEVEL;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400956}
957
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500958static void
Kristian Høgsberg8dc378f2011-01-21 18:02:24 -0500959surface_map_transient(struct wl_client *client,
960 struct wl_surface *surface, struct wl_surface *parent,
961 int x, int y, uint32_t flags)
962{
963 struct wlsc_surface *es = (struct wlsc_surface *) surface;
964 struct wlsc_surface *pes = (struct wlsc_surface *) parent;
965
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500966 switch (es->map_type) {
967 case WLSC_SURFACE_MAP_UNMAPPED:
968 wl_list_insert(&es->compositor->surface_list, &es->link);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100969 /* assign to parents output */
970 es->output = pes->output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500971 break;
972 case WLSC_SURFACE_MAP_FULLSCREEN:
973 es->fullscreen_output = NULL;
974 break;
975 default:
976 break;
977 }
Kristian Høgsberg8dc378f2011-01-21 18:02:24 -0500978
979 es->x = pes->x + x;
980 es->y = pes->y + y;
981
982 wlsc_surface_update_matrix(es);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500983 wlsc_surface_damage(es);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500984 es->map_type = WLSC_SURFACE_MAP_TRANSIENT;
985}
986
987static void
988surface_map_fullscreen(struct wl_client *client, struct wl_surface *surface)
989{
990 struct wlsc_surface *es = (struct wlsc_surface *) surface;
991 struct wlsc_output *output;
992
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100993 /* FIXME: Fullscreen on first output */
994 /* FIXME: Handle output going away */
995 output = container_of(es->compositor->output_list.next,
996 struct wlsc_output, link);
997
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500998 switch (es->map_type) {
999 case WLSC_SURFACE_MAP_UNMAPPED:
1000 es->x = 10 + random() % 400;
1001 es->y = 10 + random() % 400;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001002 /* assign to first output */
1003 es->output = output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001004 wl_list_insert(&es->compositor->surface_list, &es->link);
1005 break;
1006 case WLSC_SURFACE_MAP_FULLSCREEN:
1007 return;
1008 default:
1009 break;
1010 }
1011
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001012 es->saved_x = es->x;
1013 es->saved_y = es->y;
1014 es->x = (output->width - es->width) / 2;
1015 es->y = (output->height - es->height) / 2;
1016 es->fullscreen_output = output;
1017 wlsc_surface_update_matrix(es);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001018 wlsc_surface_damage(es);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001019 es->map_type = WLSC_SURFACE_MAP_FULLSCREEN;
Kristian Høgsberg8dc378f2011-01-21 18:02:24 -05001020}
1021
1022static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001023surface_damage(struct wl_client *client,
1024 struct wl_surface *surface,
1025 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001026{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001027 struct wlsc_surface *es = (struct wlsc_surface *) surface;
1028
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001029 wlsc_surface_damage_rectangle(es, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001030}
1031
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001032const static struct wl_surface_interface surface_interface = {
1033 surface_destroy,
1034 surface_attach,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001035 surface_map_toplevel,
Kristian Høgsberg8dc378f2011-01-21 18:02:24 -05001036 surface_map_transient,
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001037 surface_map_fullscreen,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001038 surface_damage
1039};
1040
1041static void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001042wlsc_input_device_attach(struct wlsc_input_device *device,
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001043 int x, int y, int width, int height)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001044{
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001045 wlsc_surface_damage(device->sprite);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001046
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001047 device->hotspot_x = x;
1048 device->hotspot_y = y;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001049
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001050 device->sprite->x = device->input_device.x - device->hotspot_x;
1051 device->sprite->y = device->input_device.y - device->hotspot_y;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001052 device->sprite->width = width;
1053 device->sprite->height = height;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001054 wlsc_surface_update_matrix(device->sprite);
1055
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001056 wlsc_surface_damage(device->sprite);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001057}
1058
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001059static void
1060wlsc_input_device_attach_buffer(struct wlsc_input_device *device,
1061 struct wl_buffer *buffer, int x, int y)
1062{
1063 wlsc_buffer_attach(buffer, &device->sprite->surface);
1064 wlsc_input_device_attach(device, x, y, buffer->width, buffer->height);
1065}
1066
1067static void
1068wlsc_input_device_attach_sprite(struct wlsc_input_device *device,
1069 struct wlsc_sprite *sprite, int x, int y)
1070{
1071 wlsc_sprite_attach(sprite, &device->sprite->surface);
1072 wlsc_input_device_attach(device, x, y, sprite->width, sprite->height);
1073}
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001074
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001075void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001076wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
1077 enum wlsc_pointer_type type)
1078{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001079 struct wlsc_compositor *compositor =
1080 (struct wlsc_compositor *) device->input_device.compositor;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001081
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001082 wlsc_input_device_attach_sprite(device,
1083 compositor->pointer_sprites[type],
1084 pointer_images[type].hotspot_x,
1085 pointer_images[type].hotspot_y);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001086}
1087
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001088static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001089compositor_create_surface(struct wl_client *client,
1090 struct wl_compositor *compositor, uint32_t id)
1091{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001092 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001093 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001094
Kristian Høgsbergc5d6be92011-01-14 16:22:37 -05001095 surface = wlsc_surface_create(ec, 0, 0, 0, 0);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001096 if (surface == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -04001097 wl_client_post_no_memory(client);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001098 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001099 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001100
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001101 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001102
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001103 surface->surface.resource.object.id = id;
1104 surface->surface.resource.object.interface = &wl_surface_interface;
1105 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001106 (void (**)(void)) &surface_interface;
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001107 surface->surface.client = client;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001108
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001109 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001110}
1111
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001112const static struct wl_compositor_interface compositor_interface = {
1113 compositor_create_surface,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001114};
1115
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001116static void
1117wlsc_surface_transform(struct wlsc_surface *surface,
1118 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1119{
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001120 struct wlsc_vector v = { { x, y, 0, 1 } };
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001121
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001122 wlsc_matrix_transform(&surface->matrix_inv, &v);
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -04001123 *sx = v.f[0] * surface->width;
1124 *sy = v.f[1] * surface->height;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001125}
1126
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001127struct wlsc_surface *
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001128pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001129{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001130 struct wlsc_compositor *ec =
1131 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001132 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001133
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001134 wl_list_for_each(es, &ec->surface_list, link) {
1135 wlsc_surface_transform(es, device->x, device->y, sx, sy);
1136 if (0 <= *sx && *sx < es->width &&
1137 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001138 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001139 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001140
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001141 return NULL;
1142}
1143
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001144
1145static void
1146motion_grab_motion(struct wl_grab *grab,
1147 uint32_t time, int32_t x, int32_t y)
1148{
1149 struct wlsc_input_device *device =
1150 (struct wlsc_input_device *) grab->input_device;
1151 struct wlsc_surface *es =
1152 (struct wlsc_surface *) device->input_device.pointer_focus;
1153 int32_t sx, sy;
1154
1155 wlsc_surface_transform(es, x, y, &sx, &sy);
1156 wl_client_post_event(es->surface.client,
1157 &device->input_device.object,
1158 WL_INPUT_DEVICE_MOTION,
1159 time, x, y, sx, sy);
1160}
1161
1162static void
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001163motion_grab_button(struct wl_grab *grab,
1164 uint32_t time, int32_t button, int32_t state)
1165{
1166 wl_client_post_event(grab->input_device->pointer_focus->client,
1167 &grab->input_device->object,
1168 WL_INPUT_DEVICE_BUTTON,
1169 time, button, state);
1170}
1171
1172static void
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001173motion_grab_end(struct wl_grab *grab, uint32_t time)
1174{
1175}
1176
1177static const struct wl_grab_interface motion_grab_interface = {
1178 motion_grab_motion,
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001179 motion_grab_button,
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001180 motion_grab_end
1181};
1182
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001183void
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001184wlsc_compositor_wake(struct wlsc_compositor *compositor)
1185{
1186 if (compositor->idle_inhibit)
1187 return;
1188
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001189 wlsc_compositor_fade(compositor, 0.0);
1190 compositor->state = WLSC_COMPOSITOR_ACTIVE;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001191
1192 wl_event_source_timer_update(compositor->idle_source,
1193 option_idle_time * 1000);
1194}
1195
1196static void
1197wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor)
1198{
1199 wlsc_compositor_wake(compositor);
1200 compositor->idle_inhibit++;
1201}
1202
1203static void
1204wlsc_compositor_idle_release(struct wlsc_compositor *compositor)
1205{
1206 compositor->idle_inhibit--;
1207 wlsc_compositor_wake(compositor);
1208}
1209
1210static int
1211idle_handler(void *data)
1212{
1213 struct wlsc_compositor *compositor = data;
1214
1215 if (compositor->idle_inhibit)
1216 return 1;
1217
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001218 wlsc_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001219
1220 return 1;
1221}
1222
1223void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001224notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001225{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001226 struct wlsc_surface *es;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001227 struct wlsc_compositor *ec =
1228 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001229 struct wlsc_output *output;
Kristian Høgsberg6d65d5f2010-12-07 13:30:18 -05001230 const struct wl_grab_interface *interface;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001231 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsbergfc9c28a2010-12-07 13:04:43 -05001232 int32_t sx, sy;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001233 int x_valid = 0, y_valid = 0;
1234 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 -05001235
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001236 wlsc_compositor_wake(ec);
1237
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001238 wl_list_for_each(output, &ec->output_list, link) {
1239 if (output->x <= x && x <= output->x + output->width)
1240 x_valid = 1;
1241
1242 if (output->y <= y && y <= output->y + output->height)
1243 y_valid = 1;
1244
1245 /* FIXME: calculate this only on output addition/deletion */
1246 if (output->x < min_x)
1247 min_x = output->x;
1248 if (output->y < min_y)
1249 min_y = output->y;
1250
1251 if (output->x + output->width > max_x)
1252 max_x = output->x + output->width;
1253 if (output->y + output->height > max_y)
1254 max_y = output->y + output->height;
1255 }
1256
1257 if (!x_valid) {
1258 if (x < min_x)
1259 x = min_x;
1260 else if (x >= max_x)
1261 x = max_x;
1262 }
1263 if (!y_valid) {
1264 if (y < min_y)
1265 y = min_y;
1266 else if (y >= max_y)
1267 y = max_y;
1268 }
Ray Strode90e701d2008-12-18 23:05:43 -05001269
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -05001270 device->x = x;
1271 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001272
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001273 if (device->grab) {
1274 interface = device->grab->interface;
1275 interface->motion(device->grab, time, x, y);
1276 } else {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001277 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001278 wl_input_device_set_pointer_focus(device,
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001279 &es->surface,
Kristian Høgsberg26437072010-12-01 10:17:47 -05001280 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001281 if (es)
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001282 wl_client_post_event(es->surface.client,
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001283 &device->object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001284 WL_INPUT_DEVICE_MOTION,
1285 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001286 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001287
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001288 wlsc_surface_damage(wd->sprite);
1289
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001290 wd->sprite->x = device->x - wd->hotspot_x;
1291 wd->sprite->y = device->y - wd->hotspot_y;
1292 wlsc_surface_update_matrix(wd->sprite);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001293
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001294 wlsc_surface_damage(wd->sprite);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001295}
1296
Kristian Høgsberg30021d72011-04-12 17:42:30 -04001297void
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001298wlsc_surface_activate(struct wlsc_surface *surface,
1299 struct wlsc_input_device *device, uint32_t time)
1300{
1301 wlsc_surface_raise(surface);
1302 if (device->selection)
1303 wlsc_selection_set_focus(device->selection,
1304 &surface->surface, time);
1305
1306 wl_input_device_set_keyboard_focus(&device->input_device,
1307 &surface->surface,
1308 time);
1309}
1310
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001311struct wlsc_binding {
1312 uint32_t key;
1313 uint32_t button;
1314 uint32_t modifier;
1315 wlsc_binding_handler_t handler;
1316 void *data;
1317 struct wl_list link;
1318};
1319
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001320void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001321notify_button(struct wl_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001322 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001323{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001324 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001325 struct wlsc_compositor *compositor =
1326 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001327 struct wlsc_binding *b;
1328 struct wlsc_surface *surface =
1329 (struct wlsc_surface *) device->pointer_focus;
Kristian Høgsbergea081152010-12-07 08:59:51 -05001330
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001331 if (state)
1332 wlsc_compositor_idle_inhibit(compositor);
1333 else
1334 wlsc_compositor_idle_release(compositor);
1335
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -05001336 if (state && surface && device->grab == NULL) {
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001337 wlsc_surface_activate(surface, wd, time);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001338 wl_input_device_start_grab(device,
1339 &device->motion_grab,
1340 button, time);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001341 }
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -05001342
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001343 wl_list_for_each(b, &compositor->binding_list, link) {
1344 if (b->button == button &&
1345 b->modifier == wd->modifier_state && state) {
1346 b->handler(&wd->input_device,
1347 time, 0, button, state, b->data);
1348 break;
1349 }
Benjamin Franzke5a2218a2011-02-01 16:30:31 +01001350 }
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001351
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -05001352 if (device->grab)
1353 device->grab->interface->button(device->grab, time,
1354 button, state);
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -05001355
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001356 if (!state && device->grab && device->grab_button == button)
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001357 wl_input_device_end_grab(device, time);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001358}
1359
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001360static void
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001361terminate_binding(struct wl_input_device *device, uint32_t time,
1362 uint32_t key, uint32_t button, uint32_t state, void *data)
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001363{
1364 struct wlsc_compositor *compositor = data;
1365
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001366 if (state)
1367 wl_display_terminate(compositor->wl_display);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001368}
1369
1370struct wlsc_binding *
1371wlsc_compositor_add_binding(struct wlsc_compositor *compositor,
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001372 uint32_t key, uint32_t button, uint32_t modifier,
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001373 wlsc_binding_handler_t handler, void *data)
1374{
1375 struct wlsc_binding *binding;
1376
1377 binding = malloc(sizeof *binding);
1378 if (binding == NULL)
1379 return NULL;
1380
1381 binding->key = key;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001382 binding->button = button;
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001383 binding->modifier = modifier;
1384 binding->handler = handler;
1385 binding->data = data;
1386 wl_list_insert(compositor->binding_list.prev, &binding->link);
1387
1388 return binding;
1389}
1390
1391void
1392wlsc_binding_destroy(struct wlsc_binding *binding)
1393{
1394 wl_list_remove(&binding->link);
1395 free(binding);
1396}
1397
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001398static void
1399update_modifier_state(struct wlsc_input_device *device,
1400 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001401{
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001402 uint32_t modifier;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001403
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001404 switch (key) {
1405 case KEY_LEFTCTRL:
1406 case KEY_RIGHTCTRL:
1407 modifier = MODIFIER_CTRL;
1408 break;
1409
1410 case KEY_LEFTALT:
1411 case KEY_RIGHTALT:
1412 modifier = MODIFIER_ALT;
1413 break;
1414
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -04001415 case KEY_LEFTMETA:
1416 case KEY_RIGHTMETA:
1417 modifier = MODIFIER_SUPER;
1418 break;
1419
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001420 default:
1421 modifier = 0;
1422 break;
1423 }
1424
1425 if (state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001426 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001427 else
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001428 device->modifier_state &= ~modifier;
1429}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001430
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001431void
1432notify_key(struct wl_input_device *device,
1433 uint32_t time, uint32_t key, uint32_t state)
1434{
1435 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1436 struct wlsc_compositor *compositor =
1437 (struct wlsc_compositor *) device->compositor;
1438 uint32_t *k, *end;
1439 struct wlsc_binding *b;
1440
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001441 if (state)
1442 wlsc_compositor_idle_inhibit(compositor);
1443 else
1444 wlsc_compositor_idle_release(compositor);
1445
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001446 wl_list_for_each(b, &compositor->binding_list, link) {
1447 if (b->key == key &&
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001448 b->modifier == wd->modifier_state) {
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001449 b->handler(&wd->input_device,
1450 time, key, 0, state, b->data);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001451 break;
1452 }
1453 }
1454
1455 update_modifier_state(wd, key, state);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001456 end = device->keys.data + device->keys.size;
1457 for (k = device->keys.data; k < end; k++) {
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001458 if (*k == key)
1459 *k = *--end;
1460 }
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001461 device->keys.size = (void *) end - device->keys.data;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001462 if (state) {
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001463 k = wl_array_add(&device->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001464 *k = key;
1465 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001466
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001467 if (device->keyboard_focus != NULL)
1468 wl_client_post_event(device->keyboard_focus->client,
1469 &device->object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001470 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001471}
1472
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001473void
1474notify_pointer_focus(struct wl_input_device *device,
1475 uint32_t time, struct wlsc_output *output,
1476 int32_t x, int32_t y)
1477{
1478 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1479 struct wlsc_compositor *compositor =
1480 (struct wlsc_compositor *) device->compositor;
1481 struct wlsc_surface *es;
1482 int32_t sx, sy;
1483
1484 if (output) {
1485 device->x = x;
1486 device->y = y;
1487 es = pick_surface(device, &sx, &sy);
1488 wl_input_device_set_pointer_focus(device,
1489 &es->surface,
1490 time, x, y, sx, sy);
1491
1492 compositor->focus = 1;
1493
1494 wd->sprite->x = device->x - wd->hotspot_x;
1495 wd->sprite->y = device->y - wd->hotspot_y;
1496 wlsc_surface_update_matrix(wd->sprite);
1497 } else {
1498 wl_input_device_set_pointer_focus(device, NULL,
1499 time, 0, 0, 0, 0);
1500 compositor->focus = 0;
1501 }
1502
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001503 wlsc_surface_damage(wd->sprite);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001504}
1505
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001506void
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001507notify_keyboard_focus(struct wl_input_device *device,
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001508 uint32_t time, struct wlsc_output *output,
1509 struct wl_array *keys)
1510{
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001511 struct wlsc_input_device *wd =
1512 (struct wlsc_input_device *) device;
1513 struct wlsc_compositor *compositor =
1514 (struct wlsc_compositor *) device->compositor;
1515 struct wlsc_surface *es;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001516 uint32_t *k, *end;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001517
1518 if (!wl_list_empty(&compositor->surface_list))
1519 es = container_of(compositor->surface_list.next,
1520 struct wlsc_surface, link);
1521 else
1522 es = NULL;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001523
1524 if (output) {
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001525 wl_array_copy(&wd->input_device.keys, keys);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001526 wd->modifier_state = 0;
1527 end = device->keys.data + device->keys.size;
1528 for (k = device->keys.data; k < end; k++) {
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001529 wlsc_compositor_idle_inhibit(compositor);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001530 update_modifier_state(wd, *k, 1);
1531 }
1532
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001533 wl_input_device_set_keyboard_focus(&wd->input_device,
1534 &es->surface, time);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001535 } else {
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001536 end = device->keys.data + device->keys.size;
1537 for (k = device->keys.data; k < end; k++)
1538 wlsc_compositor_idle_release(compositor);
1539
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001540 wd->modifier_state = 0;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001541 wl_input_device_set_keyboard_focus(&wd->input_device,
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001542 NULL, time);
1543 }
1544}
1545
1546
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001547static void
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001548input_device_attach(struct wl_client *client,
1549 struct wl_input_device *device_base,
1550 uint32_t time,
1551 struct wl_buffer *buffer, int32_t x, int32_t y)
1552{
1553 struct wlsc_input_device *device =
1554 (struct wlsc_input_device *) device_base;
1555
1556 if (time < device->input_device.pointer_focus_time)
1557 return;
1558 if (device->input_device.pointer_focus == NULL)
1559 return;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001560 if (device->input_device.pointer_focus->client != client)
1561 return;
1562
1563 if (buffer == NULL) {
1564 wlsc_input_device_set_pointer_image(device,
1565 WLSC_POINTER_LEFT_PTR);
1566 return;
1567 }
1568
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001569 wlsc_input_device_attach_buffer(device, buffer, x, y);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001570}
1571
1572const static struct wl_input_device_interface input_device_interface = {
1573 input_device_attach,
1574};
1575
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001576void
1577wlsc_input_device_init(struct wlsc_input_device *device,
1578 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001579{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001580 wl_input_device_init(&device->input_device, &ec->compositor);
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05001581
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001582 device->input_device.object.interface = &wl_input_device_interface;
1583 device->input_device.object.implementation =
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001584 (void (**)(void)) &input_device_interface;
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001585 wl_display_add_object(ec->wl_display, &device->input_device.object);
1586 wl_display_add_global(ec->wl_display, &device->input_device.object, NULL);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001587
Kristian Høgsbergc5d6be92011-01-14 16:22:37 -05001588 device->sprite = wlsc_surface_create(ec,
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001589 device->input_device.x,
1590 device->input_device.y, 32, 32);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001591 device->hotspot_x = 16;
1592 device->hotspot_y = 16;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001593 device->modifier_state = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001594
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001595 device->input_device.motion_grab.interface = &motion_grab_interface;
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001596
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001597 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001598
1599 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001600}
1601
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001602static void
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001603wlsc_output_post_geometry(struct wl_client *client,
1604 struct wl_object *global, uint32_t version)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001605{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001606 struct wlsc_output *output =
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001607 container_of(global, struct wlsc_output, object);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001608
1609 wl_client_post_event(client, global,
1610 WL_OUTPUT_GEOMETRY,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001611 output->x, output->y,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001612 output->width, output->height);
1613}
1614
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001615static const char vertex_shader[] =
1616 "uniform mat4 proj;\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001617 "attribute vec2 position;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001618 "attribute vec2 texcoord;\n"
1619 "varying vec2 v_texcoord;\n"
1620 "void main()\n"
1621 "{\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001622 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001623 " v_texcoord = texcoord;\n"
1624 "}\n";
1625
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001626static const char texture_fragment_shader[] =
Kristian Høgsbergdfce71d2010-12-07 20:19:10 -05001627 "precision mediump float;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001628 "varying vec2 v_texcoord;\n"
1629 "uniform sampler2D tex;\n"
1630 "void main()\n"
1631 "{\n"
1632 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1633 "}\n";
1634
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001635static const char solid_fragment_shader[] =
1636 "precision mediump float;\n"
1637 "varying vec2 v_texcoord;\n"
1638 "uniform vec4 color;\n"
1639 "void main()\n"
1640 "{\n"
1641 " gl_FragColor = color\n;"
1642 "}\n";
1643
Kristian Høgsberga9468212010-06-14 21:03:11 -04001644static int
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001645compile_shader(GLenum type, const char *source)
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001646{
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001647 GLuint s;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001648 char msg[512];
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001649 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001650
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001651 s = glCreateShader(type);
1652 glShaderSource(s, 1, &source, NULL);
1653 glCompileShader(s);
1654 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001655 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001656 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1657 fprintf(stderr, "shader info: %s\n", msg);
1658 return GL_NONE;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001659 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001660
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001661 return s;
1662}
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001663
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001664static int
1665wlsc_shader_init(struct wlsc_shader *shader,
1666 const char *vertex_source, const char *fragment_source)
1667{
1668 char msg[512];
1669 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001670
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001671 shader->vertex_shader =
1672 compile_shader(GL_VERTEX_SHADER, vertex_source);
1673 shader->fragment_shader =
1674 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1675
1676 shader->program = glCreateProgram();
1677 glAttachShader(shader->program, shader->vertex_shader);
1678 glAttachShader(shader->program, shader->fragment_shader);
1679 glBindAttribLocation(shader->program, 0, "position");
1680 glBindAttribLocation(shader->program, 1, "texcoord");
1681
1682 glLinkProgram(shader->program);
1683 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001684 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001685 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001686 fprintf(stderr, "link info: %s\n", msg);
1687 return -1;
1688 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001689
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001690 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1691 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001692
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001693 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001694}
1695
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001696static int
1697init_solid_shader(struct wlsc_shader *shader,
1698 GLuint vertex_shader, const char *fragment_source)
1699{
1700 GLint status;
1701 char msg[512];
1702
1703 shader->vertex_shader = vertex_shader;
1704 shader->fragment_shader =
1705 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1706
1707 shader->program = glCreateProgram();
1708 glAttachShader(shader->program, shader->vertex_shader);
1709 glAttachShader(shader->program, shader->fragment_shader);
1710 glBindAttribLocation(shader->program, 0, "position");
1711 glBindAttribLocation(shader->program, 1, "texcoord");
1712
1713 glLinkProgram(shader->program);
1714 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1715 if (!status) {
1716 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1717 fprintf(stderr, "link info: %s\n", msg);
1718 return -1;
1719 }
1720
1721 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1722 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1723
1724 return 0;
1725}
1726
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001727void
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001728wlsc_output_destroy(struct wlsc_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001729{
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001730 destroy_surface(&output->background->surface.resource, NULL);
1731}
1732
1733void
1734wlsc_output_move(struct wlsc_output *output, int x, int y)
1735{
1736 struct wlsc_compositor *c = output->compositor;
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01001737 int flip;
1738
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001739 output->x = x;
1740 output->y = y;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001741
Benjamin Franzke264b3f92011-03-16 13:48:42 +01001742 if (output->background) {
1743 output->background->x = x;
1744 output->background->y = y;
1745 wlsc_surface_update_matrix(output->background);
1746 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001747
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001748 pixman_region32_init(&output->previous_damage_region);
1749
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001750 wlsc_matrix_init(&output->matrix);
1751 wlsc_matrix_translate(&output->matrix,
1752 -output->x - output->width / 2.0,
1753 -output->y - output->height / 2.0, 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01001754
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001755 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001756 wlsc_matrix_scale(&output->matrix,
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01001757 2.0 / output->width,
1758 flip * 2.0 / output->height, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001759
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001760 pixman_region32_union_rect(&c->damage_region,
1761 &c->damage_region,
1762 x, y, output->width, output->height);
1763}
1764
1765void
1766wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1767 int x, int y, int width, int height, uint32_t flags)
1768{
1769 output->compositor = c;
1770 output->x = x;
1771 output->y = y;
1772 output->width = width;
1773 output->height = height;
1774
1775 output->background =
1776 background_create(output, option_background);
1777
1778 output->flags = flags;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001779 output->finished = 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001780 wlsc_output_move(output, x, y);
1781
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001782 output->object.interface = &wl_output_interface;
1783 wl_display_add_object(c->wl_display, &output->object);
1784 wl_display_add_global(c->wl_display, &output->object,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001785 wlsc_output_post_geometry);
1786}
1787
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001788static void
1789shm_buffer_created(struct wl_buffer *buffer)
1790{
1791 struct wl_list *surfaces_attached_to;
1792
1793 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1794 if (!surfaces_attached_to) {
1795 buffer->user_data = NULL;
1796 return;
1797 }
1798
1799 wl_list_init(surfaces_attached_to);
1800
1801 buffer->user_data = surfaces_attached_to;
1802}
1803
1804static void
1805shm_buffer_damaged(struct wl_buffer *buffer,
1806 int32_t x, int32_t y, int32_t width, int32_t height)
1807{
1808 struct wl_list *surfaces_attached_to = buffer->user_data;
1809 struct wlsc_surface *es;
1810
1811 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1812 glBindTexture(GL_TEXTURE_2D, es->texture);
1813 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1814 buffer->width, buffer->height, 0,
1815 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1816 wl_shm_buffer_get_data(buffer));
1817 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
1818 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
1819 }
1820}
1821
1822static void
1823shm_buffer_destroyed(struct wl_buffer *buffer)
1824{
1825 struct wl_list *surfaces_attached_to = buffer->user_data;
1826 struct wlsc_surface *es, *next;
1827
1828 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
1829 es->buffer = NULL;
1830 wl_list_remove(&es->buffer_link);
1831 }
1832
1833 free(surfaces_attached_to);
1834}
1835
1836const static struct wl_shm_callbacks shm_callbacks = {
1837 shm_buffer_created,
1838 shm_buffer_damaged,
1839 shm_buffer_destroyed
1840};
1841
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001842int
1843wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1844{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001845 struct wl_event_loop *loop;
Kristian Høgsbergd711d0c2011-01-14 17:28:21 -05001846 const char *extensions;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001847
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001848 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001849
Kristian Høgsbergc5c510e2010-12-08 15:12:58 -05001850 wl_compositor_init(&ec->compositor, &compositor_interface, display);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001851
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001852 ec->shm = wl_shm_init(display, &shm_callbacks);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001853 eglBindWaylandDisplayWL(ec->display, ec->wl_display);
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04001854
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001855 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001856 wl_list_init(&ec->input_device_list);
1857 wl_list_init(&ec->output_list);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001858 wl_list_init(&ec->binding_list);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001859 wl_list_init(&ec->animation_list);
1860 wlsc_tweener_init(&ec->fade.tweener, 0.8, 0.0, 0.0);
1861 ec->fade.animation.frame = fade_frame;
1862 wl_list_init(&ec->fade.animation.link);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001863
Kristian Høgsberg07937562011-04-12 17:25:42 -04001864 wlsc_shell_init(ec);
Kristian Høgsberg30021d72011-04-12 17:42:30 -04001865 wlsc_switcher_init(ec);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001866
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001867 wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0,
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001868 MODIFIER_CTRL | MODIFIER_ALT,
1869 terminate_binding, ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001870
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001871 create_pointer_images(ec);
1872
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001873 screenshooter_create(ec);
1874
Kristian Høgsbergd711d0c2011-01-14 17:28:21 -05001875 extensions = (const char *) glGetString(GL_EXTENSIONS);
1876 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1877 fprintf(stderr,
1878 "GL_EXT_texture_format_BGRA8888 not available\n");
1879 return -1;
1880 }
1881
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001882 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001883
1884 if (wlsc_shader_init(&ec->texture_shader,
1885 vertex_shader, texture_fragment_shader) < 0)
Kristian Høgsberga9468212010-06-14 21:03:11 -04001886 return -1;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001887 if (init_solid_shader(&ec->solid_shader,
1888 ec->texture_shader.vertex_shader,
1889 solid_fragment_shader) < 0)
1890 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001891
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001892 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001893 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
1894 wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000);
1895
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001896 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001897 pixman_region32_init(&ec->damage_region);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001898 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001899
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001900 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001901}
1902
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001903static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001904{
1905 struct wlsc_compositor *ec = data;
1906
1907 wl_display_terminate(ec->wl_display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001908
1909 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001910}
1911
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001912int main(int argc, char *argv[])
1913{
1914 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001915 struct wlsc_compositor *ec;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001916 struct wl_event_loop *loop;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001917 GError *error = NULL;
1918 GOptionContext *context;
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001919 int width, height;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001920
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001921 g_type_init(); /* GdkPixbuf needs this, it seems. */
1922
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001923 context = g_option_context_new(NULL);
1924 g_option_context_add_main_entries(context, option_entries, "Wayland");
1925 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1926 fprintf(stderr, "option parsing failed: %s\n", error->message);
1927 exit(EXIT_FAILURE);
1928 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001929 if (sscanf(option_geometry, "%dx%d", &width, &height) != 2) {
1930 fprintf(stderr, "invalid geometry option: %s \n",
1931 option_geometry);
1932 exit(EXIT_FAILURE);
1933 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001934
1935 display = wl_display_create();
1936
Kristian Høgsberga9410222011-01-14 17:22:35 -05001937 ec = NULL;
1938
1939#if BUILD_WAYLAND_COMPOSITOR
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001940 if (getenv("WAYLAND_DISPLAY"))
1941 ec = wayland_compositor_create(display, width, height);
Kristian Høgsberga9410222011-01-14 17:22:35 -05001942#endif
1943
1944#if BUILD_X11_COMPOSITOR
1945 if (ec == NULL && getenv("DISPLAY"))
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001946 ec = x11_compositor_create(display, width, height);
Kristian Høgsberga9410222011-01-14 17:22:35 -05001947#endif
1948
Benjamin Franzke5d007092011-04-04 00:30:25 +02001949#if BUILD_OPENWFD_COMPOSITOR
1950 if (ec == NULL && getenv("OPENWFD"))
1951 ec = wfd_compositor_create(display, option_connector);
1952#endif
1953
Kristian Høgsberga9410222011-01-14 17:22:35 -05001954#if BUILD_DRM_COMPOSITOR
1955 if (ec == NULL)
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001956 ec = drm_compositor_create(display, option_connector);
Kristian Høgsberga9410222011-01-14 17:22:35 -05001957#endif
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001958
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001959 if (ec == NULL) {
1960 fprintf(stderr, "failed to create compositor\n");
1961 exit(EXIT_FAILURE);
1962 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001963
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001964 if (wl_display_add_socket(display, option_socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001965 fprintf(stderr, "failed to add socket: %m\n");
1966 exit(EXIT_FAILURE);
1967 }
1968
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001969 loop = wl_display_get_event_loop(ec->wl_display);
1970 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
1971 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec);
1972
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001973 wl_display_run(display);
1974
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001975 eglUnbindWaylandDisplayWL(ec->display, display);
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001976 wl_display_destroy(display);
1977
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001978 ec->destroy(ec);
1979
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001980 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001981}