blob: 824bf0d013dd80802746420dc158d8359adbf763 [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øgsberg16eb6752008-10-08 22:51:32 -040019#include <stdio.h>
20#include <string.h>
21#include <stdlib.h>
22#include <stdint.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050023#include <stdarg.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040024#include <sys/ioctl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040025#include <fcntl.h>
26#include <unistd.h>
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -050027#include <cairo.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050028#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050029#include <math.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050030#include <time.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040031#include <linux/input.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050032
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -040033#define GL_GLEXT_PROTOTYPES
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -050034#define EGL_EGLEXT_PROTOTYPES
Kristian Høgsberg27803c62010-06-06 22:23:21 -040035#include <GLES2/gl2.h>
36#include <GLES2/gl2ext.h>
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -050037#include <EGL/egl.h>
38#include <EGL/eglext.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040039
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050040#include "wayland.h"
Kristian Høgsbergfe831a72008-12-21 21:50:23 -050041#include "wayland-protocol.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050042#include "cairo-util.h"
Kristian Høgsberg82863022010-06-04 21:52:02 -040043#include "compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050044
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040045const char *option_background = "background.jpg";
46int 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øgsberg81ce09a2008-12-31 16:18:42 -050053 { NULL }
54};
55
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050056
57static void
58wlsc_matrix_init(struct wlsc_matrix *matrix)
59{
60 static const struct wlsc_matrix identity = {
61 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
62 };
63
64 memcpy(matrix, &identity, sizeof identity);
65}
66
67static void
68wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
69{
70 struct wlsc_matrix tmp;
Kristian Høgsberg27803c62010-06-06 22:23:21 -040071 const GLfloat *row, *column;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050072 div_t d;
73 int i, j;
74
75 for (i = 0; i < 16; i++) {
76 tmp.d[i] = 0;
77 d = div(i, 4);
78 row = m->d + d.quot * 4;
79 column = n->d + d.rem;
80 for (j = 0; j < 4; j++)
81 tmp.d[i] += row[j] * column[j * 4];
82 }
83 memcpy(m, &tmp, sizeof tmp);
84}
85
86static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040087wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050088{
Kristian Høgsberg27803c62010-06-06 22:23:21 -040089 struct wlsc_matrix translate = {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050090 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
91 };
92
93 wlsc_matrix_multiply(matrix, &translate);
94}
95
96static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -040097wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050098{
99 struct wlsc_matrix scale = {
100 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
101 };
102
103 wlsc_matrix_multiply(matrix, &scale);
104}
105
106static void
107wlsc_matrix_rotate(struct wlsc_matrix *matrix,
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400108 GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500109{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400110 GLfloat c = cos(angle);
111 GLfloat s = sin(angle);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500112 struct wlsc_matrix rotate = {
113 { x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0,
Kristian Høgsbergfa548852010-03-08 17:16:01 -0500114 x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) + x * s, 0,
115 x * z * (1 - c) + y * s, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0,
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500116 0, 0, 0, 1 }
117 };
118
119 wlsc_matrix_multiply(matrix, &rotate);
120}
121
122static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400123wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
124{
125 int i, j;
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400126 struct wlsc_vector t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400127
128 for (i = 0; i < 4; i++) {
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400129 t.f[i] = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400130 for (j = 0; j < 4; j++)
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400131 t.f[i] += v->f[j] * matrix->d[i + j * 4];
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400132 }
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400133
134 *v = t;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400135}
136
137static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500138wlsc_surface_init(struct wlsc_surface *surface,
139 struct wlsc_compositor *compositor, struct wl_visual *visual,
140 int32_t x, int32_t y, int32_t width, int32_t height)
141{
142 glGenTextures(1, &surface->texture);
143 surface->compositor = compositor;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500144 surface->visual = visual;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400145 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400146 wlsc_matrix_scale(&surface->matrix, width, height, 1);
147 wlsc_matrix_translate(&surface->matrix, x, y, 0);
148
149 wlsc_matrix_init(&surface->matrix_inv);
150 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
151 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500152}
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500153
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500154static struct wlsc_surface *
155wlsc_surface_create_from_cairo_surface(struct wlsc_compositor *ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500156 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500157 int x, int y, int width, int height)
158{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500159 struct wlsc_surface *es;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500160 int stride;
161 void *data;
162
163 stride = cairo_image_surface_get_stride(surface);
164 data = cairo_image_surface_get_data(surface);
165
166 es = malloc(sizeof *es);
167 if (es == NULL)
168 return NULL;
169
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500170 wlsc_surface_init(es, ec, &ec->premultiplied_argb_visual,
171 x, y, width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500172 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400173 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
174 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500175 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
176 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500177 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400178 GL_RGBA, GL_UNSIGNED_BYTE, data);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500179
Kristian Høgsberg54879822008-11-23 17:07:32 -0500180 return es;
181}
182
183static void
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400184wlsc_surface_destroy(struct wlsc_surface *surface,
185 struct wlsc_compositor *compositor)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500186{
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500187 struct wlsc_listener *l;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400188
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400189 wl_list_remove(&surface->link);
190 glDeleteTextures(1, &surface->texture);
191 wl_client_remove_surface(surface->base.client, &surface->base);
192
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500193 wl_list_for_each(l, &compositor->surface_destroy_listener_list, link)
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400194 l->func(l, surface);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400195
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400196 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500197}
198
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400199static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500200pointer_path(cairo_t *cr, int x, int y)
201{
202 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
203 const int width = 16, height = 16;
204
205 cairo_move_to(cr, x, y);
206 cairo_line_to(cr, x + tx, y + ty);
207 cairo_line_to(cr, x + dx, y + dy);
208 cairo_line_to(cr, x + width - end, y + height);
209 cairo_line_to(cr, x + width, y + height - end);
210 cairo_line_to(cr, x + dy, y + dx);
211 cairo_line_to(cr, x + ty, y + tx);
212 cairo_close_path(cr);
213}
214
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500215static struct wlsc_surface *
216pointer_create(struct wlsc_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500217{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500218 struct wlsc_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500219 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500220 cairo_surface_t *surface;
221 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500222
223 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
224 width, height);
225
226 cr = cairo_create(surface);
227 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500228 cairo_set_line_width(cr, 2);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500229 cairo_set_source_rgb(cr, 0, 0, 0);
230 cairo_stroke_preserve(cr);
231 cairo_fill(cr);
232 blur_surface(surface, width);
233
234 pointer_path(cr, hotspot_x, hotspot_y);
235 cairo_stroke_preserve(cr);
236 cairo_set_source_rgb(cr, 1, 1, 1);
237 cairo_fill(cr);
238 cairo_destroy(cr);
239
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500240 es = wlsc_surface_create_from_cairo_surface(ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500241 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500242 x - hotspot_x,
243 y - hotspot_y,
244 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500245
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500246 cairo_surface_destroy(surface);
247
Kristian Høgsberg54879822008-11-23 17:07:32 -0500248 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500249}
250
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500251static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500252background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500253{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500254 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500255 GdkPixbuf *pixbuf;
256 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500257 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500258 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500259
260 background = malloc(sizeof *background);
261 if (background == NULL)
262 return NULL;
263
264 g_type_init();
265
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500266 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500267 output->width,
268 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500269 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500270 if (error != NULL) {
271 free(background);
272 return NULL;
273 }
274
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500275 data = gdk_pixbuf_get_pixels(pixbuf);
276
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500277 wlsc_surface_init(background, output->compositor,
278 &output->compositor->rgb_visual,
279 output->x, output->y, output->width, output->height);
280
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500281 glBindTexture(GL_TEXTURE_2D, background->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400282 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
283 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500284 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
285 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Ray Strode18fd33c2008-12-18 21:05:20 -0500286
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500287 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500288 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500289 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500290 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500291
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500292 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
293 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500294 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500295
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500296 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500297}
298
299static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400300wlsc_surface_draw(struct wlsc_surface *es, struct wlsc_output *output)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500301{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500302 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400303 struct wlsc_matrix tmp;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500304
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400305 tmp = es->matrix;
306 wlsc_matrix_multiply(&tmp, &output->matrix);
307 glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, tmp.d);
308 glUniform1i(ec->tex_uniform, 0);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500309
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500310 if (es->visual == &ec->argb_visual) {
311 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
312 glEnable(GL_BLEND);
313 } else if (es->visual == &ec->premultiplied_argb_visual) {
314 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
315 glEnable(GL_BLEND);
316 } else {
317 glDisable(GL_BLEND);
318 }
319
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500320 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500321 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400322 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
323 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
324 5 * sizeof(GLfloat), NULL);
325 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
326 5 * sizeof(GLfloat), (GLfloat *) 0 + 3);
327 glEnableVertexAttribArray(0);
328 glEnableVertexAttribArray(1);
329 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500330}
331
332static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400333wlsc_surface_raise(struct wlsc_surface *surface)
334{
335 struct wlsc_compositor *compositor = surface->compositor;
336
337 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400338 wl_list_insert(&compositor->surface_list, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400339}
340
341static void
342wlsc_surface_lower(struct wlsc_surface *surface)
343{
344 struct wlsc_compositor *compositor = surface->compositor;
345
346 wl_list_remove(&surface->link);
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400347 wl_list_insert(compositor->surface_list.prev, &surface->link);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400348}
349
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400350void
351wlsc_compositor_finish_frame(struct wlsc_compositor *compositor, int msecs)
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400352{
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400353 wl_display_post_frame(compositor->wl_display,
354 &compositor->base,
355 compositor->current_frame, msecs);
356
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400357 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400358 compositor->current_frame++;
359}
360
361static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400362wlsc_output_repaint(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400363{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500364 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500365 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500366 struct wlsc_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500367
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500368 glViewport(0, 0, output->width, output->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500369
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500370 if (output->background)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400371 wlsc_surface_draw(output->background, output);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500372 else
373 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400374
Kristian Høgsberg747638b2010-07-12 17:06:06 -0400375 wl_list_for_each_reverse(es, &ec->surface_list, link)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400376 wlsc_surface_draw(es, output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400377
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400378 if (ec->focus)
379 wl_list_for_each(eid, &ec->input_device_list, link)
380 wlsc_surface_draw(eid->sprite, output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500381}
382
383static void
384repaint(void *data)
385{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500386 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500387 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500388
389 if (!ec->repaint_needed) {
390 ec->repaint_on_timeout = 0;
391 return;
392 }
393
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500394 wl_list_for_each(output, &ec->output_list, link)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400395 wlsc_output_repaint(output);
396
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400397 ec->present(ec);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500398
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500399 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400400}
401
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400402void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400403wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400404{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400405 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400406 if (compositor->repaint_on_timeout)
407 return;
408
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400409 wl_event_source_timer_update(compositor->timer_source, 1);
410 compositor->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400411}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500412
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500413static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500414surface_destroy(struct wl_client *client,
415 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400416{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500417 struct wlsc_surface *es = (struct wlsc_surface *) surface;
418 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500419
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500420 wlsc_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400421
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400422 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400423}
424
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500425static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500426surface_attach(struct wl_client *client,
427 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500428 uint32_t width, uint32_t height, uint32_t stride,
429 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400430{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500431 struct wlsc_surface *es = (struct wlsc_surface *) surface;
432 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500433 EGLint attribs[] = {
Kristian Høgsberga2ee6752010-06-04 22:14:28 -0400434 EGL_WIDTH, 0,
435 EGL_HEIGHT, 0,
Kristian Høgsberga2ee6752010-06-04 22:14:28 -0400436 EGL_IMAGE_STRIDE_MESA, 0,
Kristian Høgsberg6619d382010-06-07 12:23:01 -0400437 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500438 EGL_NONE
439 };
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400440
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500441 es->width = width;
442 es->height = height;
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400443
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500444 if (visual == &ec->argb_visual.base)
445 es->visual = &ec->argb_visual;
446 else if (visual == &ec->premultiplied_argb_visual.base)
447 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500448 else if (visual == &ec->rgb_visual.base)
449 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500450 else
451 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400452
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500453 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400454 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
455 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500456 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
457 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500458
459 if (es->image)
460 eglDestroyImageKHR(ec->display, es->image);
461
462 attribs[1] = width;
463 attribs[3] = height;
Kristian Høgsberg12923832010-06-06 22:44:12 -0400464 attribs[5] = stride / 4;
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500465
466 es->image = eglCreateImageKHR(ec->display, ec->context,
Kristian Høgsberg12923832010-06-06 22:44:12 -0400467 EGL_DRM_IMAGE_MESA,
468 (EGLClientBuffer) name, attribs);
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500469 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, es->image);
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400470
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400471}
472
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500473static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500474surface_map(struct wl_client *client,
475 struct wl_surface *surface,
476 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400477{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500478 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400479
Kristian Høgsberg2cdc1842010-06-08 10:44:38 -0400480 wlsc_matrix_init(&es->matrix);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400481 wlsc_matrix_scale(&es->matrix, width, height, 1);
Kristian Høgsberg2cdc1842010-06-08 10:44:38 -0400482 wlsc_matrix_translate(&es->matrix, x, y, 0);
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400483
484 wlsc_matrix_init(&es->matrix_inv);
485 wlsc_matrix_translate(&es->matrix_inv, -x, -y, 0);
486 wlsc_matrix_scale(&es->matrix_inv, 1.0 / width, 1.0 / height, 1);
487
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400488}
489
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500490static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500491surface_damage(struct wl_client *client,
492 struct wl_surface *surface,
493 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500494{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500495 /* FIXME: This need to take a damage region, of course. */
496}
497
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500498const static struct wl_surface_interface surface_interface = {
499 surface_destroy,
500 surface_attach,
501 surface_map,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500502 surface_damage
503};
504
505static void
506compositor_create_surface(struct wl_client *client,
507 struct wl_compositor *compositor, uint32_t id)
508{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500509 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400510 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500511
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400512 surface = malloc(sizeof *surface);
513 if (surface == NULL)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500514 /* FIXME: Send OOM event. */
515 return;
516
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400517 wlsc_surface_init(surface, ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500518
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400519 wl_list_insert(ec->surface_list.prev, &surface->link);
520 wl_client_add_surface(client, &surface->base,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500521 &surface_interface, id);
522}
523
524static void
525compositor_commit(struct wl_client *client,
526 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500527{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500528 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500529
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400530 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500531 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500532}
533
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500534const static struct wl_compositor_interface compositor_interface = {
535 compositor_create_surface,
536 compositor_commit
537};
538
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500539static void
540wlsc_surface_transform(struct wlsc_surface *surface,
541 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
542{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400543 struct wlsc_vector v = { { x, y, 0, 1 } };
544
545 wlsc_matrix_transform(&surface->matrix_inv, &v);
Kristian Høgsberg0b8646b2010-06-08 15:29:14 -0400546 *sx = v.f[0] * surface->width;
547 *sy = v.f[1] * surface->height;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500548}
549
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500550static void
551wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
552 struct wlsc_surface *surface)
553{
554 if (device->keyboard_focus == surface)
555 return;
556
557 if (device->keyboard_focus &&
558 (!surface || device->keyboard_focus->base.client != surface->base.client))
559 wl_surface_post_event(&device->keyboard_focus->base,
560 &device->base,
Kristian Høgsberg786ca0d2009-03-06 21:25:21 -0500561 WL_INPUT_KEYBOARD_FOCUS, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500562
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500563 if (surface)
564 wl_surface_post_event(&surface->base,
565 &device->base,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500566 WL_INPUT_KEYBOARD_FOCUS,
567 &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500568
569 device->keyboard_focus = surface;
570}
571
572static void
573wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
574 struct wlsc_surface *surface)
575{
576 if (device->pointer_focus == surface)
577 return;
578
579 if (device->pointer_focus &&
580 (!surface || device->pointer_focus->base.client != surface->base.client))
581 wl_surface_post_event(&device->pointer_focus->base,
582 &device->base,
583 WL_INPUT_POINTER_FOCUS, NULL);
584 if (surface)
585 wl_surface_post_event(&surface->base,
586 &device->base,
587 WL_INPUT_POINTER_FOCUS, &surface->base);
588
589 device->pointer_focus = surface;
590}
591
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500592static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500593pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500594{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500595 struct wlsc_compositor *ec = device->ec;
596 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500597
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500598 if (device->grab > 0) {
599 wlsc_surface_transform(device->grab_surface,
600 device->x, device->y, sx, sy);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500601 return device->grab_surface;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500602 }
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500603
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400604 wl_list_for_each(es, &ec->surface_list, link) {
605 wlsc_surface_transform(es, device->x, device->y, sx, sy);
606 if (0 <= *sx && *sx < es->width &&
607 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500608 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400609 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500610
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500611 return NULL;
612}
613
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500614void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500615notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500616{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500617 struct wlsc_surface *es;
618 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500619 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500620 const int hotspot_x = 16, hotspot_y = 16;
621 int32_t sx, sy;
622
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500623 /* FIXME: We need some multi head love here. */
624 output = container_of(ec->output_list.next, struct wlsc_output, link);
625 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500626 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500627 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500628 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500629 if (x >= output->x + output->width)
630 x = output->x + output->width - 1;
631 if (y >= output->y + output->height)
632 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500633
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500634 device->x = x;
635 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500636 es = pick_surface(device, &sx, &sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500637
638 wlsc_input_device_set_pointer_focus(device, es);
639
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500640 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500641 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500642 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500643
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400644 wlsc_matrix_init(&device->sprite->matrix);
645 wlsc_matrix_scale(&device->sprite->matrix, 64, 64, 1);
646 wlsc_matrix_translate(&device->sprite->matrix,
647 x - hotspot_x, y - hotspot_y, 0);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500648
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400649 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500650}
651
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500652void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500653notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500654 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500655{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400656 struct wlsc_surface *surface;
657 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500658 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500659
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400660 surface = pick_surface(device, &sx, &sy);
661 if (surface) {
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500662 if (state) {
Kristian Høgsbergffbc6072009-09-18 17:03:18 -0400663 wlsc_surface_raise(surface);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500664 device->grab++;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400665 device->grab_surface = surface;
666 wlsc_input_device_set_keyboard_focus(device,
667 surface);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500668 } else {
669 device->grab--;
670 }
671
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500672 /* FIXME: Swallow click on raise? */
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400673 wl_surface_post_event(&surface->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500674 WL_INPUT_BUTTON, button, state,
675 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500676
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400677 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500678 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500679}
680
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500681void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500682notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500683 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500684{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400685 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500686 uint32_t *k, *end;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400687 uint32_t modifier;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500688
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400689 switch (key | compositor->modifier_state) {
690 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400691 kill(0, SIGTERM);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500692 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500693 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500694
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400695 switch (key) {
696 case KEY_LEFTCTRL:
697 case KEY_RIGHTCTRL:
698 modifier = MODIFIER_CTRL;
699 break;
700
701 case KEY_LEFTALT:
702 case KEY_RIGHTALT:
703 modifier = MODIFIER_ALT;
704 break;
705
706 default:
707 modifier = 0;
708 break;
709 }
710
711 if (state)
712 compositor->modifier_state |= modifier;
713 else
714 compositor->modifier_state &= ~modifier;
715
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500716 end = device->keys.data + device->keys.size;
717 for (k = device->keys.data; k < end; k++) {
718 if (*k == key)
719 *k = *--end;
720 }
721 device->keys.size = (void *) end - device->keys.data;
722 if (state) {
723 k = wl_array_add(&device->keys, sizeof *k);
724 *k = key;
725 }
Ray Strodee96dcb82008-12-20 02:00:49 -0500726
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500727 if (device->keyboard_focus != NULL)
728 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -0500729 &device->base,
730 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500731}
732
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400733static void
734handle_surface_destroy(struct wlsc_listener *listener,
735 struct wlsc_surface *surface)
736{
737 struct wlsc_input_device *device =
738 container_of(listener, struct wlsc_input_device, listener);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400739 struct wlsc_surface *focus;
740 int32_t sx, sy;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400741
742 if (device->grab_surface == surface) {
743 device->grab_surface = NULL;
744 device->grab = 0;
745 }
746 if (device->keyboard_focus == surface)
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400747 wlsc_input_device_set_keyboard_focus(device, NULL);
748 if (device->pointer_focus == surface) {
749 focus = pick_surface(device, &sx, &sy);
750 wlsc_input_device_set_pointer_focus(device, focus);
751 fprintf(stderr, "lost pointer focus surface, reverting to %p\n", focus);
752 }
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400753}
754
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400755void
756wlsc_input_device_init(struct wlsc_input_device *device,
757 struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500758{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500759 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500760 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500761 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500762 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500763 device->x = 100;
764 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500765 device->ec = ec;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400766 device->sprite = pointer_create(ec, device->x, device->y, 64, 64);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500767
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400768 device->listener.func = handle_surface_destroy;
769 wl_list_insert(ec->surface_destroy_listener_list.prev,
770 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500771 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500772}
773
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500774static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400775wlsc_output_post_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500776{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500777 struct wlsc_output *output =
778 container_of(global, struct wlsc_output, base);
779
780 wl_client_post_event(client, global,
781 WL_OUTPUT_GEOMETRY,
782 output->width, output->height);
783}
784
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400785static const char vertex_shader[] =
786 "uniform mat4 proj;\n"
787 "attribute vec4 position;\n"
788 "attribute vec2 texcoord;\n"
789 "varying vec2 v_texcoord;\n"
790 "void main()\n"
791 "{\n"
792 " gl_Position = proj * position;\n"
793 " v_texcoord = texcoord;\n"
794 "}\n";
795
796static const char fragment_shader[] =
797 /* "precision mediump float;\n" */
798 "varying vec2 v_texcoord;\n"
799 "uniform sampler2D tex;\n"
800 "void main()\n"
801 "{\n"
802 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
803 "}\n";
804
Kristian Høgsberga9468212010-06-14 21:03:11 -0400805static int
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400806init_shaders(struct wlsc_compositor *ec)
807{
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400808 GLuint v, f, program;
809 const char *p;
810 char msg[512];
811 GLfloat vertices[4 * 5];
812 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400813
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400814 p = vertex_shader;
815 v = glCreateShader(GL_VERTEX_SHADER);
816 glShaderSource(v, 1, &p, NULL);
817 glCompileShader(v);
818 glGetShaderiv(v, GL_COMPILE_STATUS, &status);
819 if (!status) {
820 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
821 fprintf(stderr, "vertex shader info: %s\n", msg);
822 return -1;
823 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400824
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400825 p = fragment_shader;
826 f = glCreateShader(GL_FRAGMENT_SHADER);
827 glShaderSource(f, 1, &p, NULL);
828 glCompileShader(f);
829 glGetShaderiv(f, GL_COMPILE_STATUS, &status);
830 if (!status) {
831 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
832 fprintf(stderr, "fragment shader info: %s\n", msg);
833 return -1;
834 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400835
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400836 program = glCreateProgram();
837 glAttachShader(program, v);
838 glAttachShader(program, f);
839 glBindAttribLocation(program, 0, "position");
840 glBindAttribLocation(program, 1, "texcoord");
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400841
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400842 glLinkProgram(program);
843 glGetProgramiv(program, GL_LINK_STATUS, &status);
844 if (!status) {
845 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
846 fprintf(stderr, "link info: %s\n", msg);
847 return -1;
848 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400849
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400850 glUseProgram(program);
851 ec->proj_uniform = glGetUniformLocation(program, "proj");
852 ec->tex_uniform = glGetUniformLocation(program, "tex");
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400853
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400854 vertices[ 0] = 0.0;
855 vertices[ 1] = 0.0;
856 vertices[ 2] = 0.0;
857 vertices[ 3] = 0.0;
858 vertices[ 4] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400859
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400860 vertices[ 5] = 0.0;
861 vertices[ 6] = 1.0;
862 vertices[ 7] = 0.0;
863 vertices[ 8] = 0.0;
864 vertices[ 9] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400865
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400866 vertices[10] = 1.0;
867 vertices[11] = 0.0;
868 vertices[12] = 0.0;
869 vertices[13] = 1.0;
870 vertices[14] = 0.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400871
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400872 vertices[15] = 1.0;
873 vertices[16] = 1.0;
874 vertices[17] = 0.0;
875 vertices[18] = 1.0;
876 vertices[19] = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400877
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400878 glGenBuffers(1, &ec->vbo);
879 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
880 glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
Kristian Høgsberga9468212010-06-14 21:03:11 -0400881
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -0400882 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400883}
884
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500885static const struct wl_interface visual_interface = {
886 "visual", 1,
887};
888
889static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500890add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500891{
892 ec->argb_visual.base.interface = &visual_interface;
893 ec->argb_visual.base.implementation = NULL;
894 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500895 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500896
897 ec->premultiplied_argb_visual.base.interface = &visual_interface;
898 ec->premultiplied_argb_visual.base.implementation = NULL;
899 wl_display_add_object(ec->wl_display,
900 &ec->premultiplied_argb_visual.base);
901 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500902 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500903
904 ec->rgb_visual.base.interface = &visual_interface;
905 ec->rgb_visual.base.implementation = NULL;
906 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500907 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
908}
909
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400910void
911wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
912 int x, int y, int width, int height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400913{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400914 output->compositor = c;
915 output->x = x;
916 output->y = y;
917 output->width = width;
918 output->height = height;
919
920 output->background =
921 background_create(output, option_background);
922
923 wlsc_matrix_init(&output->matrix);
924 wlsc_matrix_translate(&output->matrix,
925 -output->x - output->width / 2.0,
926 -output->y - output->height / 2.0, 0);
927 wlsc_matrix_scale(&output->matrix,
928 2.0 / output->width, 2.0 / output->height, 1);
929
930 output->base.interface = &wl_output_interface;
931 wl_display_add_object(c->wl_display, &output->base);
932 wl_display_add_global(c->wl_display, &output->base,
933 wlsc_output_post_geometry);
934}
935
936int
937wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
938{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500939 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400940
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400941 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400942
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500943 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500944
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500945 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500946
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500947 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500948 wl_list_init(&ec->input_device_list);
949 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400950 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400951
952 screenshooter_create(ec);
953
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400954 glGenFramebuffers(1, &ec->fbo);
955 glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo);
956 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberga9468212010-06-14 21:03:11 -0400957 if (init_shaders(ec) < 0)
958 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500959
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500960 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500961 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400962 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400963
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400964 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -0500965}
966
967/* The plan here is to generate a random anonymous socket name and
968 * advertise that through a service on the session dbus.
969 */
970static const char socket_name[] = "\0wayland";
971
972int main(int argc, char *argv[])
973{
974 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500975 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -0500976 GError *error = NULL;
977 GOptionContext *context;
978
979 context = g_option_context_new(NULL);
980 g_option_context_add_main_entries(context, option_entries, "Wayland");
981 if (!g_option_context_parse(context, &argc, &argv, &error)) {
982 fprintf(stderr, "option parsing failed: %s\n", error->message);
983 exit(EXIT_FAILURE);
984 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -0500985
986 display = wl_display_create();
987
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400988 if (getenv("DISPLAY"))
989 ec = x11_compositor_create(display);
990 else
991 ec = drm_compositor_create(display);
992
Kristian Høgsberg841883b2008-12-05 11:19:56 -0500993 if (ec == NULL) {
994 fprintf(stderr, "failed to create compositor\n");
995 exit(EXIT_FAILURE);
996 }
997
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500998 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -0500999 fprintf(stderr, "failed to add socket: %m\n");
1000 exit(EXIT_FAILURE);
1001 }
1002
1003 wl_display_run(display);
1004
1005 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001006}