blob: d8fc906dd73598d174cc84c6eb4ea75cf07ed263 [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øgsberg890bc052008-12-30 14:31:33 -050036
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050037#include "wayland-server.h"
Kristian Høgsberg82863022010-06-04 21:52:02 -040038#include "compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050039
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010040/* The plan here is to generate a random anonymous socket name and
41 * advertise that through a service on the session dbus.
42 */
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -050043static const char *option_socket_name = NULL;
Kristian Høgsberg61a82512010-10-26 11:26:44 -040044static const char *option_background = "background.jpg";
45static const char *option_geometry = "1024x640";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040046static const char *option_shell = NULL;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -040047static int option_idle_time = 5;
Kristian Høgsberg61a82512010-10-26 11:26:44 -040048static int option_connector = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050049
50static const GOptionEntry option_entries[] = {
51 { "background", 'b', 0, G_OPTION_ARG_STRING,
52 &option_background, "Background image" },
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -040053 { "connector", 'c', 0, G_OPTION_ARG_INT,
54 &option_connector, "KMS connector" },
Kristian Høgsberg61a82512010-10-26 11:26:44 -040055 { "geometry", 'g', 0, G_OPTION_ARG_STRING,
56 &option_geometry, "Geometry" },
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010057 { "socket", 's', 0, G_OPTION_ARG_STRING,
58 &option_socket_name, "Socket Name" },
Kristian Høgsberge10a5d92011-04-22 14:01:18 -040059 { "idle-time", 'i', 0, G_OPTION_ARG_INT,
60 &option_idle_time, "Screensaver idle time" },
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040061 { "shell", 'i', 0, G_OPTION_ARG_STRING,
62 &option_shell, "Shell module" },
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050063 { NULL }
64};
65
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050066static void
67wlsc_matrix_init(struct wlsc_matrix *matrix)
68{
69 static const struct wlsc_matrix identity = {
70 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
71 };
72
73 memcpy(matrix, &identity, sizeof identity);
74}
75
76static void
77wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
78{
79 struct wlsc_matrix tmp;
Kristian Høgsberg27803c62010-06-06 22:23:21 -040080 const GLfloat *row, *column;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050081 div_t d;
82 int i, j;
83
84 for (i = 0; i < 16; i++) {
85 tmp.d[i] = 0;
86 d = div(i, 4);
87 row = m->d + d.quot * 4;
88 column = n->d + d.rem;
89 for (j = 0; j < 4; j++)
90 tmp.d[i] += row[j] * column[j * 4];
91 }
92 memcpy(m, &tmp, sizeof tmp);
93}
94
95static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040096wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050097{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -050098 struct wlsc_matrix translate = {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050099 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
100 };
101
102 wlsc_matrix_multiply(matrix, &translate);
103}
104
105static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400106wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500107{
108 struct wlsc_matrix scale = {
109 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
110 };
111
112 wlsc_matrix_multiply(matrix, &scale);
113}
114
115static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400116wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
117{
118 int i, j;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400119 struct wlsc_vector t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400120
121 for (i = 0; i < 4; i++) {
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400122 t.f[i] = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400123 for (j = 0; j < 4; j++)
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400124 t.f[i] += v->f[j] * matrix->d[i + j * 4];
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400125 }
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500126
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400127 *v = t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400128}
129
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400130WL_EXPORT void
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400131wlsc_tweener_init(struct wlsc_tweener *tweener,
132 double k, double current, double target)
133{
134 tweener->k = k;
135 tweener->current = current;
136 tweener->previous = current;
137 tweener->target = target;
138}
139
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400140WL_EXPORT void
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400141wlsc_tweener_update(struct wlsc_tweener *tweener, uint32_t msec)
142{
143 double force, current, step;
144
145 step = (msec - tweener->timestamp) / 100.0;
146 tweener->timestamp = msec;
147
148 current = tweener->current;
149 force = tweener->k * (tweener->target - current) / 10.0 +
150 (tweener->previous - current);
151
152 tweener->current =
153 current + (current - tweener->previous) + force * step * step;
154 tweener->previous = current;
155
156 if (tweener->current >= 1.0) {
157 tweener->current = 1.0;
158 tweener->previous = 1.0;
159 }
160
161 if (tweener->current <= 0.0) {
162 tweener->current = 0.0;
163 tweener->previous = 0.0;
164 }
165}
166
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400167WL_EXPORT int
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400168wlsc_tweener_done(struct wlsc_tweener *tweener)
169{
170 return fabs(tweener->previous - tweener->target) < 0.0002 &&
171 fabs(tweener->current - tweener->target) < 0.0002;
172}
173
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400174WL_EXPORT struct wlsc_surface *
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400175wlsc_surface_create(struct wlsc_compositor *compositor,
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400176 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500177{
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400178 struct wlsc_surface *surface;
179
180 surface = malloc(sizeof *surface);
181 if (surface == NULL)
182 return NULL;
183
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500184 wl_list_init(&surface->surface.destroy_listener_list);
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500185 wl_list_init(&surface->link);
Benjamin Franzkebab41fb2011-03-07 18:04:59 +0100186 wl_list_init(&surface->buffer_link);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500187 surface->map_type = WLSC_SURFACE_MAP_UNMAPPED;
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500188
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500189 glGenTextures(1, &surface->texture);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400190 glBindTexture(GL_TEXTURE_2D, surface->texture);
191 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
192 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
193 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
194 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
195
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500196 surface->compositor = compositor;
Kristian Høgsbergc5d6be92011-01-14 16:22:37 -0500197 surface->visual = NULL;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200198 surface->image = EGL_NO_IMAGE_KHR;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200199 surface->saved_texture = 0;
Benjamin Franzkebab41fb2011-03-07 18:04:59 +0100200 surface->buffer = NULL;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400201 surface->x = x;
202 surface->y = y;
203 surface->width = width;
204 surface->height = height;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400205 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400206 wlsc_matrix_scale(&surface->matrix, width, height, 1);
207 wlsc_matrix_translate(&surface->matrix, x, y, 0);
208
209 wlsc_matrix_init(&surface->matrix_inv);
210 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
211 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500212
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400213 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500214}
215
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400216WL_EXPORT void
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500217wlsc_surface_damage_rectangle(struct wlsc_surface *surface,
218 int32_t x, int32_t y,
219 int32_t width, int32_t height)
220{
221 struct wlsc_compositor *compositor = surface->compositor;
222
223 pixman_region32_union_rect(&compositor->damage_region,
224 &compositor->damage_region,
225 surface->x + x, surface->y + y,
226 width, height);
227 wlsc_compositor_schedule_repaint(compositor);
228}
229
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400230WL_EXPORT void
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500231wlsc_surface_damage(struct wlsc_surface *surface)
232{
233 wlsc_surface_damage_rectangle(surface, 0, 0,
234 surface->width, surface->height);
235}
236
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400237WL_EXPORT uint32_t
238wlsc_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500239{
240 struct timeval tv;
241
242 gettimeofday(&tv, NULL);
243
244 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
245}
246
Kristian Høgsberg54879822008-11-23 17:07:32 -0500247static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400248destroy_surface(struct wl_resource *resource, struct wl_client *client)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500249{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400250 struct wlsc_surface *surface =
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500251 container_of(resource, struct wlsc_surface, surface.resource);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500252 struct wl_listener *l, *next;
Kristian Høgsberg4685fa32010-12-06 21:38:50 -0500253 uint32_t time;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400254
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500255 wlsc_surface_damage(surface);
256
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400257 wl_list_remove(&surface->link);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200258 if (surface->saved_texture == 0)
259 glDeleteTextures(1, &surface->texture);
260 else
261 glDeleteTextures(1, &surface->saved_texture);
262
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400263
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200264 if (surface->image != EGL_NO_IMAGE_KHR)
265 eglDestroyImageKHR(surface->compositor->display,
266 surface->image);
267
Benjamin Franzkebab41fb2011-03-07 18:04:59 +0100268 wl_list_remove(&surface->buffer_link);
269
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400270 time = wlsc_compositor_get_time();
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500271 wl_list_for_each_safe(l, next,
272 &surface->surface.destroy_listener_list, link)
Kristian Høgsberg4685fa32010-12-06 21:38:50 -0500273 l->func(l, &surface->surface, time);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400274
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400275 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500276}
277
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500278uint32_t *
279wlsc_load_image(const char *filename, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500280{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400281 GdkPixbuf *pixbuf;
282 GError *error = NULL;
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500283 int stride, i, n_channels;
284 unsigned char *pixels, *end, *argb_pixels, *s, *d;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500285
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400286 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
287 width, height,
288 FALSE, &error);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500289 if (error != NULL) {
290 fprintf(stderr, "failed to load image: %s\n", error->message);
291 g_error_free(error);
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500292 return NULL;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500293 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400294
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500295 stride = gdk_pixbuf_get_rowstride(pixbuf);
296 pixels = gdk_pixbuf_get_pixels(pixbuf);
297 n_channels = gdk_pixbuf_get_n_channels(pixbuf);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400298
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500299 argb_pixels = malloc (height * width * 4);
300 if (argb_pixels == NULL) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500301 g_object_unref(pixbuf);
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500302 return NULL;
303 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400304
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500305 if (n_channels == 4) {
306 for (i = 0; i < height; i++) {
307 s = pixels + i * stride;
308 end = s + width * 4;
309 d = argb_pixels + i * width * 4;
310 while (s < end) {
311 unsigned int t;
312
313#define MULT(_d,c,a,t) \
314 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
315
316 MULT(d[0], s[2], s[3], t);
317 MULT(d[1], s[1], s[3], t);
318 MULT(d[2], s[0], s[3], t);
319 d[3] = s[3];
320 s += 4;
321 d += 4;
322 }
323 }
324 } else if (n_channels == 3) {
325 for (i = 0; i < height; i++) {
326 s = pixels + i * stride;
327 end = s + width * 3;
328 d = argb_pixels + i * width * 4;
329 while (s < end) {
330 d[0] = s[2];
331 d[1] = s[1];
332 d[2] = s[0];
333 d[3] = 0xff;
334 s += 3;
335 d += 4;
336 }
337 }
338 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400339
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500340 g_object_unref(pixbuf);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400341
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500342 return (uint32_t *) argb_pixels;
343}
344
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100345static void
346wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
347{
348 struct wlsc_surface *es = (struct wlsc_surface *) surface;
349 struct wlsc_compositor *ec = es->compositor;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100350 struct wl_list *surfaces_attached_to;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100351
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100352 if (es->saved_texture != 0)
353 es->texture = es->saved_texture;
354
355 glBindTexture(GL_TEXTURE_2D, es->texture);
356
357 if (wl_buffer_is_shm(buffer)) {
358 /* Unbind any EGLImage texture that may be bound, so we don't
359 * overwrite it.*/
360 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
361 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200362 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100363 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200364 es->pitch, buffer->height, 0,
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100365 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
366 wl_shm_buffer_get_data(buffer));
367 es->visual = buffer->visual;
368
369 surfaces_attached_to = buffer->user_data;
370
371 if (es->buffer)
372 wl_list_remove(&es->buffer_link);
373 wl_list_insert(surfaces_attached_to, &es->buffer_link);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100374 } else {
Kristian Høgsbergdf2f1972011-04-21 23:48:13 -0400375 es->image = eglCreateImageKHR(ec->display, NULL,
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200376 EGL_WAYLAND_BUFFER_WL,
377 buffer, NULL);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100378
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200379 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, es->image);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100380 es->visual = buffer->visual;
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200381 es->pitch = es->width;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100382 }
383}
384
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200385static void
386wlsc_sprite_attach(struct wlsc_sprite *sprite, struct wl_surface *surface)
387{
388 struct wlsc_surface *es = (struct wlsc_surface *) surface;
389
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200390 es->pitch = es->width;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200391 es->image = sprite->image;
392 if (sprite->image != EGL_NO_IMAGE_KHR) {
393 glBindTexture(GL_TEXTURE_2D, es->texture);
394 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, es->image);
395 } else {
396 if (es->saved_texture == 0)
397 es->saved_texture = es->texture;
398 es->texture = sprite->texture;
399 }
400
401 es->visual = sprite->visual;
402}
403
404enum sprite_usage {
405 SPRITE_USE_CURSOR = (1 << 0),
406};
407
408static struct wlsc_sprite *
409create_sprite_from_png(struct wlsc_compositor *ec,
410 const char *filename, int width, int height,
411 uint32_t usage)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500412{
413 uint32_t *pixels;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200414 struct wlsc_sprite *sprite;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500415
416 pixels = wlsc_load_image(filename, width, height);
Tim Wiederhakeac5c5e72011-01-27 01:32:36 +0100417 if(pixels == NULL)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200418 return NULL;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500419
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200420 sprite = malloc(sizeof *sprite);
421 if (sprite == NULL) {
422 free(pixels);
423 return NULL;
424 }
425
426 sprite->visual = &ec->compositor.premultiplied_argb_visual;
427 sprite->width = width;
428 sprite->height = height;
429 sprite->image = EGL_NO_IMAGE_KHR;
430
431 if (usage & SPRITE_USE_CURSOR && ec->create_cursor_image != NULL)
432 sprite->image = ec->create_cursor_image(ec, width, height);
433
434 glGenTextures(1, &sprite->texture);
435 glBindTexture(GL_TEXTURE_2D, sprite->texture);
436 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
437 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
438 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
439 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
440
441 if (sprite->image != EGL_NO_IMAGE_KHR) {
442 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, sprite->image);
443 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
444 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
445 } else {
446 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, width, height, 0,
447 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
448 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500449
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500450 free(pixels);
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500451
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200452 return sprite;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500453}
454
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400455static const struct {
456 const char *filename;
457 int hotspot_x, hotspot_y;
458} pointer_images[] = {
Kristian Høgsberg4219a402010-08-16 16:43:03 -0400459 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
460 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
461 { DATADIR "/wayland/bottom_side.png", 16, 20 },
462 { DATADIR "/wayland/grabbing.png", 20, 17 },
463 { DATADIR "/wayland/left_ptr.png", 10, 5 },
464 { DATADIR "/wayland/left_side.png", 10, 20 },
465 { DATADIR "/wayland/right_side.png", 30, 19 },
466 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
467 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
468 { DATADIR "/wayland/top_side.png", 18, 8 },
469 { DATADIR "/wayland/xterm.png", 15, 15 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400470};
471
472static void
473create_pointer_images(struct wlsc_compositor *ec)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500474{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400475 int i, count;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400476 const int width = 32, height = 32;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400477
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400478 count = ARRAY_LENGTH(pointer_images);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200479 ec->pointer_sprites = malloc(count * sizeof *ec->pointer_sprites);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400480 for (i = 0; i < count; i++) {
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200481 ec->pointer_sprites[i] =
482 create_sprite_from_png(ec,
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500483 pointer_images[i].filename,
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200484 width, height,
485 SPRITE_USE_CURSOR);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400486 }
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400487}
488
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500489static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500490background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500491{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500492 struct wlsc_surface *background;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200493 struct wlsc_sprite *sprite;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500494
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400495 background = wlsc_surface_create(output->compositor,
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400496 output->x, output->y,
497 output->width, output->height);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500498 if (background == NULL)
499 return NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500500
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200501 sprite = create_sprite_from_png(output->compositor, filename,
502 output->width, output->height, 0);
503 if (sprite == NULL) {
Benjamin Franzked3b023e2011-01-15 12:34:49 +0100504 free(background);
505 return NULL;
506 }
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100507
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200508 wlsc_sprite_attach(sprite, &background->surface);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500509
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500510 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500511}
512
513static void
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500514wlsc_surface_draw(struct wlsc_surface *es,
515 struct wlsc_output *output, pixman_region32_t *clip)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500516{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500517 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500518 GLfloat *v, inv_width, inv_height;
519 unsigned int *p;
520 pixman_region32_t repaint;
521 pixman_box32_t *rectangles;
522 int i, n;
523
524 pixman_region32_init_rect(&repaint,
525 es->x, es->y, es->width, es->height);
526 pixman_region32_intersect(&repaint, &repaint, clip);
527 if (!pixman_region32_not_empty(&repaint))
528 return;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500529
Kristian Høgsbergc5c510e2010-12-08 15:12:58 -0500530 if (es->visual == &ec->compositor.argb_visual) {
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500531 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
532 glEnable(GL_BLEND);
Kristian Høgsbergc5c510e2010-12-08 15:12:58 -0500533 } else if (es->visual == &ec->compositor.premultiplied_argb_visual) {
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500534 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
535 glEnable(GL_BLEND);
536 } else {
537 glDisable(GL_BLEND);
538 }
539
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500540 rectangles = pixman_region32_rectangles(&repaint, &n);
541 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
542 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200543 inv_width = 1.0 / es->pitch;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500544 inv_height = 1.0 / es->height;
545 for (i = 0; i < n; i++, v += 16, p += 6) {
546 v[ 0] = rectangles[i].x1;
547 v[ 1] = rectangles[i].y1;
548 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
549 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500550
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500551 v[ 4] = rectangles[i].x1;
552 v[ 5] = rectangles[i].y2;
553 v[ 6] = v[ 2];
554 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500555
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500556 v[ 8] = rectangles[i].x2;
557 v[ 9] = rectangles[i].y1;
558 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
559 v[11] = v[ 3];
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500560
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500561 v[12] = rectangles[i].x2;
562 v[13] = rectangles[i].y2;
563 v[14] = v[10];
564 v[15] = v[ 7];
565
566 p[0] = i * 4 + 0;
567 p[1] = i * 4 + 1;
568 p[2] = i * 4 + 2;
569 p[3] = i * 4 + 2;
570 p[4] = i * 4 + 1;
571 p[5] = i * 4 + 3;
572 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500573
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500574 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500575 v = ec->vertices.data;
576 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
577 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400578 glEnableVertexAttribArray(0);
579 glEnableVertexAttribArray(1);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500580 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
581
582 ec->vertices.size = 0;
583 ec->indices.size = 0;
584 pixman_region32_fini(&repaint);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500585}
586
587static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400588wlsc_surface_raise(struct wlsc_surface *surface)
589{
590 struct wlsc_compositor *compositor = surface->compositor;
591
592 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400593 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400594}
595
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400596WL_EXPORT void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400597wlsc_surface_update_matrix(struct wlsc_surface *es)
598{
599 wlsc_matrix_init(&es->matrix);
600 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
601 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
602
603 wlsc_matrix_init(&es->matrix_inv);
604 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
605 wlsc_matrix_scale(&es->matrix_inv,
606 1.0 / es->width, 1.0 / es->height, 1);
607}
608
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400609WL_EXPORT void
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400610wlsc_compositor_damage_all(struct wlsc_compositor *compositor)
611{
612 struct wlsc_output *output;
613
614 wl_list_for_each(output, &compositor->output_list, link)
615 wlsc_output_damage(output);
616}
617
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400618WL_EXPORT void
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100619wlsc_output_finish_frame(struct wlsc_output *output, int msecs)
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400620{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100621 struct wlsc_compositor *compositor = output->compositor;
622 struct wlsc_surface *es;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400623 struct wlsc_animation *animation, *next;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100624
625 wl_list_for_each(es, &compositor->surface_list, link) {
626 if (es->output == output) {
627 wl_display_post_frame(compositor->wl_display,
628 &es->surface, msecs);
629 }
630 }
631
632 output->finished = 1;
633
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400634 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsbergf30c67e2011-02-06 12:58:44 -0500635 compositor->repaint_on_timeout = 1;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400636
637 wl_list_for_each_safe(animation, next,
638 &compositor->animation_list, link)
639 animation->frame(animation, output, msecs);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400640}
641
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400642WL_EXPORT void
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400643wlsc_output_damage(struct wlsc_output *output)
644{
645 struct wlsc_compositor *compositor = output->compositor;
646
647 pixman_region32_union_rect(&compositor->damage_region,
648 &compositor->damage_region,
649 output->x, output->y,
650 output->width, output->height);
651 wlsc_compositor_schedule_repaint(compositor);
652}
653
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400654static void
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400655fade_frame(struct wlsc_animation *animation,
656 struct wlsc_output *output, uint32_t msecs)
657{
658 struct wlsc_compositor *compositor =
659 container_of(animation,
660 struct wlsc_compositor, fade.animation);
661
662 wlsc_tweener_update(&compositor->fade.tweener, msecs);
663 if (wlsc_tweener_done(&compositor->fade.tweener)) {
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400664 if (compositor->fade.tweener.current > 0.999) {
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400665 compositor->state = WLSC_COMPOSITOR_SLEEPING;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400666 compositor->shell->lock(compositor->shell);
667 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400668 compositor->fade.tweener.current =
669 compositor->fade.tweener.target;
670 wl_list_remove(&animation->link);
671 wl_list_init(&animation->link);
672 }
673
674 wlsc_output_damage(output);
675}
676
677static void
678fade_output(struct wlsc_output *output,
679 GLfloat tint, pixman_region32_t *region)
680{
681 struct wlsc_compositor *compositor = output->compositor;
682 struct wlsc_surface surface;
683 GLfloat color[4] = { 0.0, 0.0, 0.0, tint };
684
685 surface.compositor = compositor;
686 surface.x = output->x;
687 surface.y = output->y;
688 surface.width = output->width;
689 surface.height = output->height;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400690 surface.texture = GL_NONE;
Kristian Høgsbergc352ab02011-04-23 15:34:50 -0400691
692 if (tint <= 1.0)
693 surface.visual =
694 &compositor->compositor.premultiplied_argb_visual;
695 else
696 surface.visual = &compositor->compositor.rgb_visual;
697
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400698 glUseProgram(compositor->solid_shader.program);
699 glUniformMatrix4fv(compositor->solid_shader.proj_uniform,
700 1, GL_FALSE, output->matrix.d);
701 glUniform4fv(compositor->solid_shader.color_uniform, 1, color);
702 wlsc_surface_draw(&surface, output, region);
703}
704
705static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400706wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400707{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500708 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500709 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500710 struct wlsc_input_device *eid;
Kristian Høgsbergcfc6d272011-04-11 13:34:24 -0400711 pixman_region32_t new_damage, total_damage, repaint;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200712 int using_hardware_cursor = 1;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500713
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100714 output->prepare_render(output);
715
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500716 glViewport(0, 0, output->width, output->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500717
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -0400718 glUseProgram(ec->texture_shader.program);
719 glUniformMatrix4fv(ec->texture_shader.proj_uniform,
720 1, GL_FALSE, output->matrix.d);
721 glUniform1i(ec->texture_shader.tex_uniform, 0);
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500722
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500723 pixman_region32_init(&new_damage);
724 pixman_region32_init(&total_damage);
725 pixman_region32_intersect_rect(&new_damage,
726 &ec->damage_region,
727 output->x, output->y,
728 output->width, output->height);
729 pixman_region32_subtract(&ec->damage_region,
730 &ec->damage_region, &new_damage);
731 pixman_region32_union(&total_damage, &new_damage,
732 &output->previous_damage_region);
733 pixman_region32_copy(&output->previous_damage_region, &new_damage);
734
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200735 if (ec->focus)
736 if (output->set_hardware_cursor(output, ec->input_device) < 0)
737 using_hardware_cursor = 0;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400738 if (ec->fade.tweener.current > 0.001)
739 using_hardware_cursor = 0;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200740
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500741 es = container_of(ec->surface_list.next, struct wlsc_surface, link);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400742 if (es->fullscreen_output == output) {
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200743 if (es->visual == &ec->compositor.rgb_visual &&
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200744 using_hardware_cursor) {
745 if (output->prepare_scanout_surface(output, es) == 0) {
746 /* We're drawing nothing now,
747 * draw the damaged regions later. */
748 pixman_region32_union(&ec->damage_region,
749 &ec->damage_region,
750 &total_damage);
751 return;
752 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200753 }
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200754
755 if (es->width < output->width ||
756 es->height < output->height)
757 glClear(GL_COLOR_BUFFER_BIT);
758 wlsc_surface_draw(es, output, &total_damage);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500759 } else {
Kristian Høgsbergcfc6d272011-04-11 13:34:24 -0400760 wl_list_for_each(es, &ec->surface_list, link) {
761 if (es->visual != &ec->compositor.rgb_visual)
762 continue;
763
764 pixman_region32_init_rect(&repaint,
765 es->x, es->y,
766 es->width, es->height);
767 wlsc_surface_draw(es, output, &total_damage);
768 pixman_region32_subtract(&total_damage,
769 &total_damage, &repaint);
770 }
771
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500772 if (output->background)
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500773 wlsc_surface_draw(output->background,
774 output, &total_damage);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500775 else
Kristian Høgsbergc352ab02011-04-23 15:34:50 -0400776 fade_output(output, 1.0, &total_damage);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500777
Kristian Høgsbergc352ab02011-04-23 15:34:50 -0400778 glUseProgram(ec->texture_shader.program);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500779 wl_list_for_each_reverse(es, &ec->surface_list, link) {
Kristian Høgsberg547cadf2011-04-12 22:23:30 -0400780 if (ec->overlay == es)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500781 continue;
782
Kristian Høgsbergcfc6d272011-04-11 13:34:24 -0400783 if (es->visual == &ec->compositor.rgb_visual) {
784 pixman_region32_union_rect(&total_damage,
785 &total_damage,
786 es->x, es->y,
787 es->width, es->height);
788 continue;
789 }
790
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500791 wlsc_surface_draw(es, output, &total_damage);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500792 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500793 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400794
Kristian Høgsberg547cadf2011-04-12 22:23:30 -0400795 if (ec->overlay)
796 wlsc_surface_draw(ec->overlay, output, &total_damage);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500797
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400798 if (ec->focus)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200799 wl_list_for_each(eid, &ec->input_device_list, link) {
800 if (&eid->input_device != ec->input_device ||
801 !using_hardware_cursor)
802 wlsc_surface_draw(eid->sprite, output,
803 &total_damage);
804 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400805
806 if (ec->fade.tweener.current > 0.001)
807 fade_output(output, ec->fade.tweener.current, &total_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500808}
809
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400810static int
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500811repaint(void *data)
812{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500813 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500814 struct wlsc_output *output;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100815 int repainted_all_outputs = 1;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500816
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100817 wl_list_for_each(output, &ec->output_list, link) {
818 if (!output->repaint_needed)
819 continue;
820
821 if (!output->finished) {
822 repainted_all_outputs = 0;
823 continue;
824 }
825
826 wlsc_output_repaint(output);
827 output->finished = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100828 output->repaint_needed = 0;
Kristian Høgsberg11e28282011-04-11 16:47:50 -0400829 output->present(output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500830 }
831
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100832 if (repainted_all_outputs)
833 ec->repaint_on_timeout = 0;
834 else
835 wl_event_source_timer_update(ec->timer_source, 1);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400836
837 return 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400838}
839
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400840WL_EXPORT void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400841wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400842{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100843 struct wlsc_output *output;
844
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400845 if (compositor->state == WLSC_COMPOSITOR_SLEEPING)
846 return;
847
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100848 wl_list_for_each(output, &compositor->output_list, link)
849 output->repaint_needed = 1;
850
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400851 if (compositor->repaint_on_timeout)
852 return;
853
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400854 wl_event_source_timer_update(compositor->timer_source, 1);
855 compositor->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400856}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500857
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400858WL_EXPORT void
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400859wlsc_compositor_fade(struct wlsc_compositor *compositor, float tint)
860{
861 int done;
862
863 done = wlsc_tweener_done(&compositor->fade.tweener);
864 compositor->fade.tweener.target = tint;
865 if (wlsc_tweener_done(&compositor->fade.tweener))
866 return;
867
868 if (done)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400869 compositor->fade.tweener.timestamp =
870 wlsc_compositor_get_time();
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400871
872 wlsc_compositor_damage_all(compositor);
873 if (wl_list_empty(&compositor->fade.animation.link))
874 wl_list_insert(compositor->animation_list.prev,
875 &compositor->fade.animation.link);
876}
877
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500878static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500879surface_destroy(struct wl_client *client,
880 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400881{
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500882 wl_resource_destroy(&surface->resource, client);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400883}
884
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400885WL_EXPORT void
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100886wlsc_surface_assign_output(struct wlsc_surface *es)
887{
888 struct wlsc_compositor *ec = es->compositor;
889 struct wlsc_output *output;
890
891 struct wlsc_output *tmp = es->output;
892 es->output = NULL;
893
894 wl_list_for_each(output, &ec->output_list, link) {
895 if (output->x < es->x && es->x < output->x + output->width &&
896 output->y < es->y && es->y < output->y + output->height) {
897 if (output != tmp)
898 printf("assiging surface %p to output %p\n",
899 es, output);
900 es->output = output;
901 }
902 }
903
904 if (es->output == NULL) {
905 printf("no output found\n");
906 es->output = container_of(ec->output_list.next,
907 struct wlsc_output, link);
908 }
909}
910
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500911static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500912surface_attach(struct wl_client *client,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500913 struct wl_surface *surface, struct wl_buffer *buffer,
914 int32_t x, int32_t y)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400915{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500916 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400917
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500918 /* FIXME: This damages the entire old surface, but we should
919 * really just damage the part that's no longer covered by the
920 * surface. Anything covered by the new surface will be
921 * damaged by the client. */
922 wlsc_surface_damage(es);
923
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100924 wlsc_buffer_attach(buffer, surface);
925
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400926 es->buffer = buffer;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500927 es->x += x;
928 es->y += y;
929 es->width = buffer->width;
930 es->height = buffer->height;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100931 if (x != 0 || y != 0)
932 wlsc_surface_assign_output(es);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500933 wlsc_surface_update_matrix(es);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400934
935 es->compositor->shell->attach(es->compositor->shell, es);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400936}
937
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500938static void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500939surface_map_toplevel(struct wl_client *client,
940 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400941{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500942 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100943 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400944
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500945 switch (es->map_type) {
946 case WLSC_SURFACE_MAP_UNMAPPED:
947 es->x = 10 + random() % 400;
948 es->y = 10 + random() % 400;
949 wlsc_surface_update_matrix(es);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100950 /* assign to first output */
951 es->output = container_of(ec->output_list.next,
952 struct wlsc_output, link);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500953 wl_list_insert(&es->compositor->surface_list, &es->link);
954 break;
955 case WLSC_SURFACE_MAP_TOPLEVEL:
Kristian Høgsberg8f66a572011-01-07 08:38:56 -0500956 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500957 case WLSC_SURFACE_MAP_FULLSCREEN:
958 es->fullscreen_output = NULL;
959 es->x = es->saved_x;
960 es->y = es->saved_y;
961 wlsc_surface_update_matrix(es);
962 break;
963 default:
964 break;
965 }
Kristian Høgsberg8f66a572011-01-07 08:38:56 -0500966
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500967 wlsc_surface_damage(es);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500968 es->map_type = WLSC_SURFACE_MAP_TOPLEVEL;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400969}
970
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500971static void
Kristian Høgsberg8dc378f2011-01-21 18:02:24 -0500972surface_map_transient(struct wl_client *client,
973 struct wl_surface *surface, struct wl_surface *parent,
974 int x, int y, uint32_t flags)
975{
976 struct wlsc_surface *es = (struct wlsc_surface *) surface;
977 struct wlsc_surface *pes = (struct wlsc_surface *) parent;
978
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500979 switch (es->map_type) {
980 case WLSC_SURFACE_MAP_UNMAPPED:
981 wl_list_insert(&es->compositor->surface_list, &es->link);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100982 /* assign to parents output */
983 es->output = pes->output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500984 break;
985 case WLSC_SURFACE_MAP_FULLSCREEN:
986 es->fullscreen_output = NULL;
987 break;
988 default:
989 break;
990 }
Kristian Høgsberg8dc378f2011-01-21 18:02:24 -0500991
992 es->x = pes->x + x;
993 es->y = pes->y + y;
994
995 wlsc_surface_update_matrix(es);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500996 wlsc_surface_damage(es);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500997 es->map_type = WLSC_SURFACE_MAP_TRANSIENT;
998}
999
1000static void
1001surface_map_fullscreen(struct wl_client *client, struct wl_surface *surface)
1002{
1003 struct wlsc_surface *es = (struct wlsc_surface *) surface;
1004 struct wlsc_output *output;
1005
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001006 /* FIXME: Fullscreen on first output */
1007 /* FIXME: Handle output going away */
1008 output = container_of(es->compositor->output_list.next,
1009 struct wlsc_output, link);
1010
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001011 switch (es->map_type) {
1012 case WLSC_SURFACE_MAP_UNMAPPED:
1013 es->x = 10 + random() % 400;
1014 es->y = 10 + random() % 400;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001015 /* assign to first output */
1016 es->output = output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001017 wl_list_insert(&es->compositor->surface_list, &es->link);
1018 break;
1019 case WLSC_SURFACE_MAP_FULLSCREEN:
1020 return;
1021 default:
1022 break;
1023 }
1024
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001025 es->saved_x = es->x;
1026 es->saved_y = es->y;
1027 es->x = (output->width - es->width) / 2;
1028 es->y = (output->height - es->height) / 2;
1029 es->fullscreen_output = output;
1030 wlsc_surface_update_matrix(es);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001031 wlsc_surface_damage(es);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001032 es->map_type = WLSC_SURFACE_MAP_FULLSCREEN;
Kristian Høgsberg8dc378f2011-01-21 18:02:24 -05001033}
1034
1035static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001036surface_damage(struct wl_client *client,
1037 struct wl_surface *surface,
1038 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001039{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001040 struct wlsc_surface *es = (struct wlsc_surface *) surface;
1041
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001042 wlsc_surface_damage_rectangle(es, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001043}
1044
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001045const static struct wl_surface_interface surface_interface = {
1046 surface_destroy,
1047 surface_attach,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001048 surface_map_toplevel,
Kristian Høgsberg8dc378f2011-01-21 18:02:24 -05001049 surface_map_transient,
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001050 surface_map_fullscreen,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001051 surface_damage
1052};
1053
1054static void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001055wlsc_input_device_attach(struct wlsc_input_device *device,
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001056 int x, int y, int width, int height)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001057{
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001058 wlsc_surface_damage(device->sprite);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001059
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001060 device->hotspot_x = x;
1061 device->hotspot_y = y;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001062
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001063 device->sprite->x = device->input_device.x - device->hotspot_x;
1064 device->sprite->y = device->input_device.y - device->hotspot_y;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001065 device->sprite->width = width;
1066 device->sprite->height = height;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001067 wlsc_surface_update_matrix(device->sprite);
1068
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001069 wlsc_surface_damage(device->sprite);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001070}
1071
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001072static void
1073wlsc_input_device_attach_buffer(struct wlsc_input_device *device,
1074 struct wl_buffer *buffer, int x, int y)
1075{
1076 wlsc_buffer_attach(buffer, &device->sprite->surface);
1077 wlsc_input_device_attach(device, x, y, buffer->width, buffer->height);
1078}
1079
1080static void
1081wlsc_input_device_attach_sprite(struct wlsc_input_device *device,
1082 struct wlsc_sprite *sprite, int x, int y)
1083{
1084 wlsc_sprite_attach(sprite, &device->sprite->surface);
1085 wlsc_input_device_attach(device, x, y, sprite->width, sprite->height);
1086}
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001087
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001088void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001089wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
1090 enum wlsc_pointer_type type)
1091{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001092 struct wlsc_compositor *compositor =
1093 (struct wlsc_compositor *) device->input_device.compositor;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001094
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001095 wlsc_input_device_attach_sprite(device,
1096 compositor->pointer_sprites[type],
1097 pointer_images[type].hotspot_x,
1098 pointer_images[type].hotspot_y);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -04001099}
1100
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001101static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001102compositor_create_surface(struct wl_client *client,
1103 struct wl_compositor *compositor, uint32_t id)
1104{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001105 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001106 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001107
Kristian Høgsbergc5d6be92011-01-14 16:22:37 -05001108 surface = wlsc_surface_create(ec, 0, 0, 0, 0);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001109 if (surface == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -04001110 wl_client_post_no_memory(client);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001111 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001112 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001113
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001114 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001115
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001116 surface->surface.resource.object.id = id;
1117 surface->surface.resource.object.interface = &wl_surface_interface;
1118 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001119 (void (**)(void)) &surface_interface;
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001120 surface->surface.client = client;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001121
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001122 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001123}
1124
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001125const static struct wl_compositor_interface compositor_interface = {
1126 compositor_create_surface,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001127};
1128
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001129static void
1130wlsc_surface_transform(struct wlsc_surface *surface,
1131 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1132{
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001133 struct wlsc_vector v = { { x, y, 0, 1 } };
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001134
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001135 wlsc_matrix_transform(&surface->matrix_inv, &v);
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -04001136 *sx = v.f[0] * surface->width;
1137 *sy = v.f[1] * surface->height;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001138}
1139
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001140struct wlsc_surface *
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001141pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001142{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001143 struct wlsc_compositor *ec =
1144 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001145 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001146
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001147 wl_list_for_each(es, &ec->surface_list, link) {
1148 wlsc_surface_transform(es, device->x, device->y, sx, sy);
1149 if (0 <= *sx && *sx < es->width &&
1150 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001151 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001152 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001153
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001154 return NULL;
1155}
1156
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001157
1158static void
1159motion_grab_motion(struct wl_grab *grab,
1160 uint32_t time, int32_t x, int32_t y)
1161{
1162 struct wlsc_input_device *device =
1163 (struct wlsc_input_device *) grab->input_device;
1164 struct wlsc_surface *es =
1165 (struct wlsc_surface *) device->input_device.pointer_focus;
1166 int32_t sx, sy;
1167
1168 wlsc_surface_transform(es, x, y, &sx, &sy);
1169 wl_client_post_event(es->surface.client,
1170 &device->input_device.object,
1171 WL_INPUT_DEVICE_MOTION,
1172 time, x, y, sx, sy);
1173}
1174
1175static void
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001176motion_grab_button(struct wl_grab *grab,
1177 uint32_t time, int32_t button, int32_t state)
1178{
1179 wl_client_post_event(grab->input_device->pointer_focus->client,
1180 &grab->input_device->object,
1181 WL_INPUT_DEVICE_BUTTON,
1182 time, button, state);
1183}
1184
1185static void
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001186motion_grab_end(struct wl_grab *grab, uint32_t time)
1187{
1188}
1189
1190static const struct wl_grab_interface motion_grab_interface = {
1191 motion_grab_motion,
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001192 motion_grab_button,
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001193 motion_grab_end
1194};
1195
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001196WL_EXPORT void
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001197wlsc_compositor_wake(struct wlsc_compositor *compositor)
1198{
1199 if (compositor->idle_inhibit)
1200 return;
1201
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001202 wlsc_compositor_fade(compositor, 0.0);
1203 compositor->state = WLSC_COMPOSITOR_ACTIVE;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001204
1205 wl_event_source_timer_update(compositor->idle_source,
1206 option_idle_time * 1000);
1207}
1208
1209static void
1210wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor)
1211{
1212 wlsc_compositor_wake(compositor);
1213 compositor->idle_inhibit++;
1214}
1215
1216static void
1217wlsc_compositor_idle_release(struct wlsc_compositor *compositor)
1218{
1219 compositor->idle_inhibit--;
1220 wlsc_compositor_wake(compositor);
1221}
1222
1223static int
1224idle_handler(void *data)
1225{
1226 struct wlsc_compositor *compositor = data;
1227
1228 if (compositor->idle_inhibit)
1229 return 1;
1230
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001231 wlsc_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001232
1233 return 1;
1234}
1235
1236void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001237notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001238{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001239 struct wlsc_surface *es;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001240 struct wlsc_compositor *ec =
1241 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001242 struct wlsc_output *output;
Kristian Høgsberg6d65d5f2010-12-07 13:30:18 -05001243 const struct wl_grab_interface *interface;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001244 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsbergfc9c28a2010-12-07 13:04:43 -05001245 int32_t sx, sy;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001246 int x_valid = 0, y_valid = 0;
1247 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 -05001248
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001249 wlsc_compositor_wake(ec);
1250
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001251 wl_list_for_each(output, &ec->output_list, link) {
1252 if (output->x <= x && x <= output->x + output->width)
1253 x_valid = 1;
1254
1255 if (output->y <= y && y <= output->y + output->height)
1256 y_valid = 1;
1257
1258 /* FIXME: calculate this only on output addition/deletion */
1259 if (output->x < min_x)
1260 min_x = output->x;
1261 if (output->y < min_y)
1262 min_y = output->y;
1263
1264 if (output->x + output->width > max_x)
1265 max_x = output->x + output->width;
1266 if (output->y + output->height > max_y)
1267 max_y = output->y + output->height;
1268 }
1269
1270 if (!x_valid) {
1271 if (x < min_x)
1272 x = min_x;
1273 else if (x >= max_x)
1274 x = max_x;
1275 }
1276 if (!y_valid) {
1277 if (y < min_y)
1278 y = min_y;
1279 else if (y >= max_y)
1280 y = max_y;
1281 }
Ray Strode90e701d2008-12-18 23:05:43 -05001282
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -05001283 device->x = x;
1284 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001285
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001286 if (device->grab) {
1287 interface = device->grab->interface;
1288 interface->motion(device->grab, time, x, y);
1289 } else {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001290 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001291 wl_input_device_set_pointer_focus(device,
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001292 &es->surface,
Kristian Høgsberg26437072010-12-01 10:17:47 -05001293 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001294 if (es)
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001295 wl_client_post_event(es->surface.client,
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001296 &device->object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001297 WL_INPUT_DEVICE_MOTION,
1298 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001299 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001300
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001301 wlsc_surface_damage(wd->sprite);
1302
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001303 wd->sprite->x = device->x - wd->hotspot_x;
1304 wd->sprite->y = device->y - wd->hotspot_y;
1305 wlsc_surface_update_matrix(wd->sprite);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001306
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001307 wlsc_surface_damage(wd->sprite);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001308}
1309
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001310WL_EXPORT void
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001311wlsc_surface_activate(struct wlsc_surface *surface,
1312 struct wlsc_input_device *device, uint32_t time)
1313{
1314 wlsc_surface_raise(surface);
1315 if (device->selection)
1316 wlsc_selection_set_focus(device->selection,
1317 &surface->surface, time);
1318
1319 wl_input_device_set_keyboard_focus(&device->input_device,
1320 &surface->surface,
1321 time);
1322}
1323
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001324struct wlsc_binding {
1325 uint32_t key;
1326 uint32_t button;
1327 uint32_t modifier;
1328 wlsc_binding_handler_t handler;
1329 void *data;
1330 struct wl_list link;
1331};
1332
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001333void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001334notify_button(struct wl_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001335 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001336{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001337 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001338 struct wlsc_compositor *compositor =
1339 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001340 struct wlsc_binding *b;
1341 struct wlsc_surface *surface =
1342 (struct wlsc_surface *) device->pointer_focus;
Kristian Høgsbergea081152010-12-07 08:59:51 -05001343
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001344 if (state)
1345 wlsc_compositor_idle_inhibit(compositor);
1346 else
1347 wlsc_compositor_idle_release(compositor);
1348
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -05001349 if (state && surface && device->grab == NULL) {
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001350 wlsc_surface_activate(surface, wd, time);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001351 wl_input_device_start_grab(device,
1352 &device->motion_grab,
1353 button, time);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001354 }
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -05001355
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001356 wl_list_for_each(b, &compositor->binding_list, link) {
1357 if (b->button == button &&
1358 b->modifier == wd->modifier_state && state) {
1359 b->handler(&wd->input_device,
1360 time, 0, button, state, b->data);
1361 break;
1362 }
Benjamin Franzke5a2218a2011-02-01 16:30:31 +01001363 }
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001364
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -05001365 if (device->grab)
1366 device->grab->interface->button(device->grab, time,
1367 button, state);
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -05001368
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001369 if (!state && device->grab && device->grab_button == button)
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001370 wl_input_device_end_grab(device, time);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001371}
1372
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001373static void
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001374terminate_binding(struct wl_input_device *device, uint32_t time,
1375 uint32_t key, uint32_t button, uint32_t state, void *data)
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001376{
1377 struct wlsc_compositor *compositor = data;
1378
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001379 if (state)
1380 wl_display_terminate(compositor->wl_display);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001381}
1382
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001383WL_EXPORT struct wlsc_binding *
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001384wlsc_compositor_add_binding(struct wlsc_compositor *compositor,
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001385 uint32_t key, uint32_t button, uint32_t modifier,
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001386 wlsc_binding_handler_t handler, void *data)
1387{
1388 struct wlsc_binding *binding;
1389
1390 binding = malloc(sizeof *binding);
1391 if (binding == NULL)
1392 return NULL;
1393
1394 binding->key = key;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001395 binding->button = button;
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001396 binding->modifier = modifier;
1397 binding->handler = handler;
1398 binding->data = data;
1399 wl_list_insert(compositor->binding_list.prev, &binding->link);
1400
1401 return binding;
1402}
1403
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001404WL_EXPORT void
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001405wlsc_binding_destroy(struct wlsc_binding *binding)
1406{
1407 wl_list_remove(&binding->link);
1408 free(binding);
1409}
1410
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001411static void
1412update_modifier_state(struct wlsc_input_device *device,
1413 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001414{
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001415 uint32_t modifier;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001416
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001417 switch (key) {
1418 case KEY_LEFTCTRL:
1419 case KEY_RIGHTCTRL:
1420 modifier = MODIFIER_CTRL;
1421 break;
1422
1423 case KEY_LEFTALT:
1424 case KEY_RIGHTALT:
1425 modifier = MODIFIER_ALT;
1426 break;
1427
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -04001428 case KEY_LEFTMETA:
1429 case KEY_RIGHTMETA:
1430 modifier = MODIFIER_SUPER;
1431 break;
1432
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001433 default:
1434 modifier = 0;
1435 break;
1436 }
1437
1438 if (state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001439 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001440 else
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001441 device->modifier_state &= ~modifier;
1442}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001443
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001444void
1445notify_key(struct wl_input_device *device,
1446 uint32_t time, uint32_t key, uint32_t state)
1447{
1448 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1449 struct wlsc_compositor *compositor =
1450 (struct wlsc_compositor *) device->compositor;
1451 uint32_t *k, *end;
1452 struct wlsc_binding *b;
1453
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001454 if (state)
1455 wlsc_compositor_idle_inhibit(compositor);
1456 else
1457 wlsc_compositor_idle_release(compositor);
1458
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001459 wl_list_for_each(b, &compositor->binding_list, link) {
1460 if (b->key == key &&
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001461 b->modifier == wd->modifier_state) {
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001462 b->handler(&wd->input_device,
1463 time, key, 0, state, b->data);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001464 break;
1465 }
1466 }
1467
1468 update_modifier_state(wd, key, state);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001469 end = device->keys.data + device->keys.size;
1470 for (k = device->keys.data; k < end; k++) {
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001471 if (*k == key)
1472 *k = *--end;
1473 }
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001474 device->keys.size = (void *) end - device->keys.data;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001475 if (state) {
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001476 k = wl_array_add(&device->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001477 *k = key;
1478 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001479
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001480 if (device->keyboard_focus != NULL)
1481 wl_client_post_event(device->keyboard_focus->client,
1482 &device->object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001483 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001484}
1485
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001486void
1487notify_pointer_focus(struct wl_input_device *device,
1488 uint32_t time, struct wlsc_output *output,
1489 int32_t x, int32_t y)
1490{
1491 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1492 struct wlsc_compositor *compositor =
1493 (struct wlsc_compositor *) device->compositor;
1494 struct wlsc_surface *es;
1495 int32_t sx, sy;
1496
1497 if (output) {
1498 device->x = x;
1499 device->y = y;
1500 es = pick_surface(device, &sx, &sy);
1501 wl_input_device_set_pointer_focus(device,
1502 &es->surface,
1503 time, x, y, sx, sy);
1504
1505 compositor->focus = 1;
1506
1507 wd->sprite->x = device->x - wd->hotspot_x;
1508 wd->sprite->y = device->y - wd->hotspot_y;
1509 wlsc_surface_update_matrix(wd->sprite);
1510 } else {
1511 wl_input_device_set_pointer_focus(device, NULL,
1512 time, 0, 0, 0, 0);
1513 compositor->focus = 0;
1514 }
1515
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001516 wlsc_surface_damage(wd->sprite);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001517}
1518
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001519void
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001520notify_keyboard_focus(struct wl_input_device *device,
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001521 uint32_t time, struct wlsc_output *output,
1522 struct wl_array *keys)
1523{
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001524 struct wlsc_input_device *wd =
1525 (struct wlsc_input_device *) device;
1526 struct wlsc_compositor *compositor =
1527 (struct wlsc_compositor *) device->compositor;
1528 struct wlsc_surface *es;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001529 uint32_t *k, *end;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001530
1531 if (!wl_list_empty(&compositor->surface_list))
1532 es = container_of(compositor->surface_list.next,
1533 struct wlsc_surface, link);
1534 else
1535 es = NULL;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001536
1537 if (output) {
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001538 wl_array_copy(&wd->input_device.keys, keys);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001539 wd->modifier_state = 0;
1540 end = device->keys.data + device->keys.size;
1541 for (k = device->keys.data; k < end; k++) {
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001542 wlsc_compositor_idle_inhibit(compositor);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001543 update_modifier_state(wd, *k, 1);
1544 }
1545
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001546 wl_input_device_set_keyboard_focus(&wd->input_device,
1547 &es->surface, time);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001548 } else {
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001549 end = device->keys.data + device->keys.size;
1550 for (k = device->keys.data; k < end; k++)
1551 wlsc_compositor_idle_release(compositor);
1552
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001553 wd->modifier_state = 0;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001554 wl_input_device_set_keyboard_focus(&wd->input_device,
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001555 NULL, time);
1556 }
1557}
1558
1559
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001560static void
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001561input_device_attach(struct wl_client *client,
1562 struct wl_input_device *device_base,
1563 uint32_t time,
1564 struct wl_buffer *buffer, int32_t x, int32_t y)
1565{
1566 struct wlsc_input_device *device =
1567 (struct wlsc_input_device *) device_base;
1568
1569 if (time < device->input_device.pointer_focus_time)
1570 return;
1571 if (device->input_device.pointer_focus == NULL)
1572 return;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001573 if (device->input_device.pointer_focus->client != client)
1574 return;
1575
1576 if (buffer == NULL) {
1577 wlsc_input_device_set_pointer_image(device,
1578 WLSC_POINTER_LEFT_PTR);
1579 return;
1580 }
1581
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001582 wlsc_input_device_attach_buffer(device, buffer, x, y);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001583}
1584
1585const static struct wl_input_device_interface input_device_interface = {
1586 input_device_attach,
1587};
1588
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001589void
1590wlsc_input_device_init(struct wlsc_input_device *device,
1591 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001592{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001593 wl_input_device_init(&device->input_device, &ec->compositor);
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05001594
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001595 device->input_device.object.interface = &wl_input_device_interface;
1596 device->input_device.object.implementation =
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001597 (void (**)(void)) &input_device_interface;
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001598 wl_display_add_object(ec->wl_display, &device->input_device.object);
1599 wl_display_add_global(ec->wl_display, &device->input_device.object, NULL);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001600
Kristian Høgsbergc5d6be92011-01-14 16:22:37 -05001601 device->sprite = wlsc_surface_create(ec,
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001602 device->input_device.x,
1603 device->input_device.y, 32, 32);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001604 device->hotspot_x = 16;
1605 device->hotspot_y = 16;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001606 device->modifier_state = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001607
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001608 device->input_device.motion_grab.interface = &motion_grab_interface;
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001609
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001610 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001611
1612 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001613}
1614
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001615static void
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001616wlsc_output_post_geometry(struct wl_client *client,
1617 struct wl_object *global, uint32_t version)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001618{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001619 struct wlsc_output *output =
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001620 container_of(global, struct wlsc_output, object);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001621
1622 wl_client_post_event(client, global,
1623 WL_OUTPUT_GEOMETRY,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001624 output->x, output->y,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001625 output->width, output->height);
1626}
1627
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001628static const char vertex_shader[] =
1629 "uniform mat4 proj;\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001630 "attribute vec2 position;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001631 "attribute vec2 texcoord;\n"
1632 "varying vec2 v_texcoord;\n"
1633 "void main()\n"
1634 "{\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001635 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001636 " v_texcoord = texcoord;\n"
1637 "}\n";
1638
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001639static const char texture_fragment_shader[] =
Kristian Høgsbergdfce71d2010-12-07 20:19:10 -05001640 "precision mediump float;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001641 "varying vec2 v_texcoord;\n"
1642 "uniform sampler2D tex;\n"
1643 "void main()\n"
1644 "{\n"
1645 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1646 "}\n";
1647
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001648static const char solid_fragment_shader[] =
1649 "precision mediump float;\n"
1650 "varying vec2 v_texcoord;\n"
1651 "uniform vec4 color;\n"
1652 "void main()\n"
1653 "{\n"
1654 " gl_FragColor = color\n;"
1655 "}\n";
1656
Kristian Høgsberga9468212010-06-14 21:03:11 -04001657static int
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001658compile_shader(GLenum type, const char *source)
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001659{
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001660 GLuint s;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001661 char msg[512];
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001662 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001663
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001664 s = glCreateShader(type);
1665 glShaderSource(s, 1, &source, NULL);
1666 glCompileShader(s);
1667 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001668 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001669 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1670 fprintf(stderr, "shader info: %s\n", msg);
1671 return GL_NONE;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001672 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001673
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001674 return s;
1675}
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001676
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001677static int
1678wlsc_shader_init(struct wlsc_shader *shader,
1679 const char *vertex_source, const char *fragment_source)
1680{
1681 char msg[512];
1682 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001683
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001684 shader->vertex_shader =
1685 compile_shader(GL_VERTEX_SHADER, vertex_source);
1686 shader->fragment_shader =
1687 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1688
1689 shader->program = glCreateProgram();
1690 glAttachShader(shader->program, shader->vertex_shader);
1691 glAttachShader(shader->program, shader->fragment_shader);
1692 glBindAttribLocation(shader->program, 0, "position");
1693 glBindAttribLocation(shader->program, 1, "texcoord");
1694
1695 glLinkProgram(shader->program);
1696 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001697 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001698 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001699 fprintf(stderr, "link info: %s\n", msg);
1700 return -1;
1701 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001702
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001703 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1704 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001705
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001706 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001707}
1708
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001709static int
1710init_solid_shader(struct wlsc_shader *shader,
1711 GLuint vertex_shader, const char *fragment_source)
1712{
1713 GLint status;
1714 char msg[512];
1715
1716 shader->vertex_shader = vertex_shader;
1717 shader->fragment_shader =
1718 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1719
1720 shader->program = glCreateProgram();
1721 glAttachShader(shader->program, shader->vertex_shader);
1722 glAttachShader(shader->program, shader->fragment_shader);
1723 glBindAttribLocation(shader->program, 0, "position");
1724 glBindAttribLocation(shader->program, 1, "texcoord");
1725
1726 glLinkProgram(shader->program);
1727 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1728 if (!status) {
1729 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1730 fprintf(stderr, "link info: %s\n", msg);
1731 return -1;
1732 }
1733
1734 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1735 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1736
1737 return 0;
1738}
1739
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001740void
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001741wlsc_output_destroy(struct wlsc_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001742{
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001743 destroy_surface(&output->background->surface.resource, NULL);
1744}
1745
1746void
1747wlsc_output_move(struct wlsc_output *output, int x, int y)
1748{
1749 struct wlsc_compositor *c = output->compositor;
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01001750 int flip;
1751
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001752 output->x = x;
1753 output->y = y;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001754
Benjamin Franzke264b3f92011-03-16 13:48:42 +01001755 if (output->background) {
1756 output->background->x = x;
1757 output->background->y = y;
1758 wlsc_surface_update_matrix(output->background);
1759 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001760
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001761 pixman_region32_init(&output->previous_damage_region);
1762
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001763 wlsc_matrix_init(&output->matrix);
1764 wlsc_matrix_translate(&output->matrix,
1765 -output->x - output->width / 2.0,
1766 -output->y - output->height / 2.0, 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01001767
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001768 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001769 wlsc_matrix_scale(&output->matrix,
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01001770 2.0 / output->width,
1771 flip * 2.0 / output->height, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001772
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001773 pixman_region32_union_rect(&c->damage_region,
1774 &c->damage_region,
1775 x, y, output->width, output->height);
1776}
1777
1778void
1779wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1780 int x, int y, int width, int height, uint32_t flags)
1781{
1782 output->compositor = c;
1783 output->x = x;
1784 output->y = y;
1785 output->width = width;
1786 output->height = height;
1787
1788 output->background =
1789 background_create(output, option_background);
1790
1791 output->flags = flags;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001792 output->finished = 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001793 wlsc_output_move(output, x, y);
1794
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001795 output->object.interface = &wl_output_interface;
1796 wl_display_add_object(c->wl_display, &output->object);
1797 wl_display_add_global(c->wl_display, &output->object,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001798 wlsc_output_post_geometry);
1799}
1800
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001801static void
1802shm_buffer_created(struct wl_buffer *buffer)
1803{
1804 struct wl_list *surfaces_attached_to;
1805
1806 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1807 if (!surfaces_attached_to) {
1808 buffer->user_data = NULL;
1809 return;
1810 }
1811
1812 wl_list_init(surfaces_attached_to);
1813
1814 buffer->user_data = surfaces_attached_to;
1815}
1816
1817static void
1818shm_buffer_damaged(struct wl_buffer *buffer,
1819 int32_t x, int32_t y, int32_t width, int32_t height)
1820{
1821 struct wl_list *surfaces_attached_to = buffer->user_data;
1822 struct wlsc_surface *es;
Benjamin Franzkefab5ec12011-04-25 19:44:47 +02001823 GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001824
1825 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1826 glBindTexture(GL_TEXTURE_2D, es->texture);
1827 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Benjamin Franzkefab5ec12011-04-25 19:44:47 +02001828 tex_width, buffer->height, 0,
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001829 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1830 wl_shm_buffer_get_data(buffer));
1831 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
1832 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
1833 }
1834}
1835
1836static void
1837shm_buffer_destroyed(struct wl_buffer *buffer)
1838{
1839 struct wl_list *surfaces_attached_to = buffer->user_data;
1840 struct wlsc_surface *es, *next;
1841
1842 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
1843 es->buffer = NULL;
1844 wl_list_remove(&es->buffer_link);
1845 }
1846
1847 free(surfaces_attached_to);
1848}
1849
1850const static struct wl_shm_callbacks shm_callbacks = {
1851 shm_buffer_created,
1852 shm_buffer_damaged,
1853 shm_buffer_destroyed
1854};
1855
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001856int
1857wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1858{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001859 struct wl_event_loop *loop;
Kristian Høgsbergd711d0c2011-01-14 17:28:21 -05001860 const char *extensions;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001861
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001862 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001863
Kristian Høgsbergc5c510e2010-12-08 15:12:58 -05001864 wl_compositor_init(&ec->compositor, &compositor_interface, display);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001865
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001866 ec->shm = wl_shm_init(display, &shm_callbacks);
Benjamin Franzkecdd9db72011-04-25 17:47:44 +02001867 if (strstr(eglQueryString(ec->display, EGL_EXTENSIONS),
1868 "EGL_WL_bind_wayland_display"))
1869 eglBindWaylandDisplayWL(ec->display, ec->wl_display);
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04001870
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001871 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001872 wl_list_init(&ec->input_device_list);
1873 wl_list_init(&ec->output_list);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001874 wl_list_init(&ec->binding_list);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001875 wl_list_init(&ec->animation_list);
1876 wlsc_tweener_init(&ec->fade.tweener, 0.8, 0.0, 0.0);
1877 ec->fade.animation.frame = fade_frame;
1878 wl_list_init(&ec->fade.animation.link);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001879
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001880 wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0,
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001881 MODIFIER_CTRL | MODIFIER_ALT,
1882 terminate_binding, ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001883
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001884 create_pointer_images(ec);
1885
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001886 screenshooter_create(ec);
1887
Kristian Høgsbergd711d0c2011-01-14 17:28:21 -05001888 extensions = (const char *) glGetString(GL_EXTENSIONS);
1889 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1890 fprintf(stderr,
1891 "GL_EXT_texture_format_BGRA8888 not available\n");
1892 return -1;
1893 }
1894
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001895 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04001896
1897 if (wlsc_shader_init(&ec->texture_shader,
1898 vertex_shader, texture_fragment_shader) < 0)
Kristian Høgsberga9468212010-06-14 21:03:11 -04001899 return -1;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001900 if (init_solid_shader(&ec->solid_shader,
1901 ec->texture_shader.vertex_shader,
1902 solid_fragment_shader) < 0)
1903 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001904
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001905 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001906 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
1907 wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000);
1908
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001909 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001910 pixman_region32_init(&ec->damage_region);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001911 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001912
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001913 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001914}
1915
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001916static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001917{
1918 struct wlsc_compositor *ec = data;
1919
1920 wl_display_terminate(ec->wl_display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001921
1922 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001923}
1924
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001925int main(int argc, char *argv[])
1926{
1927 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001928 struct wlsc_compositor *ec;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001929 struct wl_event_loop *loop;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001930 GError *error = NULL;
1931 GOptionContext *context;
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001932 int width, height;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001933 void *shell_module;
1934 int (*shell_init)(struct wlsc_compositor *ec);
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001935
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001936 g_type_init(); /* GdkPixbuf needs this, it seems. */
1937
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001938 context = g_option_context_new(NULL);
1939 g_option_context_add_main_entries(context, option_entries, "Wayland");
1940 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1941 fprintf(stderr, "option parsing failed: %s\n", error->message);
1942 exit(EXIT_FAILURE);
1943 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001944 if (sscanf(option_geometry, "%dx%d", &width, &height) != 2) {
1945 fprintf(stderr, "invalid geometry option: %s \n",
1946 option_geometry);
1947 exit(EXIT_FAILURE);
1948 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001949
1950 display = wl_display_create();
1951
Kristian Høgsberga9410222011-01-14 17:22:35 -05001952 ec = NULL;
1953
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001954 shell_init = desktop_shell_init;
1955 if (option_shell) {
1956 shell_module = dlopen(option_shell, RTLD_LAZY);
1957 if (!shell_module) {
1958 fprintf(stderr, "failed to load shell module: %m\n");
1959 exit(EXIT_FAILURE);
1960 }
1961 shell_init = dlsym(shell_module, "shell_init");
1962 if (!shell_init) {
1963 fprintf(stderr,
1964 "failed to lookup shell init function: %m\n");
1965 exit(EXIT_FAILURE);
1966 }
1967 }
1968
Kristian Høgsberga9410222011-01-14 17:22:35 -05001969#if BUILD_WAYLAND_COMPOSITOR
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001970 if (getenv("WAYLAND_DISPLAY"))
1971 ec = wayland_compositor_create(display, width, height);
Kristian Høgsberga9410222011-01-14 17:22:35 -05001972#endif
1973
1974#if BUILD_X11_COMPOSITOR
1975 if (ec == NULL && getenv("DISPLAY"))
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001976 ec = x11_compositor_create(display, width, height);
Kristian Høgsberga9410222011-01-14 17:22:35 -05001977#endif
1978
Benjamin Franzke5d007092011-04-04 00:30:25 +02001979#if BUILD_OPENWFD_COMPOSITOR
1980 if (ec == NULL && getenv("OPENWFD"))
1981 ec = wfd_compositor_create(display, option_connector);
1982#endif
1983
Kristian Høgsberga9410222011-01-14 17:22:35 -05001984#if BUILD_DRM_COMPOSITOR
1985 if (ec == NULL)
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001986 ec = drm_compositor_create(display, option_connector);
Kristian Høgsberga9410222011-01-14 17:22:35 -05001987#endif
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001988
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001989 if (ec == NULL) {
1990 fprintf(stderr, "failed to create compositor\n");
1991 exit(EXIT_FAILURE);
1992 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001993
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001994 if (shell_init(ec) < 0)
1995 exit(EXIT_FAILURE);
1996
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001997 if (wl_display_add_socket(display, option_socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001998 fprintf(stderr, "failed to add socket: %m\n");
1999 exit(EXIT_FAILURE);
2000 }
2001
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002002 loop = wl_display_get_event_loop(ec->wl_display);
2003 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
2004 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec);
2005
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002006 wl_display_run(display);
2007
Benjamin Franzkecdd9db72011-04-25 17:47:44 +02002008 if (strstr(eglQueryString(ec->display, EGL_EXTENSIONS),
2009 "EGL_WL_bind_wayland_display"))
2010 eglUnbindWaylandDisplayWL(ec->display, display);
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002011 wl_display_destroy(display);
2012
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002013 ec->destroy(ec);
2014
Kristian Høgsberg122912c2008-12-05 11:13:50 -05002015 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002016}