blob: 5fd2d708d1b17646e5e1004fe5b91872cd6ca677 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05008 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050017 */
18
Kristian Høgsberg06bc2642010-12-01 09:50:16 -050019#define _GNU_SOURCE
20
Kristian Høgsberga9410222011-01-14 17:22:35 -050021#include "config.h"
22
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040023#include <stdio.h>
24#include <string.h>
25#include <stdlib.h>
26#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010027#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050028#include <stdarg.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040029#include <sys/ioctl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040030#include <fcntl.h>
31#include <unistd.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050032#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050033#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040034#include <linux/input.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050035
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036#include "wayland-server.h"
Kristian Høgsberg82863022010-06-04 21:52:02 -040037#include "compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050038
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010039/* The plan here is to generate a random anonymous socket name and
40 * advertise that through a service on the session dbus.
41 */
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -050042static const char *option_socket_name = NULL;
Kristian Høgsberg61a82512010-10-26 11:26:44 -040043static const char *option_background = "background.jpg";
44static const char *option_geometry = "1024x640";
Kristian Høgsberge10a5d92011-04-22 14:01:18 -040045static int option_idle_time = 5;
Kristian Høgsberg61a82512010-10-26 11:26:44 -040046static int option_connector = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050047
48static const GOptionEntry option_entries[] = {
49 { "background", 'b', 0, G_OPTION_ARG_STRING,
50 &option_background, "Background image" },
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -040051 { "connector", 'c', 0, G_OPTION_ARG_INT,
52 &option_connector, "KMS connector" },
Kristian Høgsberg61a82512010-10-26 11:26:44 -040053 { "geometry", 'g', 0, G_OPTION_ARG_STRING,
54 &option_geometry, "Geometry" },
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010055 { "socket", 's', 0, G_OPTION_ARG_STRING,
56 &option_socket_name, "Socket Name" },
Kristian Høgsberge10a5d92011-04-22 14:01:18 -040057 { "idle-time", 'i', 0, G_OPTION_ARG_INT,
58 &option_idle_time, "Screensaver idle time" },
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050059 { NULL }
60};
61
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050062static void
63wlsc_matrix_init(struct wlsc_matrix *matrix)
64{
65 static const struct wlsc_matrix identity = {
66 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
67 };
68
69 memcpy(matrix, &identity, sizeof identity);
70}
71
72static void
73wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
74{
75 struct wlsc_matrix tmp;
Kristian Høgsberg27803c62010-06-06 22:23:21 -040076 const GLfloat *row, *column;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050077 div_t d;
78 int i, j;
79
80 for (i = 0; i < 16; i++) {
81 tmp.d[i] = 0;
82 d = div(i, 4);
83 row = m->d + d.quot * 4;
84 column = n->d + d.rem;
85 for (j = 0; j < 4; j++)
86 tmp.d[i] += row[j] * column[j * 4];
87 }
88 memcpy(m, &tmp, sizeof tmp);
89}
90
91static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040092wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050093{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -050094 struct wlsc_matrix translate = {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050095 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
96 };
97
98 wlsc_matrix_multiply(matrix, &translate);
99}
100
101static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400102wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500103{
104 struct wlsc_matrix scale = {
105 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
106 };
107
108 wlsc_matrix_multiply(matrix, &scale);
109}
110
111static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400112wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
113{
114 int i, j;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400115 struct wlsc_vector t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400116
117 for (i = 0; i < 4; i++) {
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400118 t.f[i] = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400119 for (j = 0; j < 4; j++)
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400120 t.f[i] += v->f[j] * matrix->d[i + j * 4];
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400121 }
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500122
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400123 *v = t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400124}
125
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200126struct wlsc_surface *
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400127wlsc_surface_create(struct wlsc_compositor *compositor,
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400128 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500129{
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400130 struct wlsc_surface *surface;
131
132 surface = malloc(sizeof *surface);
133 if (surface == NULL)
134 return NULL;
135
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500136 wl_list_init(&surface->surface.destroy_listener_list);
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500137 wl_list_init(&surface->link);
Benjamin Franzkebab41fb2011-03-07 18:04:59 +0100138 wl_list_init(&surface->buffer_link);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500139 surface->map_type = WLSC_SURFACE_MAP_UNMAPPED;
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500140
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500141 glGenTextures(1, &surface->texture);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400142 glBindTexture(GL_TEXTURE_2D, surface->texture);
143 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
144 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
145 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
146 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
147
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500148 surface->compositor = compositor;
Kristian Høgsbergc5d6be92011-01-14 16:22:37 -0500149 surface->visual = NULL;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200150 surface->image = EGL_NO_IMAGE_KHR;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200151 surface->saved_texture = 0;
Benjamin Franzkebab41fb2011-03-07 18:04:59 +0100152 surface->buffer = NULL;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400153 surface->x = x;
154 surface->y = y;
155 surface->width = width;
156 surface->height = height;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400157 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400158 wlsc_matrix_scale(&surface->matrix, width, height, 1);
159 wlsc_matrix_translate(&surface->matrix, x, y, 0);
160
161 wlsc_matrix_init(&surface->matrix_inv);
162 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
163 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500164
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400165 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500166}
167
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500168void
169wlsc_surface_damage_rectangle(struct wlsc_surface *surface,
170 int32_t x, int32_t y,
171 int32_t width, int32_t height)
172{
173 struct wlsc_compositor *compositor = surface->compositor;
174
175 pixman_region32_union_rect(&compositor->damage_region,
176 &compositor->damage_region,
177 surface->x + x, surface->y + y,
178 width, height);
179 wlsc_compositor_schedule_repaint(compositor);
180}
181
182void
183wlsc_surface_damage(struct wlsc_surface *surface)
184{
185 wlsc_surface_damage_rectangle(surface, 0, 0,
186 surface->width, surface->height);
187}
188
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500189uint32_t
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500190get_time(void)
191{
192 struct timeval tv;
193
194 gettimeofday(&tv, NULL);
195
196 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
197}
198
Kristian Høgsberg54879822008-11-23 17:07:32 -0500199static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400200destroy_surface(struct wl_resource *resource, struct wl_client *client)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500201{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400202 struct wlsc_surface *surface =
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500203 container_of(resource, struct wlsc_surface, surface.resource);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500204 struct wl_listener *l, *next;
Kristian Høgsberg4685fa32010-12-06 21:38:50 -0500205 uint32_t time;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400206
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500207 wlsc_surface_damage(surface);
208
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400209 wl_list_remove(&surface->link);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200210 if (surface->saved_texture == 0)
211 glDeleteTextures(1, &surface->texture);
212 else
213 glDeleteTextures(1, &surface->saved_texture);
214
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400215
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200216 if (surface->image != EGL_NO_IMAGE_KHR)
217 eglDestroyImageKHR(surface->compositor->display,
218 surface->image);
219
Benjamin Franzkebab41fb2011-03-07 18:04:59 +0100220 wl_list_remove(&surface->buffer_link);
221
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500222 time = get_time();
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500223 wl_list_for_each_safe(l, next,
224 &surface->surface.destroy_listener_list, link)
Kristian Høgsberg4685fa32010-12-06 21:38:50 -0500225 l->func(l, &surface->surface, time);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400226
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400227 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500228}
229
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500230uint32_t *
231wlsc_load_image(const char *filename, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500232{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400233 GdkPixbuf *pixbuf;
234 GError *error = NULL;
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500235 int stride, i, n_channels;
236 unsigned char *pixels, *end, *argb_pixels, *s, *d;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500237
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400238 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
239 width, height,
240 FALSE, &error);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500241 if (error != NULL) {
242 fprintf(stderr, "failed to load image: %s\n", error->message);
243 g_error_free(error);
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500244 return NULL;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500245 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400246
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500247 stride = gdk_pixbuf_get_rowstride(pixbuf);
248 pixels = gdk_pixbuf_get_pixels(pixbuf);
249 n_channels = gdk_pixbuf_get_n_channels(pixbuf);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400250
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500251 argb_pixels = malloc (height * width * 4);
252 if (argb_pixels == NULL) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500253 g_object_unref(pixbuf);
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500254 return NULL;
255 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400256
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500257 if (n_channels == 4) {
258 for (i = 0; i < height; i++) {
259 s = pixels + i * stride;
260 end = s + width * 4;
261 d = argb_pixels + i * width * 4;
262 while (s < end) {
263 unsigned int t;
264
265#define MULT(_d,c,a,t) \
266 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
267
268 MULT(d[0], s[2], s[3], t);
269 MULT(d[1], s[1], s[3], t);
270 MULT(d[2], s[0], s[3], t);
271 d[3] = s[3];
272 s += 4;
273 d += 4;
274 }
275 }
276 } else if (n_channels == 3) {
277 for (i = 0; i < height; i++) {
278 s = pixels + i * stride;
279 end = s + width * 3;
280 d = argb_pixels + i * width * 4;
281 while (s < end) {
282 d[0] = s[2];
283 d[1] = s[1];
284 d[2] = s[0];
285 d[3] = 0xff;
286 s += 3;
287 d += 4;
288 }
289 }
290 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400291
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500292 g_object_unref(pixbuf);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400293
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500294 return (uint32_t *) argb_pixels;
295}
296
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100297static void
298wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
299{
300 struct wlsc_surface *es = (struct wlsc_surface *) surface;
301 struct wlsc_compositor *ec = es->compositor;
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100302 struct wl_list *surfaces_attached_to;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100303
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100304 if (es->saved_texture != 0)
305 es->texture = es->saved_texture;
306
307 glBindTexture(GL_TEXTURE_2D, es->texture);
308
309 if (wl_buffer_is_shm(buffer)) {
310 /* Unbind any EGLImage texture that may be bound, so we don't
311 * overwrite it.*/
312 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
313 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
314 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
315 buffer->width, buffer->height, 0,
316 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
317 wl_shm_buffer_get_data(buffer));
318 es->visual = buffer->visual;
319
320 surfaces_attached_to = buffer->user_data;
321
322 if (es->buffer)
323 wl_list_remove(&es->buffer_link);
324 wl_list_insert(surfaces_attached_to, &es->buffer_link);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100325 } else {
Kristian Høgsbergdf2f1972011-04-21 23:48:13 -0400326 es->image = eglCreateImageKHR(ec->display, NULL,
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200327 EGL_WAYLAND_BUFFER_WL,
328 buffer, NULL);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100329
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200330 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, es->image);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100331 es->visual = buffer->visual;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100332 }
333}
334
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200335static void
336wlsc_sprite_attach(struct wlsc_sprite *sprite, struct wl_surface *surface)
337{
338 struct wlsc_surface *es = (struct wlsc_surface *) surface;
339
340 es->image = sprite->image;
341 if (sprite->image != EGL_NO_IMAGE_KHR) {
342 glBindTexture(GL_TEXTURE_2D, es->texture);
343 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, es->image);
344 } else {
345 if (es->saved_texture == 0)
346 es->saved_texture = es->texture;
347 es->texture = sprite->texture;
348 }
349
350 es->visual = sprite->visual;
351}
352
353enum sprite_usage {
354 SPRITE_USE_CURSOR = (1 << 0),
355};
356
357static struct wlsc_sprite *
358create_sprite_from_png(struct wlsc_compositor *ec,
359 const char *filename, int width, int height,
360 uint32_t usage)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500361{
362 uint32_t *pixels;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200363 struct wlsc_sprite *sprite;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500364
365 pixels = wlsc_load_image(filename, width, height);
Tim Wiederhakeac5c5e72011-01-27 01:32:36 +0100366 if(pixels == NULL)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200367 return NULL;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500368
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200369 sprite = malloc(sizeof *sprite);
370 if (sprite == NULL) {
371 free(pixels);
372 return NULL;
373 }
374
375 sprite->visual = &ec->compositor.premultiplied_argb_visual;
376 sprite->width = width;
377 sprite->height = height;
378 sprite->image = EGL_NO_IMAGE_KHR;
379
380 if (usage & SPRITE_USE_CURSOR && ec->create_cursor_image != NULL)
381 sprite->image = ec->create_cursor_image(ec, width, height);
382
383 glGenTextures(1, &sprite->texture);
384 glBindTexture(GL_TEXTURE_2D, sprite->texture);
385 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
386 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
387 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
388 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
389
390 if (sprite->image != EGL_NO_IMAGE_KHR) {
391 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, sprite->image);
392 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
393 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
394 } else {
395 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, width, height, 0,
396 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
397 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500398
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500399 free(pixels);
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500400
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200401 return sprite;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500402}
403
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400404static const struct {
405 const char *filename;
406 int hotspot_x, hotspot_y;
407} pointer_images[] = {
Kristian Høgsberg4219a402010-08-16 16:43:03 -0400408 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
409 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
410 { DATADIR "/wayland/bottom_side.png", 16, 20 },
411 { DATADIR "/wayland/grabbing.png", 20, 17 },
412 { DATADIR "/wayland/left_ptr.png", 10, 5 },
413 { DATADIR "/wayland/left_side.png", 10, 20 },
414 { DATADIR "/wayland/right_side.png", 30, 19 },
415 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
416 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
417 { DATADIR "/wayland/top_side.png", 18, 8 },
418 { DATADIR "/wayland/xterm.png", 15, 15 }
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400419};
420
421static void
422create_pointer_images(struct wlsc_compositor *ec)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500423{
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400424 int i, count;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400425 const int width = 32, height = 32;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400426
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400427 count = ARRAY_LENGTH(pointer_images);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200428 ec->pointer_sprites = malloc(count * sizeof *ec->pointer_sprites);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400429 for (i = 0; i < count; i++) {
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200430 ec->pointer_sprites[i] =
431 create_sprite_from_png(ec,
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500432 pointer_images[i].filename,
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200433 width, height,
434 SPRITE_USE_CURSOR);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400435 }
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400436}
437
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500438static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500439background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500440{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500441 struct wlsc_surface *background;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200442 struct wlsc_sprite *sprite;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500443
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400444 background = wlsc_surface_create(output->compositor,
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400445 output->x, output->y,
446 output->width, output->height);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500447 if (background == NULL)
448 return NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500449
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200450 sprite = create_sprite_from_png(output->compositor, filename,
451 output->width, output->height, 0);
452 if (sprite == NULL) {
Benjamin Franzked3b023e2011-01-15 12:34:49 +0100453 free(background);
454 return NULL;
455 }
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100456
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200457 wlsc_sprite_attach(sprite, &background->surface);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500458
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500459 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500460}
461
462static void
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500463wlsc_surface_draw(struct wlsc_surface *es,
464 struct wlsc_output *output, pixman_region32_t *clip)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500465{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500466 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500467 GLfloat *v, inv_width, inv_height;
468 unsigned int *p;
469 pixman_region32_t repaint;
470 pixman_box32_t *rectangles;
471 int i, n;
472
473 pixman_region32_init_rect(&repaint,
474 es->x, es->y, es->width, es->height);
475 pixman_region32_intersect(&repaint, &repaint, clip);
476 if (!pixman_region32_not_empty(&repaint))
477 return;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500478
Kristian Høgsbergc5c510e2010-12-08 15:12:58 -0500479 if (es->visual == &ec->compositor.argb_visual) {
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500480 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
481 glEnable(GL_BLEND);
Kristian Høgsbergc5c510e2010-12-08 15:12:58 -0500482 } else if (es->visual == &ec->compositor.premultiplied_argb_visual) {
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500483 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
484 glEnable(GL_BLEND);
485 } else {
486 glDisable(GL_BLEND);
487 }
488
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500489 rectangles = pixman_region32_rectangles(&repaint, &n);
490 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
491 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
492 inv_width = 1.0 / es->width;
493 inv_height = 1.0 / es->height;
494 for (i = 0; i < n; i++, v += 16, p += 6) {
495 v[ 0] = rectangles[i].x1;
496 v[ 1] = rectangles[i].y1;
497 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
498 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500499
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500500 v[ 4] = rectangles[i].x1;
501 v[ 5] = rectangles[i].y2;
502 v[ 6] = v[ 2];
503 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500504
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500505 v[ 8] = rectangles[i].x2;
506 v[ 9] = rectangles[i].y1;
507 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
508 v[11] = v[ 3];
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500509
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500510 v[12] = rectangles[i].x2;
511 v[13] = rectangles[i].y2;
512 v[14] = v[10];
513 v[15] = v[ 7];
514
515 p[0] = i * 4 + 0;
516 p[1] = i * 4 + 1;
517 p[2] = i * 4 + 2;
518 p[3] = i * 4 + 2;
519 p[4] = i * 4 + 1;
520 p[5] = i * 4 + 3;
521 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500522
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500523 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500524 v = ec->vertices.data;
525 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
526 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400527 glEnableVertexAttribArray(0);
528 glEnableVertexAttribArray(1);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500529 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
530
531 ec->vertices.size = 0;
532 ec->indices.size = 0;
533 pixman_region32_fini(&repaint);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500534}
535
536static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400537wlsc_surface_raise(struct wlsc_surface *surface)
538{
539 struct wlsc_compositor *compositor = surface->compositor;
540
541 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400542 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400543}
544
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500545void
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400546wlsc_surface_update_matrix(struct wlsc_surface *es)
547{
548 wlsc_matrix_init(&es->matrix);
549 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
550 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
551
552 wlsc_matrix_init(&es->matrix_inv);
553 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
554 wlsc_matrix_scale(&es->matrix_inv,
555 1.0 / es->width, 1.0 / es->height, 1);
556}
557
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400558void
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400559wlsc_compositor_damage_all(struct wlsc_compositor *compositor)
560{
561 struct wlsc_output *output;
562
563 wl_list_for_each(output, &compositor->output_list, link)
564 wlsc_output_damage(output);
565}
566
567void
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100568wlsc_output_finish_frame(struct wlsc_output *output, int msecs)
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400569{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100570 struct wlsc_compositor *compositor = output->compositor;
571 struct wlsc_surface *es;
572
573 wl_list_for_each(es, &compositor->surface_list, link) {
574 if (es->output == output) {
575 wl_display_post_frame(compositor->wl_display,
576 &es->surface, msecs);
577 }
578 }
579
580 output->finished = 1;
581
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400582 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsbergf30c67e2011-02-06 12:58:44 -0500583 compositor->repaint_on_timeout = 1;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400584}
585
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400586void
587wlsc_output_damage(struct wlsc_output *output)
588{
589 struct wlsc_compositor *compositor = output->compositor;
590
591 pixman_region32_union_rect(&compositor->damage_region,
592 &compositor->damage_region,
593 output->x, output->y,
594 output->width, output->height);
595 wlsc_compositor_schedule_repaint(compositor);
596}
597
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400598static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400599wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400600{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500601 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500602 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500603 struct wlsc_input_device *eid;
Kristian Høgsbergcfc6d272011-04-11 13:34:24 -0400604 pixman_region32_t new_damage, total_damage, repaint;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200605 int using_hardware_cursor = 1;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500606
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100607 output->prepare_render(output);
608
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500609 glViewport(0, 0, output->width, output->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500610
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500611 glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, output->matrix.d);
612 glUniform1i(ec->tex_uniform, 0);
613
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500614 pixman_region32_init(&new_damage);
615 pixman_region32_init(&total_damage);
616 pixman_region32_intersect_rect(&new_damage,
617 &ec->damage_region,
618 output->x, output->y,
619 output->width, output->height);
620 pixman_region32_subtract(&ec->damage_region,
621 &ec->damage_region, &new_damage);
622 pixman_region32_union(&total_damage, &new_damage,
623 &output->previous_damage_region);
624 pixman_region32_copy(&output->previous_damage_region, &new_damage);
625
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400626 if (ec->state == WLSC_COMPOSITOR_SLEEPING) {
627 glClear(GL_COLOR_BUFFER_BIT);
628 return;
629 }
630
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200631 if (ec->focus)
632 if (output->set_hardware_cursor(output, ec->input_device) < 0)
633 using_hardware_cursor = 0;
634
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500635 es = container_of(ec->surface_list.next, struct wlsc_surface, link);
636 if (es->map_type == WLSC_SURFACE_MAP_FULLSCREEN &&
637 es->fullscreen_output == output) {
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200638 if (es->visual == &ec->compositor.rgb_visual &&
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200639 using_hardware_cursor) {
640 if (output->prepare_scanout_surface(output, es) == 0) {
641 /* We're drawing nothing now,
642 * draw the damaged regions later. */
643 pixman_region32_union(&ec->damage_region,
644 &ec->damage_region,
645 &total_damage);
646 return;
647 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200648 }
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200649
650 if (es->width < output->width ||
651 es->height < output->height)
652 glClear(GL_COLOR_BUFFER_BIT);
653 wlsc_surface_draw(es, output, &total_damage);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500654 } else {
Kristian Høgsbergcfc6d272011-04-11 13:34:24 -0400655 wl_list_for_each(es, &ec->surface_list, link) {
656 if (es->visual != &ec->compositor.rgb_visual)
657 continue;
658
659 pixman_region32_init_rect(&repaint,
660 es->x, es->y,
661 es->width, es->height);
662 wlsc_surface_draw(es, output, &total_damage);
663 pixman_region32_subtract(&total_damage,
664 &total_damage, &repaint);
665 }
666
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500667 if (output->background)
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500668 wlsc_surface_draw(output->background,
669 output, &total_damage);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500670 else
671 glClear(GL_COLOR_BUFFER_BIT);
672
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500673 wl_list_for_each_reverse(es, &ec->surface_list, link) {
Kristian Høgsberg547cadf2011-04-12 22:23:30 -0400674 if (ec->overlay == es)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500675 continue;
676
Kristian Høgsbergcfc6d272011-04-11 13:34:24 -0400677 if (es->visual == &ec->compositor.rgb_visual) {
678 pixman_region32_union_rect(&total_damage,
679 &total_damage,
680 es->x, es->y,
681 es->width, es->height);
682 continue;
683 }
684
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500685 wlsc_surface_draw(es, output, &total_damage);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500686 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500687 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400688
Kristian Høgsberg547cadf2011-04-12 22:23:30 -0400689 if (ec->overlay)
690 wlsc_surface_draw(ec->overlay, output, &total_damage);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -0500691
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400692 if (ec->focus)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200693 wl_list_for_each(eid, &ec->input_device_list, link) {
694 if (&eid->input_device != ec->input_device ||
695 !using_hardware_cursor)
696 wlsc_surface_draw(eid->sprite, output,
697 &total_damage);
698 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500699}
700
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400701static int
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500702repaint(void *data)
703{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500704 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500705 struct wlsc_output *output;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100706 int repainted_all_outputs = 1;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500707
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100708 wl_list_for_each(output, &ec->output_list, link) {
709 if (!output->repaint_needed)
710 continue;
711
712 if (!output->finished) {
713 repainted_all_outputs = 0;
714 continue;
715 }
716
717 wlsc_output_repaint(output);
718 output->finished = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100719 output->repaint_needed = 0;
Kristian Høgsberg11e28282011-04-11 16:47:50 -0400720 output->present(output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500721 }
722
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100723 if (repainted_all_outputs)
724 ec->repaint_on_timeout = 0;
725 else
726 wl_event_source_timer_update(ec->timer_source, 1);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400727
728 return 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400729}
730
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400731void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400732wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400733{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100734 struct wlsc_output *output;
735
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400736 if (compositor->state == WLSC_COMPOSITOR_SLEEPING)
737 return;
738
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100739 wl_list_for_each(output, &compositor->output_list, link)
740 output->repaint_needed = 1;
741
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400742 if (compositor->repaint_on_timeout)
743 return;
744
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400745 wl_event_source_timer_update(compositor->timer_source, 1);
746 compositor->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400747}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500748
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500749static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500750surface_destroy(struct wl_client *client,
751 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400752{
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500753 wl_resource_destroy(&surface->resource, client);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400754}
755
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100756void
757wlsc_surface_assign_output(struct wlsc_surface *es)
758{
759 struct wlsc_compositor *ec = es->compositor;
760 struct wlsc_output *output;
761
762 struct wlsc_output *tmp = es->output;
763 es->output = NULL;
764
765 wl_list_for_each(output, &ec->output_list, link) {
766 if (output->x < es->x && es->x < output->x + output->width &&
767 output->y < es->y && es->y < output->y + output->height) {
768 if (output != tmp)
769 printf("assiging surface %p to output %p\n",
770 es, output);
771 es->output = output;
772 }
773 }
774
775 if (es->output == NULL) {
776 printf("no output found\n");
777 es->output = container_of(ec->output_list.next,
778 struct wlsc_output, link);
779 }
780}
781
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500782static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500783surface_attach(struct wl_client *client,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500784 struct wl_surface *surface, struct wl_buffer *buffer,
785 int32_t x, int32_t y)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400786{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500787 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400788
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500789 /* FIXME: This damages the entire old surface, but we should
790 * really just damage the part that's no longer covered by the
791 * surface. Anything covered by the new surface will be
792 * damaged by the client. */
793 wlsc_surface_damage(es);
794
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100795 wlsc_buffer_attach(buffer, surface);
796
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -0400797 es->buffer = buffer;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500798 es->x += x;
799 es->y += y;
800 es->width = buffer->width;
801 es->height = buffer->height;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100802 if (x != 0 || y != 0)
803 wlsc_surface_assign_output(es);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500804 wlsc_surface_update_matrix(es);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400805}
806
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500807static void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500808surface_map_toplevel(struct wl_client *client,
809 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400810{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500811 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100812 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400813
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500814 switch (es->map_type) {
815 case WLSC_SURFACE_MAP_UNMAPPED:
816 es->x = 10 + random() % 400;
817 es->y = 10 + random() % 400;
818 wlsc_surface_update_matrix(es);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100819 /* assign to first output */
820 es->output = container_of(ec->output_list.next,
821 struct wlsc_output, link);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500822 wl_list_insert(&es->compositor->surface_list, &es->link);
823 break;
824 case WLSC_SURFACE_MAP_TOPLEVEL:
Kristian Høgsberg8f66a572011-01-07 08:38:56 -0500825 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500826 case WLSC_SURFACE_MAP_FULLSCREEN:
827 es->fullscreen_output = NULL;
828 es->x = es->saved_x;
829 es->y = es->saved_y;
830 wlsc_surface_update_matrix(es);
831 break;
832 default:
833 break;
834 }
Kristian Høgsberg8f66a572011-01-07 08:38:56 -0500835
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500836 wlsc_surface_damage(es);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500837 es->map_type = WLSC_SURFACE_MAP_TOPLEVEL;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400838}
839
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500840static void
Kristian Høgsberg8dc378f2011-01-21 18:02:24 -0500841surface_map_transient(struct wl_client *client,
842 struct wl_surface *surface, struct wl_surface *parent,
843 int x, int y, uint32_t flags)
844{
845 struct wlsc_surface *es = (struct wlsc_surface *) surface;
846 struct wlsc_surface *pes = (struct wlsc_surface *) parent;
847
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500848 switch (es->map_type) {
849 case WLSC_SURFACE_MAP_UNMAPPED:
850 wl_list_insert(&es->compositor->surface_list, &es->link);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100851 /* assign to parents output */
852 es->output = pes->output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500853 break;
854 case WLSC_SURFACE_MAP_FULLSCREEN:
855 es->fullscreen_output = NULL;
856 break;
857 default:
858 break;
859 }
Kristian Høgsberg8dc378f2011-01-21 18:02:24 -0500860
861 es->x = pes->x + x;
862 es->y = pes->y + y;
863
864 wlsc_surface_update_matrix(es);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500865 wlsc_surface_damage(es);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500866 es->map_type = WLSC_SURFACE_MAP_TRANSIENT;
867}
868
869static void
870surface_map_fullscreen(struct wl_client *client, struct wl_surface *surface)
871{
872 struct wlsc_surface *es = (struct wlsc_surface *) surface;
873 struct wlsc_output *output;
874
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100875 /* FIXME: Fullscreen on first output */
876 /* FIXME: Handle output going away */
877 output = container_of(es->compositor->output_list.next,
878 struct wlsc_output, link);
879
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500880 switch (es->map_type) {
881 case WLSC_SURFACE_MAP_UNMAPPED:
882 es->x = 10 + random() % 400;
883 es->y = 10 + random() % 400;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100884 /* assign to first output */
885 es->output = output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500886 wl_list_insert(&es->compositor->surface_list, &es->link);
887 break;
888 case WLSC_SURFACE_MAP_FULLSCREEN:
889 return;
890 default:
891 break;
892 }
893
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500894 es->saved_x = es->x;
895 es->saved_y = es->y;
896 es->x = (output->width - es->width) / 2;
897 es->y = (output->height - es->height) / 2;
898 es->fullscreen_output = output;
899 wlsc_surface_update_matrix(es);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500900 wlsc_surface_damage(es);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500901 es->map_type = WLSC_SURFACE_MAP_FULLSCREEN;
Kristian Høgsberg8dc378f2011-01-21 18:02:24 -0500902}
903
904static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500905surface_damage(struct wl_client *client,
906 struct wl_surface *surface,
907 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500908{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400909 struct wlsc_surface *es = (struct wlsc_surface *) surface;
910
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500911 wlsc_surface_damage_rectangle(es, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500912}
913
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500914const static struct wl_surface_interface surface_interface = {
915 surface_destroy,
916 surface_attach,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500917 surface_map_toplevel,
Kristian Høgsberg8dc378f2011-01-21 18:02:24 -0500918 surface_map_transient,
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500919 surface_map_fullscreen,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500920 surface_damage
921};
922
923static void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400924wlsc_input_device_attach(struct wlsc_input_device *device,
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200925 int x, int y, int width, int height)
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400926{
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500927 wlsc_surface_damage(device->sprite);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400928
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400929 device->hotspot_x = x;
930 device->hotspot_y = y;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400931
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500932 device->sprite->x = device->input_device.x - device->hotspot_x;
933 device->sprite->y = device->input_device.y - device->hotspot_y;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200934 device->sprite->width = width;
935 device->sprite->height = height;
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400936 wlsc_surface_update_matrix(device->sprite);
937
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500938 wlsc_surface_damage(device->sprite);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -0400939}
940
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200941static void
942wlsc_input_device_attach_buffer(struct wlsc_input_device *device,
943 struct wl_buffer *buffer, int x, int y)
944{
945 wlsc_buffer_attach(buffer, &device->sprite->surface);
946 wlsc_input_device_attach(device, x, y, buffer->width, buffer->height);
947}
948
949static void
950wlsc_input_device_attach_sprite(struct wlsc_input_device *device,
951 struct wlsc_sprite *sprite, int x, int y)
952{
953 wlsc_sprite_attach(sprite, &device->sprite->surface);
954 wlsc_input_device_attach(device, x, y, sprite->width, sprite->height);
955}
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400956
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500957void
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400958wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
959 enum wlsc_pointer_type type)
960{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500961 struct wlsc_compositor *compositor =
962 (struct wlsc_compositor *) device->input_device.compositor;
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400963
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200964 wlsc_input_device_attach_sprite(device,
965 compositor->pointer_sprites[type],
966 pointer_images[type].hotspot_x,
967 pointer_images[type].hotspot_y);
Kristian Høgsbergeef08fb2010-08-17 21:23:10 -0400968}
969
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400970static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500971compositor_create_surface(struct wl_client *client,
972 struct wl_compositor *compositor, uint32_t id)
973{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500974 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400975 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500976
Kristian Høgsbergc5d6be92011-01-14 16:22:37 -0500977 surface = wlsc_surface_create(ec, 0, 0, 0, 0);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400978 if (surface == NULL) {
Kristian Høgsberg13b8ae42010-09-02 20:55:16 -0400979 wl_client_post_no_memory(client);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500980 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400981 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500982
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500983 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -0400984
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500985 surface->surface.resource.object.id = id;
986 surface->surface.resource.object.interface = &wl_surface_interface;
987 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -0400988 (void (**)(void)) &surface_interface;
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500989 surface->surface.client = client;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -0400990
Kristian Høgsbergb313b022010-12-01 17:07:41 -0500991 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500992}
993
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500994const static struct wl_compositor_interface compositor_interface = {
995 compositor_create_surface,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500996};
997
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500998static void
999wlsc_surface_transform(struct wlsc_surface *surface,
1000 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1001{
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001002 struct wlsc_vector v = { { x, y, 0, 1 } };
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001003
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001004 wlsc_matrix_transform(&surface->matrix_inv, &v);
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -04001005 *sx = v.f[0] * surface->width;
1006 *sy = v.f[1] * surface->height;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001007}
1008
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001009struct wlsc_surface *
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001010pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001011{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001012 struct wlsc_compositor *ec =
1013 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001014 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001015
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001016 wl_list_for_each(es, &ec->surface_list, link) {
1017 wlsc_surface_transform(es, device->x, device->y, sx, sy);
1018 if (0 <= *sx && *sx < es->width &&
1019 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001020 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001021 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001022
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001023 return NULL;
1024}
1025
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001026
1027static void
1028motion_grab_motion(struct wl_grab *grab,
1029 uint32_t time, int32_t x, int32_t y)
1030{
1031 struct wlsc_input_device *device =
1032 (struct wlsc_input_device *) grab->input_device;
1033 struct wlsc_surface *es =
1034 (struct wlsc_surface *) device->input_device.pointer_focus;
1035 int32_t sx, sy;
1036
1037 wlsc_surface_transform(es, x, y, &sx, &sy);
1038 wl_client_post_event(es->surface.client,
1039 &device->input_device.object,
1040 WL_INPUT_DEVICE_MOTION,
1041 time, x, y, sx, sy);
1042}
1043
1044static void
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001045motion_grab_button(struct wl_grab *grab,
1046 uint32_t time, int32_t button, int32_t state)
1047{
1048 wl_client_post_event(grab->input_device->pointer_focus->client,
1049 &grab->input_device->object,
1050 WL_INPUT_DEVICE_BUTTON,
1051 time, button, state);
1052}
1053
1054static void
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001055motion_grab_end(struct wl_grab *grab, uint32_t time)
1056{
1057}
1058
1059static const struct wl_grab_interface motion_grab_interface = {
1060 motion_grab_motion,
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001061 motion_grab_button,
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001062 motion_grab_end
1063};
1064
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001065void
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001066wlsc_compositor_wake(struct wlsc_compositor *compositor)
1067{
1068 if (compositor->idle_inhibit)
1069 return;
1070
1071 if (compositor->state == WLSC_COMPOSITOR_SLEEPING) {
1072 compositor->state = WLSC_COMPOSITOR_ACTIVE;
1073 wlsc_compositor_damage_all(compositor);
1074 }
1075
1076 wl_event_source_timer_update(compositor->idle_source,
1077 option_idle_time * 1000);
1078}
1079
1080static void
1081wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor)
1082{
1083 wlsc_compositor_wake(compositor);
1084 compositor->idle_inhibit++;
1085}
1086
1087static void
1088wlsc_compositor_idle_release(struct wlsc_compositor *compositor)
1089{
1090 compositor->idle_inhibit--;
1091 wlsc_compositor_wake(compositor);
1092}
1093
1094static int
1095idle_handler(void *data)
1096{
1097 struct wlsc_compositor *compositor = data;
1098
1099 if (compositor->idle_inhibit)
1100 return 1;
1101
1102 wlsc_compositor_damage_all(compositor);
1103 compositor->state = WLSC_COMPOSITOR_SLEEPING;
1104
1105 return 1;
1106}
1107
1108void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001109notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001110{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001111 struct wlsc_surface *es;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001112 struct wlsc_compositor *ec =
1113 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001114 struct wlsc_output *output;
Kristian Høgsberg6d65d5f2010-12-07 13:30:18 -05001115 const struct wl_grab_interface *interface;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001116 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsbergfc9c28a2010-12-07 13:04:43 -05001117 int32_t sx, sy;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001118 int x_valid = 0, y_valid = 0;
1119 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 -05001120
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001121 wlsc_compositor_wake(ec);
1122
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001123 wl_list_for_each(output, &ec->output_list, link) {
1124 if (output->x <= x && x <= output->x + output->width)
1125 x_valid = 1;
1126
1127 if (output->y <= y && y <= output->y + output->height)
1128 y_valid = 1;
1129
1130 /* FIXME: calculate this only on output addition/deletion */
1131 if (output->x < min_x)
1132 min_x = output->x;
1133 if (output->y < min_y)
1134 min_y = output->y;
1135
1136 if (output->x + output->width > max_x)
1137 max_x = output->x + output->width;
1138 if (output->y + output->height > max_y)
1139 max_y = output->y + output->height;
1140 }
1141
1142 if (!x_valid) {
1143 if (x < min_x)
1144 x = min_x;
1145 else if (x >= max_x)
1146 x = max_x;
1147 }
1148 if (!y_valid) {
1149 if (y < min_y)
1150 y = min_y;
1151 else if (y >= max_y)
1152 y = max_y;
1153 }
Ray Strode90e701d2008-12-18 23:05:43 -05001154
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -05001155 device->x = x;
1156 device->y = y;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001157
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001158 if (device->grab) {
1159 interface = device->grab->interface;
1160 interface->motion(device->grab, time, x, y);
1161 } else {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001162 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001163 wl_input_device_set_pointer_focus(device,
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001164 &es->surface,
Kristian Høgsberg26437072010-12-01 10:17:47 -05001165 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001166 if (es)
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001167 wl_client_post_event(es->surface.client,
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001168 &device->object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001169 WL_INPUT_DEVICE_MOTION,
1170 time, x, y, sx, sy);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001171 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001172
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001173 wlsc_surface_damage(wd->sprite);
1174
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001175 wd->sprite->x = device->x - wd->hotspot_x;
1176 wd->sprite->y = device->y - wd->hotspot_y;
1177 wlsc_surface_update_matrix(wd->sprite);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001178
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001179 wlsc_surface_damage(wd->sprite);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001180}
1181
Kristian Høgsberg30021d72011-04-12 17:42:30 -04001182void
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001183wlsc_surface_activate(struct wlsc_surface *surface,
1184 struct wlsc_input_device *device, uint32_t time)
1185{
1186 wlsc_surface_raise(surface);
1187 if (device->selection)
1188 wlsc_selection_set_focus(device->selection,
1189 &surface->surface, time);
1190
1191 wl_input_device_set_keyboard_focus(&device->input_device,
1192 &surface->surface,
1193 time);
1194}
1195
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001196struct wlsc_binding {
1197 uint32_t key;
1198 uint32_t button;
1199 uint32_t modifier;
1200 wlsc_binding_handler_t handler;
1201 void *data;
1202 struct wl_list link;
1203};
1204
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001205void
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001206notify_button(struct wl_input_device *device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001207 uint32_t time, int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001208{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001209 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001210 struct wlsc_compositor *compositor =
1211 (struct wlsc_compositor *) device->compositor;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001212 struct wlsc_binding *b;
1213 struct wlsc_surface *surface =
1214 (struct wlsc_surface *) device->pointer_focus;
Kristian Høgsbergea081152010-12-07 08:59:51 -05001215
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001216 if (state)
1217 wlsc_compositor_idle_inhibit(compositor);
1218 else
1219 wlsc_compositor_idle_release(compositor);
1220
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -05001221 if (state && surface && device->grab == NULL) {
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001222 wlsc_surface_activate(surface, wd, time);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001223 wl_input_device_start_grab(device,
1224 &device->motion_grab,
1225 button, time);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001226 }
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -05001227
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001228 wl_list_for_each(b, &compositor->binding_list, link) {
1229 if (b->button == button &&
1230 b->modifier == wd->modifier_state && state) {
1231 b->handler(&wd->input_device,
1232 time, 0, button, state, b->data);
1233 break;
1234 }
Benjamin Franzke5a2218a2011-02-01 16:30:31 +01001235 }
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001236
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -05001237 if (device->grab)
1238 device->grab->interface->button(device->grab, time,
1239 button, state);
Kristian Høgsbergdff2e3c2010-12-07 09:02:09 -05001240
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001241 if (!state && device->grab && device->grab_button == button)
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001242 wl_input_device_end_grab(device, time);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001243}
1244
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001245static void
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001246terminate_binding(struct wl_input_device *device, uint32_t time,
1247 uint32_t key, uint32_t button, uint32_t state, void *data)
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001248{
1249 struct wlsc_compositor *compositor = data;
1250
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001251 if (state)
1252 wl_display_terminate(compositor->wl_display);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001253}
1254
1255struct wlsc_binding *
1256wlsc_compositor_add_binding(struct wlsc_compositor *compositor,
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001257 uint32_t key, uint32_t button, uint32_t modifier,
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001258 wlsc_binding_handler_t handler, void *data)
1259{
1260 struct wlsc_binding *binding;
1261
1262 binding = malloc(sizeof *binding);
1263 if (binding == NULL)
1264 return NULL;
1265
1266 binding->key = key;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001267 binding->button = button;
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001268 binding->modifier = modifier;
1269 binding->handler = handler;
1270 binding->data = data;
1271 wl_list_insert(compositor->binding_list.prev, &binding->link);
1272
1273 return binding;
1274}
1275
1276void
1277wlsc_binding_destroy(struct wlsc_binding *binding)
1278{
1279 wl_list_remove(&binding->link);
1280 free(binding);
1281}
1282
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001283static void
1284update_modifier_state(struct wlsc_input_device *device,
1285 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001286{
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001287 uint32_t modifier;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001288
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001289 switch (key) {
1290 case KEY_LEFTCTRL:
1291 case KEY_RIGHTCTRL:
1292 modifier = MODIFIER_CTRL;
1293 break;
1294
1295 case KEY_LEFTALT:
1296 case KEY_RIGHTALT:
1297 modifier = MODIFIER_ALT;
1298 break;
1299
Kristian Høgsberg5b75f1b2010-08-04 23:21:41 -04001300 case KEY_LEFTMETA:
1301 case KEY_RIGHTMETA:
1302 modifier = MODIFIER_SUPER;
1303 break;
1304
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001305 default:
1306 modifier = 0;
1307 break;
1308 }
1309
1310 if (state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001311 device->modifier_state |= modifier;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001312 else
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001313 device->modifier_state &= ~modifier;
1314}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001315
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001316void
1317notify_key(struct wl_input_device *device,
1318 uint32_t time, uint32_t key, uint32_t state)
1319{
1320 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1321 struct wlsc_compositor *compositor =
1322 (struct wlsc_compositor *) device->compositor;
1323 uint32_t *k, *end;
1324 struct wlsc_binding *b;
1325
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001326 if (state)
1327 wlsc_compositor_idle_inhibit(compositor);
1328 else
1329 wlsc_compositor_idle_release(compositor);
1330
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001331 wl_list_for_each(b, &compositor->binding_list, link) {
1332 if (b->key == key &&
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001333 b->modifier == wd->modifier_state) {
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001334 b->handler(&wd->input_device,
1335 time, key, 0, state, b->data);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001336 break;
1337 }
1338 }
1339
1340 update_modifier_state(wd, key, state);
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001341 end = device->keys.data + device->keys.size;
1342 for (k = device->keys.data; k < end; k++) {
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001343 if (*k == key)
1344 *k = *--end;
1345 }
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001346 device->keys.size = (void *) end - device->keys.data;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001347 if (state) {
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001348 k = wl_array_add(&device->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001349 *k = key;
1350 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001351
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001352 if (device->keyboard_focus != NULL)
1353 wl_client_post_event(device->keyboard_focus->client,
1354 &device->object,
Kristian Høgsberg50038e42010-09-07 21:08:59 -04001355 WL_INPUT_DEVICE_KEY, time, key, state);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001356}
1357
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001358void
1359notify_pointer_focus(struct wl_input_device *device,
1360 uint32_t time, struct wlsc_output *output,
1361 int32_t x, int32_t y)
1362{
1363 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1364 struct wlsc_compositor *compositor =
1365 (struct wlsc_compositor *) device->compositor;
1366 struct wlsc_surface *es;
1367 int32_t sx, sy;
1368
1369 if (output) {
1370 device->x = x;
1371 device->y = y;
1372 es = pick_surface(device, &sx, &sy);
1373 wl_input_device_set_pointer_focus(device,
1374 &es->surface,
1375 time, x, y, sx, sy);
1376
1377 compositor->focus = 1;
1378
1379 wd->sprite->x = device->x - wd->hotspot_x;
1380 wd->sprite->y = device->y - wd->hotspot_y;
1381 wlsc_surface_update_matrix(wd->sprite);
1382 } else {
1383 wl_input_device_set_pointer_focus(device, NULL,
1384 time, 0, 0, 0, 0);
1385 compositor->focus = 0;
1386 }
1387
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001388 wlsc_surface_damage(wd->sprite);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001389}
1390
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001391void
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001392notify_keyboard_focus(struct wl_input_device *device,
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001393 uint32_t time, struct wlsc_output *output,
1394 struct wl_array *keys)
1395{
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001396 struct wlsc_input_device *wd =
1397 (struct wlsc_input_device *) device;
1398 struct wlsc_compositor *compositor =
1399 (struct wlsc_compositor *) device->compositor;
1400 struct wlsc_surface *es;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001401 uint32_t *k, *end;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001402
1403 if (!wl_list_empty(&compositor->surface_list))
1404 es = container_of(compositor->surface_list.next,
1405 struct wlsc_surface, link);
1406 else
1407 es = NULL;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001408
1409 if (output) {
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001410 wl_array_copy(&wd->input_device.keys, keys);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001411 wd->modifier_state = 0;
1412 end = device->keys.data + device->keys.size;
1413 for (k = device->keys.data; k < end; k++) {
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001414 wlsc_compositor_idle_inhibit(compositor);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001415 update_modifier_state(wd, *k, 1);
1416 }
1417
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001418 wl_input_device_set_keyboard_focus(&wd->input_device,
1419 &es->surface, time);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001420 } else {
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001421 end = device->keys.data + device->keys.size;
1422 for (k = device->keys.data; k < end; k++)
1423 wlsc_compositor_idle_release(compositor);
1424
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001425 wd->modifier_state = 0;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001426 wl_input_device_set_keyboard_focus(&wd->input_device,
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001427 NULL, time);
1428 }
1429}
1430
1431
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001432static void
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001433input_device_attach(struct wl_client *client,
1434 struct wl_input_device *device_base,
1435 uint32_t time,
1436 struct wl_buffer *buffer, int32_t x, int32_t y)
1437{
1438 struct wlsc_input_device *device =
1439 (struct wlsc_input_device *) device_base;
1440
1441 if (time < device->input_device.pointer_focus_time)
1442 return;
1443 if (device->input_device.pointer_focus == NULL)
1444 return;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001445 if (device->input_device.pointer_focus->client != client)
1446 return;
1447
1448 if (buffer == NULL) {
1449 wlsc_input_device_set_pointer_image(device,
1450 WLSC_POINTER_LEFT_PTR);
1451 return;
1452 }
1453
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001454 wlsc_input_device_attach_buffer(device, buffer, x, y);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05001455}
1456
1457const static struct wl_input_device_interface input_device_interface = {
1458 input_device_attach,
1459};
1460
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001461void
1462wlsc_input_device_init(struct wlsc_input_device *device,
1463 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001464{
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001465 wl_input_device_init(&device->input_device, &ec->compositor);
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05001466
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001467 device->input_device.object.interface = &wl_input_device_interface;
1468 device->input_device.object.implementation =
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001469 (void (**)(void)) &input_device_interface;
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001470 wl_display_add_object(ec->wl_display, &device->input_device.object);
1471 wl_display_add_global(ec->wl_display, &device->input_device.object, NULL);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001472
Kristian Høgsbergc5d6be92011-01-14 16:22:37 -05001473 device->sprite = wlsc_surface_create(ec,
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001474 device->input_device.x,
1475 device->input_device.y, 32, 32);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001476 device->hotspot_x = 16;
1477 device->hotspot_y = 16;
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001478 device->modifier_state = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001479
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -05001480 device->input_device.motion_grab.interface = &motion_grab_interface;
Kristian Høgsberg8321e692010-12-07 17:06:15 -05001481
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001482 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001483
1484 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001485}
1486
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001487static void
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001488wlsc_output_post_geometry(struct wl_client *client,
1489 struct wl_object *global, uint32_t version)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001490{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001491 struct wlsc_output *output =
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001492 container_of(global, struct wlsc_output, object);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001493
1494 wl_client_post_event(client, global,
1495 WL_OUTPUT_GEOMETRY,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001496 output->x, output->y,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001497 output->width, output->height);
1498}
1499
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001500static const char vertex_shader[] =
1501 "uniform mat4 proj;\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001502 "attribute vec2 position;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001503 "attribute vec2 texcoord;\n"
1504 "varying vec2 v_texcoord;\n"
1505 "void main()\n"
1506 "{\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001507 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001508 " v_texcoord = texcoord;\n"
1509 "}\n";
1510
1511static const char fragment_shader[] =
Kristian Høgsbergdfce71d2010-12-07 20:19:10 -05001512 "precision mediump float;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001513 "varying vec2 v_texcoord;\n"
1514 "uniform sampler2D tex;\n"
1515 "void main()\n"
1516 "{\n"
1517 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1518 "}\n";
1519
Kristian Høgsberga9468212010-06-14 21:03:11 -04001520static int
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001521init_shaders(struct wlsc_compositor *ec)
1522{
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001523 GLuint v, f, program;
1524 const char *p;
1525 char msg[512];
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001526 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001527
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001528 p = vertex_shader;
1529 v = glCreateShader(GL_VERTEX_SHADER);
1530 glShaderSource(v, 1, &p, NULL);
1531 glCompileShader(v);
1532 glGetShaderiv(v, GL_COMPILE_STATUS, &status);
1533 if (!status) {
1534 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
1535 fprintf(stderr, "vertex shader info: %s\n", msg);
1536 return -1;
1537 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001538
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001539 p = fragment_shader;
1540 f = glCreateShader(GL_FRAGMENT_SHADER);
1541 glShaderSource(f, 1, &p, NULL);
1542 glCompileShader(f);
1543 glGetShaderiv(f, GL_COMPILE_STATUS, &status);
1544 if (!status) {
1545 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
1546 fprintf(stderr, "fragment shader info: %s\n", msg);
1547 return -1;
1548 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001549
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001550 program = glCreateProgram();
1551 glAttachShader(program, v);
1552 glAttachShader(program, f);
1553 glBindAttribLocation(program, 0, "position");
1554 glBindAttribLocation(program, 1, "texcoord");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001555
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001556 glLinkProgram(program);
1557 glGetProgramiv(program, GL_LINK_STATUS, &status);
1558 if (!status) {
1559 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
1560 fprintf(stderr, "link info: %s\n", msg);
1561 return -1;
1562 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001563
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001564 glUseProgram(program);
1565 ec->proj_uniform = glGetUniformLocation(program, "proj");
1566 ec->tex_uniform = glGetUniformLocation(program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001567
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04001568 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001569}
1570
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001571void
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001572wlsc_output_destroy(struct wlsc_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001573{
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001574 destroy_surface(&output->background->surface.resource, NULL);
1575}
1576
1577void
1578wlsc_output_move(struct wlsc_output *output, int x, int y)
1579{
1580 struct wlsc_compositor *c = output->compositor;
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01001581 int flip;
1582
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001583 output->x = x;
1584 output->y = y;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001585
Benjamin Franzke264b3f92011-03-16 13:48:42 +01001586 if (output->background) {
1587 output->background->x = x;
1588 output->background->y = y;
1589 wlsc_surface_update_matrix(output->background);
1590 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001591
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001592 pixman_region32_init(&output->previous_damage_region);
1593
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001594 wlsc_matrix_init(&output->matrix);
1595 wlsc_matrix_translate(&output->matrix,
1596 -output->x - output->width / 2.0,
1597 -output->y - output->height / 2.0, 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01001598
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001599 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001600 wlsc_matrix_scale(&output->matrix,
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01001601 2.0 / output->width,
1602 flip * 2.0 / output->height, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001603
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001604 pixman_region32_union_rect(&c->damage_region,
1605 &c->damage_region,
1606 x, y, output->width, output->height);
1607}
1608
1609void
1610wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1611 int x, int y, int width, int height, uint32_t flags)
1612{
1613 output->compositor = c;
1614 output->x = x;
1615 output->y = y;
1616 output->width = width;
1617 output->height = height;
1618
1619 output->background =
1620 background_create(output, option_background);
1621
1622 output->flags = flags;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001623 output->finished = 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001624 wlsc_output_move(output, x, y);
1625
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001626 output->object.interface = &wl_output_interface;
1627 wl_display_add_object(c->wl_display, &output->object);
1628 wl_display_add_global(c->wl_display, &output->object,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001629 wlsc_output_post_geometry);
1630}
1631
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001632static void
1633shm_buffer_created(struct wl_buffer *buffer)
1634{
1635 struct wl_list *surfaces_attached_to;
1636
1637 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1638 if (!surfaces_attached_to) {
1639 buffer->user_data = NULL;
1640 return;
1641 }
1642
1643 wl_list_init(surfaces_attached_to);
1644
1645 buffer->user_data = surfaces_attached_to;
1646}
1647
1648static void
1649shm_buffer_damaged(struct wl_buffer *buffer,
1650 int32_t x, int32_t y, int32_t width, int32_t height)
1651{
1652 struct wl_list *surfaces_attached_to = buffer->user_data;
1653 struct wlsc_surface *es;
1654
1655 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1656 glBindTexture(GL_TEXTURE_2D, es->texture);
1657 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1658 buffer->width, buffer->height, 0,
1659 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1660 wl_shm_buffer_get_data(buffer));
1661 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
1662 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
1663 }
1664}
1665
1666static void
1667shm_buffer_destroyed(struct wl_buffer *buffer)
1668{
1669 struct wl_list *surfaces_attached_to = buffer->user_data;
1670 struct wlsc_surface *es, *next;
1671
1672 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
1673 es->buffer = NULL;
1674 wl_list_remove(&es->buffer_link);
1675 }
1676
1677 free(surfaces_attached_to);
1678}
1679
1680const static struct wl_shm_callbacks shm_callbacks = {
1681 shm_buffer_created,
1682 shm_buffer_damaged,
1683 shm_buffer_destroyed
1684};
1685
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001686int
1687wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1688{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001689 struct wl_event_loop *loop;
Kristian Høgsbergd711d0c2011-01-14 17:28:21 -05001690 const char *extensions;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001691
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001692 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001693
Kristian Høgsbergc5c510e2010-12-08 15:12:58 -05001694 wl_compositor_init(&ec->compositor, &compositor_interface, display);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001695
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01001696 ec->shm = wl_shm_init(display, &shm_callbacks);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001697 eglBindWaylandDisplayWL(ec->display, ec->wl_display);
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04001698
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001699 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001700 wl_list_init(&ec->input_device_list);
1701 wl_list_init(&ec->output_list);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001702 wl_list_init(&ec->binding_list);
1703
Kristian Høgsberg07937562011-04-12 17:25:42 -04001704 wlsc_shell_init(ec);
Kristian Høgsberg30021d72011-04-12 17:42:30 -04001705 wlsc_switcher_init(ec);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001706
Kristian Høgsberga8ec8632011-04-12 17:22:49 -04001707 wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0,
Kristian Høgsberg3555d092011-04-11 13:58:13 -04001708 MODIFIER_CTRL | MODIFIER_ALT,
1709 terminate_binding, ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001710
Kristian Høgsberg1db21f12010-08-16 16:08:12 -04001711 create_pointer_images(ec);
1712
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001713 screenshooter_create(ec);
1714
Kristian Høgsbergd711d0c2011-01-14 17:28:21 -05001715 extensions = (const char *) glGetString(GL_EXTENSIONS);
1716 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1717 fprintf(stderr,
1718 "GL_EXT_texture_format_BGRA8888 not available\n");
1719 return -1;
1720 }
1721
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001722 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberga9468212010-06-14 21:03:11 -04001723 if (init_shaders(ec) < 0)
1724 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001725
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001726 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001727 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
1728 wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000);
1729
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001730 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001731 pixman_region32_init(&ec->damage_region);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001732 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001733
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001734 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001735}
1736
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001737static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001738{
1739 struct wlsc_compositor *ec = data;
1740
1741 wl_display_terminate(ec->wl_display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001742
1743 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001744}
1745
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001746int main(int argc, char *argv[])
1747{
1748 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001749 struct wlsc_compositor *ec;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001750 struct wl_event_loop *loop;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001751 GError *error = NULL;
1752 GOptionContext *context;
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001753 int width, height;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001754
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04001755 g_type_init(); /* GdkPixbuf needs this, it seems. */
1756
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001757 context = g_option_context_new(NULL);
1758 g_option_context_add_main_entries(context, option_entries, "Wayland");
1759 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1760 fprintf(stderr, "option parsing failed: %s\n", error->message);
1761 exit(EXIT_FAILURE);
1762 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001763 if (sscanf(option_geometry, "%dx%d", &width, &height) != 2) {
1764 fprintf(stderr, "invalid geometry option: %s \n",
1765 option_geometry);
1766 exit(EXIT_FAILURE);
1767 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001768
1769 display = wl_display_create();
1770
Kristian Høgsberga9410222011-01-14 17:22:35 -05001771 ec = NULL;
1772
1773#if BUILD_WAYLAND_COMPOSITOR
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001774 if (getenv("WAYLAND_DISPLAY"))
1775 ec = wayland_compositor_create(display, width, height);
Kristian Høgsberga9410222011-01-14 17:22:35 -05001776#endif
1777
1778#if BUILD_X11_COMPOSITOR
1779 if (ec == NULL && getenv("DISPLAY"))
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001780 ec = x11_compositor_create(display, width, height);
Kristian Høgsberga9410222011-01-14 17:22:35 -05001781#endif
1782
Benjamin Franzke5d007092011-04-04 00:30:25 +02001783#if BUILD_OPENWFD_COMPOSITOR
1784 if (ec == NULL && getenv("OPENWFD"))
1785 ec = wfd_compositor_create(display, option_connector);
1786#endif
1787
Kristian Høgsberga9410222011-01-14 17:22:35 -05001788#if BUILD_DRM_COMPOSITOR
1789 if (ec == NULL)
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001790 ec = drm_compositor_create(display, option_connector);
Kristian Høgsberga9410222011-01-14 17:22:35 -05001791#endif
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001792
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001793 if (ec == NULL) {
1794 fprintf(stderr, "failed to create compositor\n");
1795 exit(EXIT_FAILURE);
1796 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04001797
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001798 if (wl_display_add_socket(display, option_socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001799 fprintf(stderr, "failed to add socket: %m\n");
1800 exit(EXIT_FAILURE);
1801 }
1802
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001803 loop = wl_display_get_event_loop(ec->wl_display);
1804 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
1805 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec);
1806
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001807 wl_display_run(display);
1808
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001809 eglUnbindWaylandDisplayWL(ec->display, display);
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001810 wl_display_destroy(display);
1811
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001812 ec->destroy(ec);
1813
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001814 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001815}